Oracle – Why is PLSQL slower than SQL*Plus

oracleperformanceplsql

I have several Oracle queries that perform well when run through SQL*PLUS. However when they are executed as a part of a PL/SQL package, they take MUCH longer.

Our DBA has watched these queries take 10 minutes through PLSQL and 10 seconds through SQL*Plus.

Does anybody have any pointers on where to look for the misconfiguration?

Client – Windows 2000
Server – Linux (Oracle Enterprise)

Thanks

Resolution:

I wish I could have accepted everyone's answers. Several of them were quite helpful.

  • The query was converting data types.
  • The execution plans didn't match.
    (Hints fixed that.)
  • The DBA was looking at the time the cursor was
    open instead of the query time.

Best Answer

Use SQL trace to see what the execution plans are in each case. One possibility that springs to mind (from experience): is the package binding the wrong type of values to the query? It could be that in SQL Plus you are running:

select * from mytable where id = '1234';

but in PL/SQL you are running:

select * from mytable where id = p_id;

with p_id being defined as a number. That will force a TO_NUMBER on the ID column and prevent Oracle using the index.