Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle query is slow (or fails) from .NET app but is fast from SQL Developer

We use ODP.NET to perform queries on Oracle databases, and normally it works fine. There is a particular database, and a particular view in that database, though, that we just can't complete a query on from .NET. For example:

SELECT some_varchar_field FROM the_view WHERE ROWNUM < 5;

If I execute this query from within Oracle SQL developer, it finishes in less than a second. If I do an identical query from our .NET application using ODP.NET, it hangs and eventually produces an "ORA-03135: connection lost contact" error. I think that limiting it to just a few rows eliminates the possibility that it is as FetchSize issue.

There are other queries I can execute successfully, but they are slower from our program than from SQL Developer. Again, I realize SQL Developer only gets data for the first 50 rows initially, but I think the ROWNUM condition takes that out of the equation.

What might be different about the connection or command that Oracle SQL Developer is using vs the one our application is using that would cause a difference in speed?

Unfortunately, I do not have access to the server (other than to run Oracle queries against it).

Thank you.

UPDATE: I have tried the same query with Microsoft's Oracle provider and it executes very quickly. Unfortunately, that provider is deprecated so this is not a long term solution.

like image 371
Greg Smalter Avatar asked Sep 07 '10 02:09

Greg Smalter


People also ask

Why does Oracle query take so long to execute?

Check if the database is also slow or it is performing well . If database performance is already low then queries may take longer time. 4. The time you are running your query , check parameters such as Disk I/O, swap utilization, Memory and CPU utilization of the database server, These should not hit the maximum.

Why is my Oracle DB slow?

The most common causes of slow performance are as follows: Excessive round-trips from the application server to the database. Ideally, each UI operation should require exactly one round-trip to the database. Sometimes, the framework will require additional round-trips to retrieve and make session data persistent.


2 Answers

It had nothing to do with the ODP.NET provider. The problem was that the library we use to create connections for us (which, of course, is not used by Oracle SQL Developer, and which I did not use when I tried the Microsoft provider) was always executing the following statements before doing anything:

ALTER SESSION SET NLS_COMP = LINGUISTIC
ALTER SESSION SET NLS_SORT = BINARY_CI

These make Oracle case-insensitive. But, they also render all conventional indexes useless. Because we were querying from a View, it had ordering built in. And because we don't own the database, we can't make the indexes linguistic to fix the performance problem.

Providing a way to not execute those statements in this (rare) scenario fixed the problem.

like image 78
Greg Smalter Avatar answered Oct 21 '22 19:10

Greg Smalter


Immediate thoughts are

  1. CLOB, BLOB or LONG/LONG RAW which requires a lot of bandwidth for just a few rows.
  2. Invalid data (eg there are ways to get an invalid date into a date field, which may confuse some clients)
  3. "the_table" isn't actually a table but a view or something with a complex derivation or has a VPD/RLS/FGAC security policy on it.
  4. Exotic datatype (Spatial or User Defined).

Suggestions

  1. Explicitly list the columns (eg SELECT a,b,c FROM the_table WHERE ROWNUM < 5). Add columns one by one until it stops working. That assumes there is at least one 'simple' column in the table.
  2. Check the session in v$session to see what the wait EVENT is. Either the DB server is burning CPU for this SQL, or it is waiting for something (possibly the client).
  3. Check the SQL in v$sql. Is there one or more child cursors. is there one or more PLAN_HASH_VALUEs. Different child cursors can use different plans. Without a WHERE clause other than ROWNUM, this is pretty unlikely.
like image 21
Gary Myers Avatar answered Oct 21 '22 18:10

Gary Myers