简体   繁体   中英

Queries runs slow without parameters in SQL Developer

When I run a query with SQL Developer and use bind variables like :type I'm prompted for values to use, and then then the queries run faster than if I replace the bind variables with fixed literal value, say 'A' .

For instance the first query that runs faster than the second query:

First query (runs 0.5 secs)

select DMDUNIT, LOC, SUM(QTY)
from hist
where dmdunit = :lv_dmdunit and type = :lv_type and loc = :lv_loc
and startdate >= sysdate - 40
GROUP BY DMDUNIT,LOC

Second query (runs 1.7 secs)

select DMDUNIT, LOC, SUM(QTY)
from hist
where dmdunit = 'PROD_1' and type = 'A' and loc = 'B101'
and startdate >= sysdate - 40
GROUP BY DMDUNIT,LOC

Why does the first query run faster then the second query? What steps can I take to make the second query run as fast as the first?

This is a small query and 1 second does not make much difference, but I also have large queries where the difference between the same queries (params and non-params) are 10 to 15 minutes.

Here is an interesting test: Just for fun, change the names of your bind variables (for example, call them :x, :y and :z) - or, even better, just change one letter in one of the variables. The FIRST time you run the query it should take about as long as the second query, if all else is equal (which it never is).

One of the advantages of using bind variables is that the query is parsed just once. The engine still needs to run the query, but the added overhead of interpreting the query, optimizing it, etc. is avoided. The technical terms are "soft parsing" vs. "hard parsing." Good reading on this:

https://blogs.oracle.com/sql/entry/improve_sql_query_performance_by

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM