Oracle Database – Performance by Example

‚Bad‘ Performance by Example in the SQL Where Clause

WHERE (:v_name IS NULL OR last_name = :v_name)
AND (:v_odate IS NULL OR TO_CHAR(order_date,’DD-MM-YYYY‘) = :v_odate)
AND (:v_cid IS NULL OR cust_id = :v_cid)

Das Problem dabei ist das ‚OR‘ – auf Grund von ‚OR‘ für jede Spalte schätzt der Oracle cost-based optimizer (CBO) einerseits die Selektivität falsch ein und andererseits werden die Kosten für Indexzugriffe oft genug so teuer veranschlagt dass das Resultat ein Full Table Scan ist

‚Better‘ Performance by Example in the SQL Where Clause

WHERE last_name = NVL(:v_name,last_name)
AND order_date = TO_DATE(NVL(:v_odate,order_date),’DD-MM-YYYY‘)
AND cust_id = NVL(:v_cid,cust_id)

Leave a Reply

You must be logged in to post a comment.