SlideShare a Scribd company logo
You’re Smarter than a
Database
Overcoming the optimizer’s bad
cardinality estimates
About me
 Bobby Durrett
 US Foodservice
 bobbyandmarielle@yahoo.com
 Scripts in
http://www.geocities.com/bobbyandma
rielle/sqltuning.zip
What you know
What the database knows
Before SQL
 Example - mainframe Datacom/DB COBOL
 List index names
 Write loops
read a from one index i1 where one.c=10
while more table one rows exist get next row
read b from two index i2 where two.a = one.a
while more table two rows exist get next row
print one.a,two.b
end while
end while
SQL
 Tell what you want, not how to get it
select one.a,two.b
from
one,two
where
one.c=10 and one.a=two.a;
Pre-SQL versus SQL
 Pre-SQL code very efficient – runs in
megabytes – VSE mainframe COBOL
 Labor intensive
 SQL can be inefficient – runs in
gigabytes (if you are lucky!)
 Much more productive – do in minutes
what took hours before – create tables
What the database doesn’t
know
 Optimizer has a limited set of statistics
that describe the data
 It can miscalculate the number of rows
a query will return, its cardinality
 A cardinality error can lead optimizer to
choose a slow way to run the SQL
Example plan/Cardinality
-------------------------------------------------
| Id | Operation | Name | Rows | Cost
-------------------------------------------------
| 0 | SELECT STATEMENT | | 10 | 3
|* 1 | TABLE ACCESS FULL| TEST1 | 10 | 3
-------------------------------------------------
 Plan = how Oracle will run your query
 Rows = how many rows optimizer thinks that
step will return
 Cost = estimate of time query will take, a
function of the number of rows
How to fix cardinality
problems
 Find out if it really is a cardinality issue
 Determine the reason it occurred
 Single column
 Multiple columns
 Choose a strategy
 Give the optimizer more information
 Override optimizer decision
 Change the application
Four examples
 Four examples of how the optimizer
calculates cardinality
 Full scripts and their outputs on portal,
pieces on slides – edited for simplicity
Step 1: Find out if it really is a
cardinality issue
 Example 1
 Data
select a,count(*) from test1 group by a;
A COUNT(*)
---------- ----------
1 10
 Query
select * from test1 where a=1;
Step 1: Find out if it really is a
cardinality issue
 Get estimated cardinality from plan
-------------------------------------------
| Id | Operation | Name | Rows |
-------------------------------------------
| 0 | SELECT STATEMENT | | 10 |
|* 1 | TABLE ACCESS FULL| TEST1 | 10 |
-------------------------------------------
 Do query for actual number of rows
select count(*) from test1 where a=1;
Step 1: Find out if it really is a
cardinality issue
 Plan is a tree – find cardinality and select
count(*) on part of query represented by that
part of plan. join
table join
table table
Step 2: Understand the reason
for the wrong cardinality
 Unequal distribution of data:
 Within a single column
 Last name
 “Smith” or “Jones”
 Among multiple columns –
 Address
 Zipcode and State
Step 2: Understand the reason
for the wrong cardinality
 Example 2 - Unequal distribution of values in a
single column
 1,000,000 rows with value 1
 1 row with value 2
select a,count(*) from TEST2 group by a;
A COUNT(*)
---------- ----------
1 1000000
2 1
Step 2: Understand the reason
for the wrong cardinality
 SQL statement – returns one row
select * from TEST2 where a=2;
Step 2: Understand the reason
for the wrong cardinality
 Plan with wrong number of rows = 500,000
 Full scan instead of range scan – 100 times slower
---------------------------------------------
| Operation | Name | Rows |
---------------------------------------------
| SELECT STATEMENT | | 500K|
| INDEX FAST FULL SCAN| TEST2INDEX | 500K|
---------------------------------------------
Step 2: Understand the reason
for the wrong cardinality
 Column statistics – two distinct values
LOW HIGH NUM_DISTINCT
---------- ---------- ------------
1 2 2
 Table statistic – total # of rows – 1,000,001
NUM_ROWS
----------
1000001
Step 2: Understand the reason
for the wrong cardinality
 Rows in plan = (rows in table)/
(distinct values of column)
 500000=1000001/2
 Optimizer knew that there were only
two values – assumed they had equal
number of rows
Step 2: Understand the reason
for the wrong cardinality
 Example 3 - Combinations of column values
not equally distributed
 1,000,000 rows with values 1,1
 1,000,000 rows with values 2,2
 1 row with value 1,2
 ~ Equal numbers of 1’s and 2’s in each column
A B COUNT(*)
---------- ---------- ----------
1 1 1000000
1 2 1
2 2 1000000
Step 2: Understand the reason
for the wrong cardinality
 SQL statement – retrieves one row
select sum(a+b)
from TEST3
where
a=1 and b=2;
Step 2: Understand the reason
for the wrong cardinality
 Plan with wrong number of rows = 500,000
 Inefficient full scan
----------------------------------------------
| Operation | Name | Rows |
----------------------------------------------
| SELECT STATEMENT | | 1 |
| SORT AGGREGATE | | 1 |
| INDEX FAST FULL SCAN| TEST3INDEX | 500K|
----------------------------------------------
Step 2: Understand the reason
for the wrong cardinality
 Column statistics
C LOW HIGH NUM_DISTINCT
- ---------- ---------- ------------
A 1 2 2
B 1 2 2
 Table statistic – total # of rows – 2,000,001
NUM_ROWS
----------
2000001
Step 2: Understand the reason
for the wrong cardinality
 Rows in plan = (rows in table)/
(distinct values A * distinct values B)
 500000=2000001/(2 * 2)
 Optimizer assumes all four
combinations (1,1),(1,2),(2,1),(2,2)
equally likely
Step 2: Understand the reason
for the wrong cardinality
 How to tell which assumption is in play?
 Select count(*) each column
select a,count(*) from TEST3 group by a;
select b,count(*) from TEST3 group by b;
 count(*) each column combination
select a,b,count(*) from TEST3
group by a,b;
Step 3: Choose the best strategy
for fixing the cardinality problem
 Giving the optimizer more information
 Histograms
 SQL Profiles
 Overriding optimizer decisions
 Hints
 Changing the application
Try to use optimizer as much as possible to
minimize development work
Step 3: Choose the best strategy
for fixing the cardinality problem
 Giving the optimizer more information –
using histograms
 Works for unequal distribution within a
single column
 A histogram records the distribution of
values within a column in up to 254
“buckets”
 Works best on columns with fewer than
255 distinct values
Step 3: Choose the best strategy
for fixing the cardinality problem
 Run gather_table_stats command to get
histograms on the column – 254 is max
number of buckets
method_opt=>'FOR ALL COLUMNS SIZE 254'
Step 3: Choose the best strategy
for fixing the cardinality problem
 Plan for Example 2 with correct number of rows
with histogram
 Uses range scan
-----------------------------------------
| Operation | Name | Rows |
-----------------------------------------
| SELECT STATEMENT | | 1 |
| INDEX RANGE SCAN| TEST2INDEX | 1 |
-----------------------------------------
Step 3: Choose the best strategy
for fixing the cardinality problem
 Column statistics – two buckets
LOW HIGH NUM_DISTINCT NUM_BUCKETS
---------- ---------- ------------ -----------
1 2 2 2
 Table statistic – unchanged
NUM_ROWS
----------
1000001
Step 3: Choose the best strategy
for fixing the cardinality problem
 Time without histograms (1 second):
Elapsed: 00:00:01.00
 Time with histograms(1/100th second):
Elapsed: 00:00:00.01
Step 3: Choose the best strategy
for fixing the cardinality problem
 Giving the optimizer more information – using
SQL Profiles
 Works for unequal distribution among multiple
columns
 Includes information about the relationship
between columns in the SQL – correlated columns
or predicates
Step 3: Choose the best strategy
for fixing the cardinality problem
 SQL Tuning Advisor gathers statistics on the
columns
...DBMS_SQLTUNE.CREATE_TUNING_TASK(...
...DBMS_SQLTUNE.EXECUTE_TUNING_TASK(...
 Accept the SQL Profile it creates to use the
new statistics
...DBMS_SQLTUNE.ACCEPT_SQL_PROFILE (...
Step 3: Choose the best strategy
for fixing the cardinality problem
 Example 3 plan with correct number of rows = 1
using SQL profile
--------------------------------------------------
| Operation | Name | Rows | Bytes |
--------------------------------------------------
| SELECT STATEMENT | | 1 | 6 |
| SORT AGGREGATE | | 1 | 6 |
| INDEX RANGE SCAN| TEST3INDEX | 1 | 6 |
-------------------------------------------------|
Step 3: Choose the best strategy
for fixing the cardinality problem
 Time without a profile (1 second):
Elapsed: 00:00:01.09
 Time with a profile(1/100th second):
Elapsed: 00:00:00.01
Step 3: Choose the best strategy
for fixing the cardinality problem
 Overriding optimizer decisions – using hints
 Example 4 has unequal distribution of column
values across two tables – histograms and SQL
Profiles don’t work
 Hint forces index range scan
 Small amount of additional code – not like
Cobol on mainframe
Step 3: Choose the best strategy
for fixing the cardinality problem
 Example 4 - SMALL table
 MANY relates to 1 – there are many rows with
value 1
 FEW relates to 2 – there are few with value 2
insert into SMALL values ('MANY',1);
insert into SMALL values ('FEW',2);
Step 3: Choose the best strategy
for fixing the cardinality problem
 Example 4 - LARGE table:
 1,000,000 rows with value 1
 1 row with value 2
NUM COUNT(*)
---------- ----------
1 1000000
2 1
Step 3: Choose the best strategy
for fixing the cardinality problem
 SQL statement – returns one row
select B.NUM
from SMALL A,LARGE B
where
A.NUM=B.NUM and
A.NAME='FEW';
Step 3: Choose the best strategy
for fixing the cardinality problem
 Plan with wrong number of rows = 125,000
----------------------------------------------
| Operation | Name | Rows |
----------------------------------------------
| SELECT STATEMENT | | 125K|
| HASH JOIN | | 125K|
| TABLE ACCESS FULL | SMALL | 1 |
| INDEX FAST FULL SCAN| LARGEINDEX | 1000K|
----------------------------------------------
Step 3: Choose the best strategy
for fixing the cardinality problem
 Column statistics – two buckets on all
columns – using histograms
LOW HIGH NUM_DISTINCT NUM_BUCKETS
---------- ---------- ------------ -----------
1 2 2 2
LOW HIGH NUM_DISTINCT NUM_BUCKETS
---- ---- ------------ -----------
FEW MANY 2 2
Step 3: Choose the best strategy
for fixing the cardinality problem
 Table statistics – SMALL has 2 rows,
LARGE 1000001
NUM_ROWS
----------
2
NUM_ROWS
----------
1000001
Step 3: Choose the best strategy
for fixing the cardinality problem
 125000=1000001/8
 Optimizer appears to assume all eight
combinations of the three columns’
values are equally likely
 Can’t verify formula – references don’t
include formula with histograms
 Even worse without histograms –
cardinality is 500000
Step 3: Choose the best strategy
for fixing the cardinality problem
 No SQL profile from SQL Tuning Advisor:
There are no recommendations to improve the
statement.
 Neither histograms nor SQL profiles help
example 4
Step 3: Choose the best strategy
for fixing the cardinality problem
 Statement with hints:
 Use index
 Don’t do full scan
select /*+ INDEX(B LARGEINDEX)
NO_INDEX_FFS(B LARGEINDEX) */
B.NUM
from SMALL A,LARGE B
where
a.NUM=B.NUM and
A.NAME='FEW';
Step 3: Choose the best strategy
for fixing the cardinality problem
 Time without a hint (1 second):
Elapsed: 00:00:01.03
 Time with a hint (1/100th second):
Elapsed: 00:00:00.01
Step 3: Choose the best strategy
for fixing the cardinality problem
 Changing the application
 Change your tables so that the optimizer gets your
SQL’s cardinality right
 Requires more work designing tables, but keeps
productivity benefits of SQL
Step 3: Choose the best strategy
for fixing the cardinality problem
 Example 4 – moved NAME column to LARGE table and
split table in two
 One million (‘MANY’,1) rows in LARGEA
 One (‘FEW’,2) row in LARGEB
 Query:
select NUM
from (select * from largea
union
select * from largeb)
where
NAME='FEW';
Step 3: Choose the best strategy
for fixing the cardinality problem
 Plan is just as efficient as with hint:
 Number of rows = 2 (reality is 1)
 Range Scan
--------------------------------------------------------------
| Id | Operation | Name | Rows |
--------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 |
| 1 | VIEW | | 2 |
| 2 | SORT UNIQUE | | 2 |
| 3 | UNION-ALL | | |
| 4 | TABLE ACCESS BY INDEX ROWID| LARGEA | 1 |
|* 5 | INDEX RANGE SCAN | LARGEAINDEX | 1 |
| 6 | TABLE ACCESS BY INDEX ROWID| LARGEB | 1 |
|* 7 | INDEX RANGE SCAN | LARGEBINDEX | 1 |
--------------------------------------------------------------
Step 3: Choose the best strategy
for fixing the cardinality problem
 Time without table change (1 second):
Elapsed: 00:00:01.03
 Time with table change (1/100th second):
Elapsed: 00:00:00.01
Conclusion
 SQL improves productivity, optimizer has limits
 Identify cases where cardinality is wrong
 Understand why the database got it wrong
 One column
 Multiple columns
 Choose best strategy to fix
 Give optimizer more info
 Override optimizer’s choices
 Redesign tables
References
 Cost Based Optimizer Fundamentals, Jonathan Lewis
 Metalink Note:212809.1, Limitations of the Oracle Cost Based
Optimizer
 Metalink Note:68992.1, Predicate Selectivity
 Histograms – Myths and Facts, Wolfgang Breitling
Select Journal, Volume 13, Number 3

More Related Content

Similar to sqltuningcardinality1(1).ppt

Adaptive Query Optimization
Adaptive Query OptimizationAdaptive Query Optimization
Adaptive Query Optimization
Anju Garg
 
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdfNOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
cookie1969
 
query-optimization-techniques_talk.pdf
query-optimization-techniques_talk.pdfquery-optimization-techniques_talk.pdf
query-optimization-techniques_talk.pdf
garos1
 
ANALYZE for executable statements - a new way to do optimizer troubleshooting...
ANALYZE for executable statements - a new way to do optimizer troubleshooting...ANALYZE for executable statements - a new way to do optimizer troubleshooting...
ANALYZE for executable statements - a new way to do optimizer troubleshooting...
Sergey Petrunya
 
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
Hailey_Database_Performance_Made_Easy_through_Graphics.pdfHailey_Database_Performance_Made_Easy_through_Graphics.pdf
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
cookie1969
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
Nirav Shah
 
Big Data Analytics with MariaDB ColumnStore
Big Data Analytics with MariaDB ColumnStoreBig Data Analytics with MariaDB ColumnStore
Big Data Analytics with MariaDB ColumnStore
MariaDB plc
 
Pro PostgreSQL
Pro PostgreSQLPro PostgreSQL
Pro PostgreSQL
Robert Treat
 
MySQL performance tuning
MySQL performance tuningMySQL performance tuning
MySQL performance tuning
Anurag Srivastava
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
Sveta Smirnova
 
Writing efficient sql
Writing efficient sqlWriting efficient sql
Writing efficient sql
j9soto
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cPresentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Ronald Francisco Vargas Quesada
 
A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013
Connor McDonald
 
Advance MySQL Training by Pratyush Majumdar
Advance MySQL Training by Pratyush MajumdarAdvance MySQL Training by Pratyush Majumdar
Advance MySQL Training by Pratyush Majumdar
Pratyush Majumdar
 
Adaptive Query Optimization in 12c
Adaptive Query Optimization in 12cAdaptive Query Optimization in 12c
Adaptive Query Optimization in 12c
Anju Garg
 
Shaping Optimizer's Search Space
Shaping Optimizer's Search SpaceShaping Optimizer's Search Space
Shaping Optimizer's Search Space
Gerger
 
Query optimizer vivek sharma
Query optimizer vivek sharmaQuery optimizer vivek sharma
Query optimizer vivek sharma
aioughydchapter
 
12c SQL Plan Directives
12c SQL Plan Directives12c SQL Plan Directives
12c SQL Plan Directives
Franck Pachot
 
Dbms plan - A swiss army knife for performance engineers
Dbms plan - A swiss army knife for performance engineersDbms plan - A swiss army knife for performance engineers
Dbms plan - A swiss army knife for performance engineers
Riyaj Shamsudeen
 
Explaining the Postgres Query Optimizer
Explaining the Postgres Query OptimizerExplaining the Postgres Query Optimizer
Explaining the Postgres Query Optimizer
EDB
 

Similar to sqltuningcardinality1(1).ppt (20)

Adaptive Query Optimization
Adaptive Query OptimizationAdaptive Query Optimization
Adaptive Query Optimization
 
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdfNOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
 
query-optimization-techniques_talk.pdf
query-optimization-techniques_talk.pdfquery-optimization-techniques_talk.pdf
query-optimization-techniques_talk.pdf
 
ANALYZE for executable statements - a new way to do optimizer troubleshooting...
ANALYZE for executable statements - a new way to do optimizer troubleshooting...ANALYZE for executable statements - a new way to do optimizer troubleshooting...
ANALYZE for executable statements - a new way to do optimizer troubleshooting...
 
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
Hailey_Database_Performance_Made_Easy_through_Graphics.pdfHailey_Database_Performance_Made_Easy_through_Graphics.pdf
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Big Data Analytics with MariaDB ColumnStore
Big Data Analytics with MariaDB ColumnStoreBig Data Analytics with MariaDB ColumnStore
Big Data Analytics with MariaDB ColumnStore
 
Pro PostgreSQL
Pro PostgreSQLPro PostgreSQL
Pro PostgreSQL
 
MySQL performance tuning
MySQL performance tuningMySQL performance tuning
MySQL performance tuning
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
 
Writing efficient sql
Writing efficient sqlWriting efficient sql
Writing efficient sql
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cPresentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12c
 
A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013
 
Advance MySQL Training by Pratyush Majumdar
Advance MySQL Training by Pratyush MajumdarAdvance MySQL Training by Pratyush Majumdar
Advance MySQL Training by Pratyush Majumdar
 
Adaptive Query Optimization in 12c
Adaptive Query Optimization in 12cAdaptive Query Optimization in 12c
Adaptive Query Optimization in 12c
 
Shaping Optimizer's Search Space
Shaping Optimizer's Search SpaceShaping Optimizer's Search Space
Shaping Optimizer's Search Space
 
Query optimizer vivek sharma
Query optimizer vivek sharmaQuery optimizer vivek sharma
Query optimizer vivek sharma
 
12c SQL Plan Directives
12c SQL Plan Directives12c SQL Plan Directives
12c SQL Plan Directives
 
Dbms plan - A swiss army knife for performance engineers
Dbms plan - A swiss army knife for performance engineersDbms plan - A swiss army knife for performance engineers
Dbms plan - A swiss army knife for performance engineers
 
Explaining the Postgres Query Optimizer
Explaining the Postgres Query OptimizerExplaining the Postgres Query Optimizer
Explaining the Postgres Query Optimizer
 

More from TricantinoLopezPerez

con9578-2088758.pdf
con9578-2088758.pdfcon9578-2088758.pdf
con9578-2088758.pdf
TricantinoLopezPerez
 
linux-memory-explained.pdf
linux-memory-explained.pdflinux-memory-explained.pdf
linux-memory-explained.pdf
TricantinoLopezPerez
 
sqltuning101-170419021007-2.pdf
sqltuning101-170419021007-2.pdfsqltuning101-170419021007-2.pdf
sqltuning101-170419021007-2.pdf
TricantinoLopezPerez
 
Oracle Wait Events That Everyone Should Know.ppt
Oracle Wait Events That Everyone Should Know.pptOracle Wait Events That Everyone Should Know.ppt
Oracle Wait Events That Everyone Should Know.ppt
TricantinoLopezPerez
 
pdfslide.net_em12c-capacity-planning-with-oem-metrics_2.pdf
pdfslide.net_em12c-capacity-planning-with-oem-metrics_2.pdfpdfslide.net_em12c-capacity-planning-with-oem-metrics_2.pdf
pdfslide.net_em12c-capacity-planning-with-oem-metrics_2.pdf
TricantinoLopezPerez
 
exadata-database-machine-kpis-3224944.pdf
exadata-database-machine-kpis-3224944.pdfexadata-database-machine-kpis-3224944.pdf
exadata-database-machine-kpis-3224944.pdf
TricantinoLopezPerez
 
TA110_System_Capacity_Plan.doc
TA110_System_Capacity_Plan.docTA110_System_Capacity_Plan.doc
TA110_System_Capacity_Plan.doc
TricantinoLopezPerez
 
kscope2013vst-130627142814-phpapp02.pdf
kscope2013vst-130627142814-phpapp02.pdfkscope2013vst-130627142814-phpapp02.pdf
kscope2013vst-130627142814-phpapp02.pdf
TricantinoLopezPerez
 
TGorman Collab16 UnixTools 20160411.pdf
TGorman Collab16 UnixTools 20160411.pdfTGorman Collab16 UnixTools 20160411.pdf
TGorman Collab16 UnixTools 20160411.pdf
TricantinoLopezPerez
 
BrownbagIntrotosqltuning.ppt
BrownbagIntrotosqltuning.pptBrownbagIntrotosqltuning.ppt
BrownbagIntrotosqltuning.ppt
TricantinoLopezPerez
 
ash-1-130820122404-phpapp01.pdf
ash-1-130820122404-phpapp01.pdfash-1-130820122404-phpapp01.pdf
ash-1-130820122404-phpapp01.pdf
TricantinoLopezPerez
 

More from TricantinoLopezPerez (11)

con9578-2088758.pdf
con9578-2088758.pdfcon9578-2088758.pdf
con9578-2088758.pdf
 
linux-memory-explained.pdf
linux-memory-explained.pdflinux-memory-explained.pdf
linux-memory-explained.pdf
 
sqltuning101-170419021007-2.pdf
sqltuning101-170419021007-2.pdfsqltuning101-170419021007-2.pdf
sqltuning101-170419021007-2.pdf
 
Oracle Wait Events That Everyone Should Know.ppt
Oracle Wait Events That Everyone Should Know.pptOracle Wait Events That Everyone Should Know.ppt
Oracle Wait Events That Everyone Should Know.ppt
 
pdfslide.net_em12c-capacity-planning-with-oem-metrics_2.pdf
pdfslide.net_em12c-capacity-planning-with-oem-metrics_2.pdfpdfslide.net_em12c-capacity-planning-with-oem-metrics_2.pdf
pdfslide.net_em12c-capacity-planning-with-oem-metrics_2.pdf
 
exadata-database-machine-kpis-3224944.pdf
exadata-database-machine-kpis-3224944.pdfexadata-database-machine-kpis-3224944.pdf
exadata-database-machine-kpis-3224944.pdf
 
TA110_System_Capacity_Plan.doc
TA110_System_Capacity_Plan.docTA110_System_Capacity_Plan.doc
TA110_System_Capacity_Plan.doc
 
kscope2013vst-130627142814-phpapp02.pdf
kscope2013vst-130627142814-phpapp02.pdfkscope2013vst-130627142814-phpapp02.pdf
kscope2013vst-130627142814-phpapp02.pdf
 
TGorman Collab16 UnixTools 20160411.pdf
TGorman Collab16 UnixTools 20160411.pdfTGorman Collab16 UnixTools 20160411.pdf
TGorman Collab16 UnixTools 20160411.pdf
 
BrownbagIntrotosqltuning.ppt
BrownbagIntrotosqltuning.pptBrownbagIntrotosqltuning.ppt
BrownbagIntrotosqltuning.ppt
 
ash-1-130820122404-phpapp01.pdf
ash-1-130820122404-phpapp01.pdfash-1-130820122404-phpapp01.pdf
ash-1-130820122404-phpapp01.pdf
 

Recently uploaded

Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024
Top Forex Brokers Review
 
Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...
Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...
Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
Digital Marketing with a Focus on Sustainability
Digital Marketing with a Focus on SustainabilityDigital Marketing with a Focus on Sustainability
Digital Marketing with a Focus on Sustainability
sssourabhsharma
 
DearbornMusic-KatherineJasperFullSailUni
DearbornMusic-KatherineJasperFullSailUniDearbornMusic-KatherineJasperFullSailUni
DearbornMusic-KatherineJasperFullSailUni
katiejasper96
 
Ellen Burstyn: From Detroit Dreamer to Hollywood Legend | CIO Women Magazine
Ellen Burstyn: From Detroit Dreamer to Hollywood Legend | CIO Women MagazineEllen Burstyn: From Detroit Dreamer to Hollywood Legend | CIO Women Magazine
Ellen Burstyn: From Detroit Dreamer to Hollywood Legend | CIO Women Magazine
CIOWomenMagazine
 
The Most Inspiring Entrepreneurs to Follow in 2024.pdf
The Most Inspiring Entrepreneurs to Follow in 2024.pdfThe Most Inspiring Entrepreneurs to Follow in 2024.pdf
The Most Inspiring Entrepreneurs to Follow in 2024.pdf
thesiliconleaders
 
Cover Story - China's Investment Leader - Dr. Alyce SU
Cover Story - China's Investment Leader - Dr. Alyce SUCover Story - China's Investment Leader - Dr. Alyce SU
Cover Story - China's Investment Leader - Dr. Alyce SU
msthrill
 
TIMES BPO: Business Plan For Startup Industry
TIMES BPO: Business Plan For Startup IndustryTIMES BPO: Business Plan For Startup Industry
TIMES BPO: Business Plan For Startup Industry
timesbpobusiness
 
Zodiac Signs and Food Preferences_ What Your Sign Says About Your Taste
Zodiac Signs and Food Preferences_ What Your Sign Says About Your TasteZodiac Signs and Food Preferences_ What Your Sign Says About Your Taste
Zodiac Signs and Food Preferences_ What Your Sign Says About Your Taste
my Pandit
 
Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...
Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...
Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...
Neil Horowitz
 
Pitch Deck Teardown: Kinnect's $250k Angel deck
Pitch Deck Teardown: Kinnect's $250k Angel deckPitch Deck Teardown: Kinnect's $250k Angel deck
Pitch Deck Teardown: Kinnect's $250k Angel deck
HajeJanKamps
 
Innovative Uses of Revit in Urban Planning and Design
Innovative Uses of Revit in Urban Planning and DesignInnovative Uses of Revit in Urban Planning and Design
Innovative Uses of Revit in Urban Planning and Design
Chandresh Chudasama
 
Presentation by Herman Kienhuis (Curiosity VC) on Investing in AI for ABS Alu...
Presentation by Herman Kienhuis (Curiosity VC) on Investing in AI for ABS Alu...Presentation by Herman Kienhuis (Curiosity VC) on Investing in AI for ABS Alu...
Presentation by Herman Kienhuis (Curiosity VC) on Investing in AI for ABS Alu...
Herman Kienhuis
 
Industrial Tech SW: Category Renewal and Creation
Industrial Tech SW:  Category Renewal and CreationIndustrial Tech SW:  Category Renewal and Creation
Industrial Tech SW: Category Renewal and Creation
Christian Dahlen
 
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
AnnySerafinaLove
 
Best practices for project execution and delivery
Best practices for project execution and deliveryBest practices for project execution and delivery
Best practices for project execution and delivery
CLIVE MINCHIN
 
Business storytelling: key ingredients to a story
Business storytelling: key ingredients to a storyBusiness storytelling: key ingredients to a story
Business storytelling: key ingredients to a story
Alexandra Fulford
 
Call8328958814 satta matka Kalyan result satta guessing
Call8328958814 satta matka Kalyan result satta guessingCall8328958814 satta matka Kalyan result satta guessing
Call8328958814 satta matka Kalyan result satta guessing
➑➌➋➑➒➎➑➑➊➍
 
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Kalyan Satta Matka Guessing Matka Result Main Bazar chart
 
4 Benefits of Partnering with an OnlyFans Agency for Content Creators.pdf
4 Benefits of Partnering with an OnlyFans Agency for Content Creators.pdf4 Benefits of Partnering with an OnlyFans Agency for Content Creators.pdf
4 Benefits of Partnering with an OnlyFans Agency for Content Creators.pdf
onlyfansmanagedau
 

Recently uploaded (20)

Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024
 
Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...
Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...
Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...
 
Digital Marketing with a Focus on Sustainability
Digital Marketing with a Focus on SustainabilityDigital Marketing with a Focus on Sustainability
Digital Marketing with a Focus on Sustainability
 
DearbornMusic-KatherineJasperFullSailUni
DearbornMusic-KatherineJasperFullSailUniDearbornMusic-KatherineJasperFullSailUni
DearbornMusic-KatherineJasperFullSailUni
 
Ellen Burstyn: From Detroit Dreamer to Hollywood Legend | CIO Women Magazine
Ellen Burstyn: From Detroit Dreamer to Hollywood Legend | CIO Women MagazineEllen Burstyn: From Detroit Dreamer to Hollywood Legend | CIO Women Magazine
Ellen Burstyn: From Detroit Dreamer to Hollywood Legend | CIO Women Magazine
 
The Most Inspiring Entrepreneurs to Follow in 2024.pdf
The Most Inspiring Entrepreneurs to Follow in 2024.pdfThe Most Inspiring Entrepreneurs to Follow in 2024.pdf
The Most Inspiring Entrepreneurs to Follow in 2024.pdf
 
Cover Story - China's Investment Leader - Dr. Alyce SU
Cover Story - China's Investment Leader - Dr. Alyce SUCover Story - China's Investment Leader - Dr. Alyce SU
Cover Story - China's Investment Leader - Dr. Alyce SU
 
TIMES BPO: Business Plan For Startup Industry
TIMES BPO: Business Plan For Startup IndustryTIMES BPO: Business Plan For Startup Industry
TIMES BPO: Business Plan For Startup Industry
 
Zodiac Signs and Food Preferences_ What Your Sign Says About Your Taste
Zodiac Signs and Food Preferences_ What Your Sign Says About Your TasteZodiac Signs and Food Preferences_ What Your Sign Says About Your Taste
Zodiac Signs and Food Preferences_ What Your Sign Says About Your Taste
 
Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...
Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...
Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...
 
Pitch Deck Teardown: Kinnect's $250k Angel deck
Pitch Deck Teardown: Kinnect's $250k Angel deckPitch Deck Teardown: Kinnect's $250k Angel deck
Pitch Deck Teardown: Kinnect's $250k Angel deck
 
Innovative Uses of Revit in Urban Planning and Design
Innovative Uses of Revit in Urban Planning and DesignInnovative Uses of Revit in Urban Planning and Design
Innovative Uses of Revit in Urban Planning and Design
 
Presentation by Herman Kienhuis (Curiosity VC) on Investing in AI for ABS Alu...
Presentation by Herman Kienhuis (Curiosity VC) on Investing in AI for ABS Alu...Presentation by Herman Kienhuis (Curiosity VC) on Investing in AI for ABS Alu...
Presentation by Herman Kienhuis (Curiosity VC) on Investing in AI for ABS Alu...
 
Industrial Tech SW: Category Renewal and Creation
Industrial Tech SW:  Category Renewal and CreationIndustrial Tech SW:  Category Renewal and Creation
Industrial Tech SW: Category Renewal and Creation
 
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
 
Best practices for project execution and delivery
Best practices for project execution and deliveryBest practices for project execution and delivery
Best practices for project execution and delivery
 
Business storytelling: key ingredients to a story
Business storytelling: key ingredients to a storyBusiness storytelling: key ingredients to a story
Business storytelling: key ingredients to a story
 
Call8328958814 satta matka Kalyan result satta guessing
Call8328958814 satta matka Kalyan result satta guessingCall8328958814 satta matka Kalyan result satta guessing
Call8328958814 satta matka Kalyan result satta guessing
 
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...
 
4 Benefits of Partnering with an OnlyFans Agency for Content Creators.pdf
4 Benefits of Partnering with an OnlyFans Agency for Content Creators.pdf4 Benefits of Partnering with an OnlyFans Agency for Content Creators.pdf
4 Benefits of Partnering with an OnlyFans Agency for Content Creators.pdf
 

sqltuningcardinality1(1).ppt

  • 1. You’re Smarter than a Database Overcoming the optimizer’s bad cardinality estimates
  • 2. About me  Bobby Durrett  US Foodservice  bobbyandmarielle@yahoo.com  Scripts in http://www.geocities.com/bobbyandma rielle/sqltuning.zip
  • 5. Before SQL  Example - mainframe Datacom/DB COBOL  List index names  Write loops read a from one index i1 where one.c=10 while more table one rows exist get next row read b from two index i2 where two.a = one.a while more table two rows exist get next row print one.a,two.b end while end while
  • 6. SQL  Tell what you want, not how to get it select one.a,two.b from one,two where one.c=10 and one.a=two.a;
  • 7. Pre-SQL versus SQL  Pre-SQL code very efficient – runs in megabytes – VSE mainframe COBOL  Labor intensive  SQL can be inefficient – runs in gigabytes (if you are lucky!)  Much more productive – do in minutes what took hours before – create tables
  • 8. What the database doesn’t know  Optimizer has a limited set of statistics that describe the data  It can miscalculate the number of rows a query will return, its cardinality  A cardinality error can lead optimizer to choose a slow way to run the SQL
  • 9. Example plan/Cardinality ------------------------------------------------- | Id | Operation | Name | Rows | Cost ------------------------------------------------- | 0 | SELECT STATEMENT | | 10 | 3 |* 1 | TABLE ACCESS FULL| TEST1 | 10 | 3 -------------------------------------------------  Plan = how Oracle will run your query  Rows = how many rows optimizer thinks that step will return  Cost = estimate of time query will take, a function of the number of rows
  • 10. How to fix cardinality problems  Find out if it really is a cardinality issue  Determine the reason it occurred  Single column  Multiple columns  Choose a strategy  Give the optimizer more information  Override optimizer decision  Change the application
  • 11. Four examples  Four examples of how the optimizer calculates cardinality  Full scripts and their outputs on portal, pieces on slides – edited for simplicity
  • 12. Step 1: Find out if it really is a cardinality issue  Example 1  Data select a,count(*) from test1 group by a; A COUNT(*) ---------- ---------- 1 10  Query select * from test1 where a=1;
  • 13. Step 1: Find out if it really is a cardinality issue  Get estimated cardinality from plan ------------------------------------------- | Id | Operation | Name | Rows | ------------------------------------------- | 0 | SELECT STATEMENT | | 10 | |* 1 | TABLE ACCESS FULL| TEST1 | 10 | -------------------------------------------  Do query for actual number of rows select count(*) from test1 where a=1;
  • 14. Step 1: Find out if it really is a cardinality issue  Plan is a tree – find cardinality and select count(*) on part of query represented by that part of plan. join table join table table
  • 15. Step 2: Understand the reason for the wrong cardinality  Unequal distribution of data:  Within a single column  Last name  “Smith” or “Jones”  Among multiple columns –  Address  Zipcode and State
  • 16. Step 2: Understand the reason for the wrong cardinality  Example 2 - Unequal distribution of values in a single column  1,000,000 rows with value 1  1 row with value 2 select a,count(*) from TEST2 group by a; A COUNT(*) ---------- ---------- 1 1000000 2 1
  • 17. Step 2: Understand the reason for the wrong cardinality  SQL statement – returns one row select * from TEST2 where a=2;
  • 18. Step 2: Understand the reason for the wrong cardinality  Plan with wrong number of rows = 500,000  Full scan instead of range scan – 100 times slower --------------------------------------------- | Operation | Name | Rows | --------------------------------------------- | SELECT STATEMENT | | 500K| | INDEX FAST FULL SCAN| TEST2INDEX | 500K| ---------------------------------------------
  • 19. Step 2: Understand the reason for the wrong cardinality  Column statistics – two distinct values LOW HIGH NUM_DISTINCT ---------- ---------- ------------ 1 2 2  Table statistic – total # of rows – 1,000,001 NUM_ROWS ---------- 1000001
  • 20. Step 2: Understand the reason for the wrong cardinality  Rows in plan = (rows in table)/ (distinct values of column)  500000=1000001/2  Optimizer knew that there were only two values – assumed they had equal number of rows
  • 21. Step 2: Understand the reason for the wrong cardinality  Example 3 - Combinations of column values not equally distributed  1,000,000 rows with values 1,1  1,000,000 rows with values 2,2  1 row with value 1,2  ~ Equal numbers of 1’s and 2’s in each column A B COUNT(*) ---------- ---------- ---------- 1 1 1000000 1 2 1 2 2 1000000
  • 22. Step 2: Understand the reason for the wrong cardinality  SQL statement – retrieves one row select sum(a+b) from TEST3 where a=1 and b=2;
  • 23. Step 2: Understand the reason for the wrong cardinality  Plan with wrong number of rows = 500,000  Inefficient full scan ---------------------------------------------- | Operation | Name | Rows | ---------------------------------------------- | SELECT STATEMENT | | 1 | | SORT AGGREGATE | | 1 | | INDEX FAST FULL SCAN| TEST3INDEX | 500K| ----------------------------------------------
  • 24. Step 2: Understand the reason for the wrong cardinality  Column statistics C LOW HIGH NUM_DISTINCT - ---------- ---------- ------------ A 1 2 2 B 1 2 2  Table statistic – total # of rows – 2,000,001 NUM_ROWS ---------- 2000001
  • 25. Step 2: Understand the reason for the wrong cardinality  Rows in plan = (rows in table)/ (distinct values A * distinct values B)  500000=2000001/(2 * 2)  Optimizer assumes all four combinations (1,1),(1,2),(2,1),(2,2) equally likely
  • 26. Step 2: Understand the reason for the wrong cardinality  How to tell which assumption is in play?  Select count(*) each column select a,count(*) from TEST3 group by a; select b,count(*) from TEST3 group by b;  count(*) each column combination select a,b,count(*) from TEST3 group by a,b;
  • 27. Step 3: Choose the best strategy for fixing the cardinality problem  Giving the optimizer more information  Histograms  SQL Profiles  Overriding optimizer decisions  Hints  Changing the application Try to use optimizer as much as possible to minimize development work
  • 28. Step 3: Choose the best strategy for fixing the cardinality problem  Giving the optimizer more information – using histograms  Works for unequal distribution within a single column  A histogram records the distribution of values within a column in up to 254 “buckets”  Works best on columns with fewer than 255 distinct values
  • 29. Step 3: Choose the best strategy for fixing the cardinality problem  Run gather_table_stats command to get histograms on the column – 254 is max number of buckets method_opt=>'FOR ALL COLUMNS SIZE 254'
  • 30. Step 3: Choose the best strategy for fixing the cardinality problem  Plan for Example 2 with correct number of rows with histogram  Uses range scan ----------------------------------------- | Operation | Name | Rows | ----------------------------------------- | SELECT STATEMENT | | 1 | | INDEX RANGE SCAN| TEST2INDEX | 1 | -----------------------------------------
  • 31. Step 3: Choose the best strategy for fixing the cardinality problem  Column statistics – two buckets LOW HIGH NUM_DISTINCT NUM_BUCKETS ---------- ---------- ------------ ----------- 1 2 2 2  Table statistic – unchanged NUM_ROWS ---------- 1000001
  • 32. Step 3: Choose the best strategy for fixing the cardinality problem  Time without histograms (1 second): Elapsed: 00:00:01.00  Time with histograms(1/100th second): Elapsed: 00:00:00.01
  • 33. Step 3: Choose the best strategy for fixing the cardinality problem  Giving the optimizer more information – using SQL Profiles  Works for unequal distribution among multiple columns  Includes information about the relationship between columns in the SQL – correlated columns or predicates
  • 34. Step 3: Choose the best strategy for fixing the cardinality problem  SQL Tuning Advisor gathers statistics on the columns ...DBMS_SQLTUNE.CREATE_TUNING_TASK(... ...DBMS_SQLTUNE.EXECUTE_TUNING_TASK(...  Accept the SQL Profile it creates to use the new statistics ...DBMS_SQLTUNE.ACCEPT_SQL_PROFILE (...
  • 35. Step 3: Choose the best strategy for fixing the cardinality problem  Example 3 plan with correct number of rows = 1 using SQL profile -------------------------------------------------- | Operation | Name | Rows | Bytes | -------------------------------------------------- | SELECT STATEMENT | | 1 | 6 | | SORT AGGREGATE | | 1 | 6 | | INDEX RANGE SCAN| TEST3INDEX | 1 | 6 | -------------------------------------------------|
  • 36. Step 3: Choose the best strategy for fixing the cardinality problem  Time without a profile (1 second): Elapsed: 00:00:01.09  Time with a profile(1/100th second): Elapsed: 00:00:00.01
  • 37. Step 3: Choose the best strategy for fixing the cardinality problem  Overriding optimizer decisions – using hints  Example 4 has unequal distribution of column values across two tables – histograms and SQL Profiles don’t work  Hint forces index range scan  Small amount of additional code – not like Cobol on mainframe
  • 38. Step 3: Choose the best strategy for fixing the cardinality problem  Example 4 - SMALL table  MANY relates to 1 – there are many rows with value 1  FEW relates to 2 – there are few with value 2 insert into SMALL values ('MANY',1); insert into SMALL values ('FEW',2);
  • 39. Step 3: Choose the best strategy for fixing the cardinality problem  Example 4 - LARGE table:  1,000,000 rows with value 1  1 row with value 2 NUM COUNT(*) ---------- ---------- 1 1000000 2 1
  • 40. Step 3: Choose the best strategy for fixing the cardinality problem  SQL statement – returns one row select B.NUM from SMALL A,LARGE B where A.NUM=B.NUM and A.NAME='FEW';
  • 41. Step 3: Choose the best strategy for fixing the cardinality problem  Plan with wrong number of rows = 125,000 ---------------------------------------------- | Operation | Name | Rows | ---------------------------------------------- | SELECT STATEMENT | | 125K| | HASH JOIN | | 125K| | TABLE ACCESS FULL | SMALL | 1 | | INDEX FAST FULL SCAN| LARGEINDEX | 1000K| ----------------------------------------------
  • 42. Step 3: Choose the best strategy for fixing the cardinality problem  Column statistics – two buckets on all columns – using histograms LOW HIGH NUM_DISTINCT NUM_BUCKETS ---------- ---------- ------------ ----------- 1 2 2 2 LOW HIGH NUM_DISTINCT NUM_BUCKETS ---- ---- ------------ ----------- FEW MANY 2 2
  • 43. Step 3: Choose the best strategy for fixing the cardinality problem  Table statistics – SMALL has 2 rows, LARGE 1000001 NUM_ROWS ---------- 2 NUM_ROWS ---------- 1000001
  • 44. Step 3: Choose the best strategy for fixing the cardinality problem  125000=1000001/8  Optimizer appears to assume all eight combinations of the three columns’ values are equally likely  Can’t verify formula – references don’t include formula with histograms  Even worse without histograms – cardinality is 500000
  • 45. Step 3: Choose the best strategy for fixing the cardinality problem  No SQL profile from SQL Tuning Advisor: There are no recommendations to improve the statement.  Neither histograms nor SQL profiles help example 4
  • 46. Step 3: Choose the best strategy for fixing the cardinality problem  Statement with hints:  Use index  Don’t do full scan select /*+ INDEX(B LARGEINDEX) NO_INDEX_FFS(B LARGEINDEX) */ B.NUM from SMALL A,LARGE B where a.NUM=B.NUM and A.NAME='FEW';
  • 47. Step 3: Choose the best strategy for fixing the cardinality problem  Time without a hint (1 second): Elapsed: 00:00:01.03  Time with a hint (1/100th second): Elapsed: 00:00:00.01
  • 48. Step 3: Choose the best strategy for fixing the cardinality problem  Changing the application  Change your tables so that the optimizer gets your SQL’s cardinality right  Requires more work designing tables, but keeps productivity benefits of SQL
  • 49. Step 3: Choose the best strategy for fixing the cardinality problem  Example 4 – moved NAME column to LARGE table and split table in two  One million (‘MANY’,1) rows in LARGEA  One (‘FEW’,2) row in LARGEB  Query: select NUM from (select * from largea union select * from largeb) where NAME='FEW';
  • 50. Step 3: Choose the best strategy for fixing the cardinality problem  Plan is just as efficient as with hint:  Number of rows = 2 (reality is 1)  Range Scan -------------------------------------------------------------- | Id | Operation | Name | Rows | -------------------------------------------------------------- | 0 | SELECT STATEMENT | | 2 | | 1 | VIEW | | 2 | | 2 | SORT UNIQUE | | 2 | | 3 | UNION-ALL | | | | 4 | TABLE ACCESS BY INDEX ROWID| LARGEA | 1 | |* 5 | INDEX RANGE SCAN | LARGEAINDEX | 1 | | 6 | TABLE ACCESS BY INDEX ROWID| LARGEB | 1 | |* 7 | INDEX RANGE SCAN | LARGEBINDEX | 1 | --------------------------------------------------------------
  • 51. Step 3: Choose the best strategy for fixing the cardinality problem  Time without table change (1 second): Elapsed: 00:00:01.03  Time with table change (1/100th second): Elapsed: 00:00:00.01
  • 52. Conclusion  SQL improves productivity, optimizer has limits  Identify cases where cardinality is wrong  Understand why the database got it wrong  One column  Multiple columns  Choose best strategy to fix  Give optimizer more info  Override optimizer’s choices  Redesign tables
  • 53. References  Cost Based Optimizer Fundamentals, Jonathan Lewis  Metalink Note:212809.1, Limitations of the Oracle Cost Based Optimizer  Metalink Note:68992.1, Predicate Selectivity  Histograms – Myths and Facts, Wolfgang Breitling Select Journal, Volume 13, Number 3