The Basics of Efficient SQL Written for myself –Writing doesnt make you rich Proof of what works...

17
The Basics of The Basics of Efficient SQL Efficient SQL Written for myself Writing doesn’t make you rich Proof of what works and what doesn’t Three parts: Data Model Tuning SQL Code Tuning SQL Code Tuning Phys. / Config. Tuning

Transcript of The Basics of Efficient SQL Written for myself –Writing doesnt make you rich Proof of what works...

Page 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.

The Basics of Efficient SQLThe Basics of Efficient SQL

• Written for myself– Writing doesn’t make you rich

• Proof of what works– and what doesn’t

• Three parts:– Data Model Tuning

– SQL Code TuningSQL Code Tuning– Phys. / Config. Tuning

• Written for myself– Writing doesn’t make you rich

• Proof of what works– and what doesn’t

• Three parts:– Data Model Tuning

– SQL Code TuningSQL Code Tuning– Phys. / Config. Tuning

Page 2: 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.

The Basics of Efficient SQLThe Basics of Efficient SQL

• SQL Code TuningSQL Code Tuning– What is SQL?– The Basics of Efficient SQLThe 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 SQLThe Basics of Efficient SQL– Common Sense Indexing– The Optimizer: Making SQL Efficient– Finding Problem Queries– Oracle Enterprise Manager

Page 3: 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.

Some General GuidelinesSome General Guidelines

• ““KISS” KISS” (Keep It Simple Stupid)(Keep It Simple Stupid)– easier for the Optimizer

• depends on query complexity

– easier for you to tune

• Don’t go too far with granularityDon’t go too far with granularity– connecting and disconnecting for all queries

• ““KISS” KISS” (Keep It Simple Stupid)(Keep It Simple Stupid)– easier for the Optimizer

• depends on query complexity

– easier for you to tune

• Don’t go too far with granularityDon’t go too far with granularity– connecting and disconnecting for all queries

Page 4: 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.

• “KISS”– easier for the Optimizer

• depends on query complexity

– easier for you to tune

• Don’t go too far with granularity– connecting and disconnecting for all queriesconnecting and disconnecting for all queries

• “KISS”– easier for the Optimizer

• depends on query complexity

– easier for you to tune

• Don’t go too far with granularity– connecting and disconnecting for all queriesconnecting and disconnecting for all queries

Watch GranularityWatch Granularity

Page 5: 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.

• 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

• 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 6: 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.

Basic Query ComponentsBasic Query Components

• SELECTSELECT– get dataget data

• WHERE– filtering

• ORDER BY– sorting

• GROUP BY– aggregating

• SELECTSELECT– get dataget data

• WHERE– filtering

• ORDER BY– sorting

• GROUP BY– aggregating

Page 7: 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.

Basic Query ComponentsBasic Query Components

• SELECT– get data

• WHEREWHERE– filteringfiltering

• ORDER BY– sorting

• GROUP BY– aggregating

• SELECT– get data

• WHEREWHERE– filteringfiltering

• ORDER BY– sorting

• GROUP BY– aggregating

Page 8: 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.

Basic Query ComponentsBasic Query Components

• SELECT– get data

• WHERE– filtering

• ORDER BYORDER BY– sortingsorting

• GROUP BY– aggregating

• SELECT– get data

• WHERE– filtering

• ORDER BYORDER BY– sortingsorting

• GROUP BY– aggregating

Page 9: 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.

Basic Query ComponentsBasic Query Components

• SELECT– get data

• WHERE– filtering

• ORDER BY– sorting

• GROUP BYGROUP BY– aggregatingaggregating

• SELECT– get data

• WHERE– filtering

• ORDER BY– sorting

• GROUP BYGROUP BY– aggregatingaggregating

Page 10: 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.

• SELECTSELECT– FOR UPDATEFOR UPDATE

• Filtering– WHERE

• ORDER BY– often ignored– query complexity

• GROUP BY

• SELECTSELECT– FOR UPDATEFOR UPDATE

• Filtering– WHERE

• ORDER BY– often ignored– query complexity

• GROUP BY

QueriesQueries

SELECT ** FROM division;

SELECT division_id, name, city, state, country FROM division;

SELECT division_id FROM division;

Page 11: 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.

• SELECT– FOR UPDATE

• Filtering– WHEREWHERE

• ORDER BY– often ignored– query complexity

• GROUP BY

• SELECT– FOR UPDATE

• Filtering– WHEREWHERE

• ORDER BY– often ignored– query complexity

• GROUP BY

QueriesQueries

Avoid unintentional full table scansSELECT * 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 scansSELECT * 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 12: 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.

QueriesQueriesResorts on result after WHERE and GROUP BY

Don’t 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

Don’t 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

Page 13: 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.

• 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

QueriesQueriesResorts on result after WHERE and GROUP BY

Don’t 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

Don’t 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;

Page 14: 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.

QueriesQueries

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 HAVINGUse WHERE not HAVING

• SELECT– FOR UPDATE

• Filtering– WHERE

• ORDER BY– often ignored– query complexity

• GROUP BYGROUP BY– Use WHERE not HAVINGUse WHERE not HAVING

Page 15: 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.

• SELECT– FOR UPDATE

• Filtering– WHERE

• ORDER BY– often ignored– query complexity

• GROUP BYGROUP BY– Use WHERE not HAVINGUse WHERE not HAVING

• SELECT– FOR UPDATE

• Filtering– WHERE

• ORDER BY– often ignored– query complexity

• GROUP BYGROUP BY– Use WHERE not HAVINGUse WHERE not HAVING

QueriesQueries

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';

Page 16: 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.

• 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 StuffOther Stuff

Page 17: 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.

Other StuffOther Stuff

• 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