Professional Documents
Culture Documents
Buy Quest Products Buy Guy'S Book Buy Quest Products: Top Tips For Oracle SQL Tuning
Buy Quest Products Buy Guy'S Book Buy Quest Products: Top Tips For Oracle SQL Tuning
Effective SQL tuning requires either familiarity with these tools or the use of commercial alternatives such as SQLab
EXPLAIN PLAN
The EXPLAIN PLAN reveals the execution plan for an SQL statement. The execution plan reveals the exact sequence of steps that the Oracle optimizer has chosen to employ to process the SQL. The execution plan is stored in an Oracle table called the plan table Suitably formatted queries can be used to extract the execution plan from the plan table.
Tkprof output contains breakdown of execution statistics, execution plan and rows returned for each step. These stats are not available from any other source.
Tkprof is the most powerful tool, but requires a significant learning curve.
Tkprof output
count2 ------ -----Parsea 1d Executeb 1e Fetchc 20j ------ -----total 22 cpu3 elapsed4 disk5 query6 current7 rows8 ------ -------- ------- -------- -------- -----0.02 0.01 0 0 0 0 0.00 0.00 0 0 0 0 141.10 141.65 1237 1450011 386332 99i ------ -------- ------- -------- -------- -----141.12 141.66 1237k 1450011f 386332g 99h
Rowsl Execution Planm ------- --------------------------------------------------0 SELECT STATEMENT GOAL: CHOOSE 99 FILTER 96681 TABLE ACCESS GOAL: ANALYZED (FULL) OF 'CUSTOMERS' 96582 TABLE ACCESS GOAL: ANALYZED (FULL) OF 'EMPLOYEES'
Using SQLab
Because EXPLAIN PLAN and tkprof are unwieldy and hard to interpret, third party tools that automate the process and provide expert advice improve SQL tuning efficiency. The Quest SQLab product:
Identifies SQL your database that could benefit from tuning Provides a sophisticated tuning environment to examine, compare and evaluate execution plans. Incorporates an expert system to advise on indexing and SQL statement changes
700
Merge 3 indexes
40
20
100
200
300
400 Logical IO
500
600
700
800
Bit-map indexes
Contrary to widespread belief, can be effective when there are many distinct column values Not suitable for OLTP however
100
10
0.1
0.01 1 10 100 1,000 Distinct values Bitmap index B*-Tree index Full table scan 10,000 100,000 1,000,000
Index range scan (RULE) Full index scan Full table scan Parallel table scan fast full index Parallel fast full index 0 2.44 5 10 Elapsed time (s) 15 20 5.23 4.94 12.53 17.76
19.83
Oracle 8 semi-joins
Optimizes queries using EXISTS where there is no supporting index
select * No index on employees from customers c where exists (select 1 from employees e where e.surname=c.contact_surname and e.firstname=c.contact_firstname and e.date_of_birth=c.date_of_birth)
Oracle 8 semi-joins
Without the semi-join or supporting index, queries like the one on the preceding slide will perform very badly. Oracle will perform a tablescan of the inner table for each row retrieved by the outer table If customers has 100,000 rows, and employees 800 rows then 80 MILLION rows will be processed! In Oracle7, you should create the index or use an INbased subquery In Oracle8, the semi-join facility allows the query to be resolved by a sort-merge or hash join.
To Use semi-joins
Set ALWAYS_SEMI_JOIN=HASH or MERGE in INIT.ORA, OR Use a MERGE_SJ or HASH_SJ hint in the subquery of the SQL statement
SELECT * FROM customers c WHERE exists (select /*+merge_sj*/ 1 from employees e where .)
Oracle8 semi-joins
The performance improvements are impressive (note the logarithmic scale)
1,343.19 EXISTS no semi-join or indexes
10
1,000
10,000
9.94
0.01
10
50
40
Elapsed time
30
20
10
0 0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 Array size
Temporary tables
The profiler Dynamic SQL
Bonus hint: When your SQL is tuned, look to your Oracle configuration
When SQL is inefficient there is limited benefit in investing in Oracle server or operating system tuning. However, once SQL is tuned, the limiting factor for performance will be Oracle and operating system configuration. In particular, check for internal Oracle contention that typically shows up as latch contention or unusual wait conditions (buffer busy, free buffer, etc) Third party tools such as Quests Spotlight on Oracle product can be invaluable
www.quest.com