Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Basics of Efficient SQL Written for myself –Writing doesnt make you rich Proof of what works –and what doesnt Three parts: –Data Model Tuning –SQL.

Similar presentations


Presentation on theme: "The Basics of Efficient SQL Written for myself –Writing doesnt make you rich Proof of what works –and what doesnt Three parts: –Data Model Tuning –SQL."— Presentation transcript:

1 The Basics of Efficient SQL Written for myself –Writing doesnt make you rich Proof of what works –and what doesnt Three parts: –Data Model Tuning –SQL Code Tuning –Phys. / Config. Tuning Written for myself –Writing doesnt make you rich Proof of what works –and what doesnt Three parts: –Data Model Tuning –SQL Code Tuning –Phys. / Config. Tuning

2 The Basics of Efficient SQL SQL Code TuningSQL Code Tuning –What is SQL? –The Basics of Efficient SQL –Common Sense Indexing –The Optimizer: Making SQL Efficient –Finding Problem Queries –Oracle Enterprise Manager SQL Code TuningSQL Code Tuning –What is SQL? –The Basics of Efficient SQL –Common Sense Indexing –The Optimizer: Making SQL Efficient –Finding Problem Queries –Oracle Enterprise Manager

3 Some General Guidelines KISS (Keep It Simple Stupid)KISS (Keep It Simple Stupid) –easier for the Optimizer depends on query complexity –easier for you to tune Dont go too far with granularityDont go too far with granularity –connecting and disconnecting for all queries KISS (Keep It Simple Stupid)KISS (Keep It Simple Stupid) –easier for the Optimizer depends on query complexity –easier for you to tune Dont go too far with granularityDont go too far with granularity –connecting and disconnecting for all queries

4 KISS –easier for the Optimizer depends on query complexity –easier for you to tune Dont go too far with granularity –connecting and disconnecting for all queries KISS –easier for the Optimizer depends on query complexity –easier for you to tune Dont go too far with granularity –connecting and disconnecting for all queries Watch Granularity

5 Is intelligent –better with simple queries Is usually correct Nothing is set in stone Verify SQL code efficiency EXPLAIN PLAN –use EXPLAIN PLAN –SET AUTOTRACE ON EXPLAIN –$ORACLE_HOME/rdbms/admin/utlxplan.sql Is intelligent –better with simple queries Is usually correct Nothing is set in stone Verify SQL code efficiency EXPLAIN PLAN –use EXPLAIN PLAN –SET AUTOTRACE ON EXPLAIN –$ORACLE_HOME/rdbms/admin/utlxplan.sql The Optimizer

6 Basic Query Components SELECTSELECT –get data WHERE –filtering ORDER BY –sorting GROUP BY –aggregating SELECTSELECT –get data WHERE –filtering ORDER BY –sorting GROUP BY –aggregating

7 Basic Query Components SELECT –get data WHEREWHERE –filtering ORDER BY –sorting GROUP BY –aggregating SELECT –get data WHEREWHERE –filtering ORDER BY –sorting GROUP BY –aggregating

8 Basic Query Components SELECT –get data WHERE –filtering ORDER BYORDER BY –sorting GROUP BY –aggregating SELECT –get data WHERE –filtering ORDER BYORDER BY –sorting GROUP BY –aggregating

9 Basic Query Components SELECT –get data WHERE –filtering ORDER BY –sorting GROUP BYGROUP BY –aggregating SELECT –get data WHERE –filtering ORDER BY –sorting GROUP BYGROUP BY –aggregating

10 SELECTSELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BY SELECTSELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BY Queries * SELECT * FROM division; SELECT division_id, name, city, state, country FROM division; SELECT division_id FROM division;

11 SELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BY SELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BY Queries Avoid 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 Avoid 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

12 Queries Resorts on result after WHERE and GROUP BY Dont repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id FROM division ORDER BY division_id; by WHERE SELECT * FROM division WHERE division_id < 10 ORDER BY division_id; GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; by DISTINCT SELECT DISTINCT(state) FROM division ORDER BY state; by indexes SELECT division_id FROM division ORDER BY division_id; Resorts on result after WHERE and GROUP BY Dont repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id FROM division ORDER BY division_id; by WHERE SELECT * FROM division WHERE division_id < 10 ORDER BY division_id; GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; by DISTINCT SELECT DISTINCT(state) FROM division ORDER BY state; by indexes SELECT division_id FROM division ORDER BY division_id; SELECT –FOR UPDATE Filtering –WHERE ORDER BYORDER BY –often ignored –query complexity GROUP BY SELECT –FOR UPDATE Filtering –WHERE ORDER BYORDER BY –often ignored –query complexity GROUP BY

13 SELECT –FOR UPDATE Filtering –WHERE ORDER BYORDER BY –often ignored –query complexity GROUP BY SELECT –FOR UPDATE Filtering –WHERE ORDER BYORDER BY –often ignored –query complexity GROUP BY Queries Resorts on result after WHERE and GROUP BY Dont repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id FROM division ORDER BY division_id; by WHERE SELECT * FROM division WHERE division_id < 10 ORDER BY division_id; GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; by DISTINCT SELECT DISTINCT(state) FROM division ORDER BY state; by indexes SELECT division_id FROM division ORDER BY division_id; Resorts on result after WHERE and GROUP BY Dont repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id FROM division ORDER BY division_id; by WHERE SELECT * FROM division WHERE division_id < 10 ORDER BY division_id; GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; by DISTINCT SELECT DISTINCT(state) FROM division ORDER BY state; by indexes SELECT division_id FROM division ORDER BY division_id;

14 Queries GROUP BY SELECT 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 WHERE SELECT state, COUNT(state) FROM division WHERE state = 'NY' GROUP BY state; not HAVING SELECT state, COUNT(state) FROM division GROUP BY state HAVING state = 'NY'; GROUP BY SELECT 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 WHERE SELECT state, COUNT(state) FROM division WHERE state = 'NY' GROUP BY state; not HAVING SELECT 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 HAVING SELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BYGROUP BY –Use WHERE not HAVING

15 SELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BYGROUP BY –Use WHERE not HAVING SELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BYGROUP BY –Use WHERE not HAVING Queries GROUP BY SELECT 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 WHERE SELECT state, COUNT(state) FROM division WHERE state = 'NY' GROUP BY state; not HAVING SELECT state, COUNT(state) FROM division GROUP BY state HAVING state = 'NY'; GROUP BY SELECT 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 WHERE SELECT state, COUNT(state) FROM division WHERE state = 'NY' GROUP BY state; not HAVING SELECT state, COUNT(state) FROM division GROUP BY state HAVING state = 'NY';

16 FunctionsFunctions –conversions –avoid indexes –function based indexing –DECODE –CASE expressions –set operators (UNION) Use sequencesUse sequences Use equality (=) or range scans (>)Use equality (=) or range scans (>) –Avoid negatives (!=, NOT) –Avoid LIKE FunctionsFunctions –conversions –avoid indexes –function based indexing –DECODE –CASE expressions –set operators (UNION) Use sequencesUse sequences Use equality (=) or range scans (>)Use equality (=) or range scans (>) –Avoid negatives (!=, NOT) –Avoid LIKE Other Stuff

17 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 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


Download ppt "The Basics of Efficient SQL Written for myself –Writing doesnt make you rich Proof of what works –and what doesnt Three parts: –Data Model Tuning –SQL."

Similar presentations


Ads by Google