tuning oracle sql - sfoug.org · the basics of efficient sql resorts on result after where and...
TRANSCRIPT
![Page 1: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/1.jpg)
Tuning Oracle SQLTuning Oracle SQL
• The Basics of Efficient SQL• Common Sense Indexing• The Optimizer:
– Making SQL Efficient
• Finding Problem Queries• Oracle Enterprise Manager
– Wait Event Interface
![Page 2: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/2.jpg)
• SELECTSELECT– FOR UPDATEFOR UPDATE
• Filtering– WHERE
• ORDER BY– often ignored– query complexity
• GROUP BY
The Basics of Efficient SQLThe Basics of Efficient SQLSELECT ** FROM division;SELECT division_id, name, city, state, country FROM division;SELECT division_id FROM division;
![Page 3: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/3.jpg)
• SELECT– FOR UPDATE
• Filtering– WHEREWHERE
• ORDER BY– often ignored– query complexity
• GROUP BY
The Basics of Efficient SQLThe Basics of Efficient SQLAvoid unintentional full table scans
SELECT * FROM division WHERE country LIKE '%a%';
Match indexes Exact hits (equality)SELECT * FROM division WHERE division_id = 1;
Range scans / skip scans / full index scans
EXISTS (correlate) faster than IN
Biggest filters first
Full table scans can sometimes be faster
![Page 4: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/4.jpg)
The Basics of Efficient SQLThe Basics of Efficient SQLResorts on result after WHERE and GROUP BY
Don’t repeat sorting (ORDER BY often ignored)
by SELECTSELECT division_id FROM division ORDER BY division_id;
by WHERESELECT * FROM division WHERE division_id < 10 ORDER BY division_id;
GROUP BYSELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state;by DISTINCTSELECT DISTINCT(state) FROM division ORDER BY state;
by indexesSELECT division_id FROM division ORDER BY division_id;
• SELECT– FOR UPDATE
• Filtering– WHERE
• ORDER BYORDER BY– often ignored– query complexity
• GROUP BY
![Page 5: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/5.jpg)
• SELECT– FOR UPDATE
• Filtering– WHERE
• ORDER BYORDER BY– often ignored– query complexity
• GROUP BY
The Basics of Efficient SQLThe Basics of Efficient SQLResorts on result after WHERE and GROUP BY
Don’t repeat sorting (ORDER BY often ignored)
by SELECTSELECT division_id FROM division ORDER BY division_id;
by WHERESELECT * FROM division WHERE division_id < 10 ORDER BY division_id;
GROUP BYSELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state;by DISTINCTSELECT DISTINCT(state) FROM division ORDER BY state;
by indexesSELECT division_id FROM division ORDER BY division_id;
![Page 6: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/6.jpg)
The Basics of Efficient SQLThe Basics of Efficient SQL
GROUP BYSELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state;
HAVING (filters aggregate)SELECT state, COUNT(state) FROM division GROUP BY state HAVING COUNT(state) > 1;use WHERESELECT state, COUNT(state) FROM division WHERE state = 'NY' GROUP BY state;not HAVINGSELECT state, COUNT(state) FROM division GROUP BY state HAVING state = 'NY';
• SELECT– FOR UPDATE
• Filtering– WHERE
• ORDER BY– often ignored– query complexity
• GROUP BYGROUP BY– Use WHERE not HAVINGUse WHERE not HAVING
![Page 7: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/7.jpg)
• SELECT– FOR UPDATE
• Filtering– WHERE
• ORDER BY– often ignored– query complexity
• GROUP BYGROUP BY– Use WHERE not HAVINGUse WHERE not HAVING
The Basics of Efficient SQLThe Basics of Efficient SQL
GROUP BYSELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state;
HAVING (filters aggregate)SELECT state, COUNT(state) FROM division GROUP BY state HAVING COUNT(state) > 1;use WHERESELECT state, COUNT(state) FROM division WHERE state = 'NY' GROUP BY state;not HAVINGSELECT state, COUNT(state) FROM division GROUP BY state HAVING state = 'NY';
![Page 8: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/8.jpg)
• FunctionsFunctions– conversions– miss indexes
– counteract with function based indexing– avoid using
– DECODE– CASE expressions– set operators (UNION)
• Use sequencesUse sequences• Use equality (=) or range scans (>)Use equality (=) or range scans (>)
– avoid negatives (!=, NOT)– avoid LIKE
The Basics of Efficient SQLThe Basics of Efficient SQL
![Page 9: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/9.jpg)
The Basics of Efficient SQLThe Basics of Efficient SQL
• JoinsJoins– avoid Cartesian Products– avoid anti joins– avoid outer joins– perhaps replace
– multiple table complex joins– with subquery semi joins and inline views
• Be careful with viewsBe careful with views
![Page 10: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/10.jpg)
Common Sense IndexingCommon Sense Indexing
• Don’t always need indexesDon’t always need indexes– table with few columns– static data– small tables– appended tables (SQL*Loader)
• How to indexHow to index– single column surrogate sequences– don’t override PK and FKs– avoid nullable columns
![Page 11: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/11.jpg)
Common Sense IndexingCommon Sense Indexing
• Read write indexingRead write indexing– BTree
• Often read onlyOften read only– Bitmaps– IOTs– Clusters
![Page 12: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/12.jpg)
Common Sense IndexingCommon Sense Indexing
• Read write indexingRead write indexing– BTree
– function based– can help a lot– get out of control
– everybody wants one– reverse key
– surrogate keys– High insertion rates
– not DW– Oracle RAC
![Page 13: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/13.jpg)
Common Sense IndexingCommon Sense Indexing
• Often read onlyOften read only– Bitmaps
– can be much faster than BTrees – twice as fast in my book– at a previous client
– 1 year of DML activity– 100s of times slower– problem was nobody knew why– and nobody wanted to change anything
![Page 14: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/14.jpg)
Common Sense IndexingCommon Sense Indexing
• Often read onlyOften read only– IOTs
– small number of columns– small tables– heard good things in Oracle RAC– even highly active DML environments
![Page 15: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/15.jpg)
• Is intelligent– better with simple queries
• Is usually correct• Nothing is set in stone• Verify SQL code efficiency
– use EXPLAIN PLANEXPLAIN PLAN– SET AUTOTRACE ON EXPLAINSET AUTOTRACE ON EXPLAIN– $ORACLE_HOME/rdbms/admin/utlxplan.sql$ORACLE_HOME/rdbms/admin/utlxplan.sql
The OptimizerThe Optimizer
![Page 16: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/16.jpg)
• Everything cost based– rule based is redundant
• Maintain statistics• Dynamic sampling
– OPTIMIZER_DYNAMIC_SAMPLING• Set TIMED_STATISTICS • Histograms
– maintain as for statistics– use for unevenly distributed indexes
The OptimizerThe Optimizer
![Page 17: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/17.jpg)
• TablesTables– full Table scans
– small static tables– reading most of the rows
– over 10% for the Optimizer– reading deleted rows– parallel table scans
– sample table scans– SELECT * FROM generalledger SAMPLE(0.001);
– ROWID scans
The OptimizerThe Optimizer
![Page 18: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/18.jpg)
• IndexesIndexes– index unique scan– index range scan
– reverse order index range scan
– index skip scan– index full scan
– fast full index scan
– others (very specific)
The OptimizerThe Optimizer
![Page 19: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/19.jpg)
• JoinsJoins– nested loop join
– most efficient– row sets both small– one large and one small row set– one sorted
– hash join– both large with little difference– temporary hash table generated
The OptimizerThe Optimizer
![Page 20: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/20.jpg)
• More on joinsMore on joins– sort merge join
– inefficient– both rows sets sorted then merge sorted
– other join types– semi joins– bitmap joins– star queries – Cartesian joins– outer joins (nested, hash or sort merge)
The OptimizerThe Optimizer
![Page 21: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/21.jpg)
• Hints can change thingsHints can change things– influence the optimizer
• CURSOR_SHARING• FIRST or ALL_ROWS• DYNAMIC_SAMPLING
– change table scans• FULL
The OptimizerThe Optimizer
![Page 22: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/22.jpg)
The OptimizerThe Optimizer• More on hintsMore on hints
– change index scans• INDEX_ASC, INDEX_DESC• INDEX_FFS• INDEX_JOIN (join indexes)• INDEX_SS_ASC or INDEX_SS_DESC• NO_INDEX, NO_INDEX_FFS or NO_INDEX_SS
– change joins• can change join type and influence with
parameters
– parallel SQL
![Page 23: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/23.jpg)
Finding Problem QueriesFinding Problem Queries
• EXPLAIN PLANEXPLAIN PLAN– SET AUTOTRACE ON EXPLAIN– $ORACLE_HOME/rdbms/admin/utlxplan.sql$ORACLE_HOME/rdbms/admin/utlxplan.sql
• SQL Trace and TKPROF
![Page 24: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/24.jpg)
Wait Event Wait Event InterfaceInterface
Finding Problem QueriesFinding Problem Queries• Performance views
• V$SQLAREA• executions• parsing• sorting• disk and buffer reads• fetching
• V$SQL• optimizer cost• CPU time• elapsed time
![Page 25: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/25.jpg)
Wait Event InterfaceWait Event Interface
![Page 26: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/26.jpg)
Wait Event InterfaceWait Event Interface
![Page 27: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/27.jpg)
Wait Event InterfaceWait Event Interface
![Page 28: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/28.jpg)
Wait Event InterfaceWait Event Interface
![Page 29: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/29.jpg)
Wait Event InterfaceWait Event Interface
![Page 30: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/30.jpg)
Wait Event InterfaceWait Event Interface
![Page 31: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/31.jpg)
Wait Event InterfaceWait Event Interface
![Page 32: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/32.jpg)
Performance overview
![Page 33: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/33.jpg)
Database health overview
![Page 34: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/34.jpg)
Drilldown
![Page 35: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/35.jpg)
More drilldown
![Page 36: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/36.jpg)
TopSQL
![Page 37: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/37.jpg)
EXPLAIN PLAN
![Page 38: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/38.jpg)
Wait Event InterfaceWait Event Interface
![Page 39: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/39.jpg)
Wait Event InterfaceWait Event Interface
![Page 40: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/40.jpg)
Wait Event InterfaceWait Event Interface
![Page 41: Tuning Oracle SQL - sfoug.org · The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id](https://reader036.vdocuments.site/reader036/viewer/2022090606/605c1e96ec70962c4e35ca16/html5/thumbnails/41.jpg)
Use the help filesUse the HELP FILES in Use the HELP FILES in
Oracle Enterprise Oracle Enterprise ManagerManager