You are on page 1of 4

6/11/2016

OracleSQLtuningwithcardinalityestimates

SQLTuningwithcardinalityfeedback
ThecentralproblemwithcardinalityestimationistheincasesofcomplexWHEREclausestheoptimizerdoes
nothaveenoughinformationaboutinterjoinresultsetsizestodeterminetheoptimaltablejoinorder.
AlsoseetheseimportantnotesonSQLtuningbyaddingcolumnhistograms.Histogramscanimprovethe
guessesmadebytheoptimizerwhenacolumnhasanunevendistributionofvalues.

NoOptimizercanalwaysgetthecorrectcardinality
Evenwiththebestoptimizerstatistics,evenEinsteincouldnotpredicttheresultingrowsetsizefromacomplex
calculation:
impossibletopredictcardinalityofthenumberofrowsreturned
where
credit_rating*extended_credit>.07
and
(qty_in_stock*velocity)/.075<30
or
(sku_price/47)*(qty_in_stock/velocity)>47

Remember,cardinalityisafancywordfor"numberofrowsreturned"byaSQLoperation.Hereisanexample
ofthecardinalitythatisdisplayedaspartofanexecutionplan(the"card"column):
ExecutionPlan

0SELECTSTATEMENTOptimizer=CHOOSE(Cost=2871Card=2Bytes=143)
10UNIONALL
21SORT(GROUPBY)(Cost=2003Card=1Bytes=59)
32FILTER
43HASHJOIN(Cost=1999Card=1Bytes=59)
54INDEX(FASTFULLSCAN)OF'XIN8OPS_FLT_LEG'(UNIQUE)
64INDEX(RANGESCAN)OF'XIN3BAG_TAG_FLT_LEG'(UNIQUE)
71SORT(GROUPBY)(Cost=868Card=1Bytes=84)
87FILTER
98NESTEDLOOPS(Cost=864Card=1Bytes=84)
109HASHJOIN(Cost=862Card=1Bytes=57)
1110INDEX(FASTFULLSCAN)OF'XIN1SCHED_FLT_LEG'(UNIQUE)
1210INDEX(FASTFULLSCAN)OF'XIN8OPS_FLT_LEG'(UNIQUE)
139INDEX(RANGESCAN)OF'XIN2BAG_TAG_FLT_LEG'(UNIQUE)

ExecutionPlan

0SELECTSTATEMENTOptimizer=CHOOSE(Cost=3Card=100Bytes=8200)
10FILTER
21FILTER
32HASHJOIN(OUTER)
43TABLEACCESS(FULL)OF'BOOK'(Cost=1Card=20Bytes=1280)
53TABLEACCESS(FULL)OF'SALES'(Cost=1Card=100Bytes=1800)

Whenyoucatchtheoptimizermakingapoorjudgmentyoucanreverseengineerthecondition:
Gatherthe"real"rowcount.NotethatyouDONOTusethedba_tables.num_rowscolumnbecausethis
isonlyaccuratetothelastdbms_statsanalyze.Toreadthestepsofanexecutionplaninorder,seemy
notesonreadinganexecutionplaninorderofexecution.
Lookatthemetadatathatmightinfluencetheoptimizersdecision:
dba_tables.blocks
dba_tavble_num_rows
dba_tables.avg_row_leb
dba_tablespaces.blocksize
http://www.dbaoracle.com/t_sql_tuning_cardinality_plan.htm

1/4

6/11/2016

OracleSQLtuningwithcardinalityestimates

dba_histograms
dba_indexes
Asinglebadestimateofcardinalitycancascadeforward,causinglargeamountsofintermediaterowbaggageto
bepassedthroughsubsequentttablejoins,causingslowperformance.
Inthisexample,thefourwaytablejoinonlyreturns18rows,butthequerycarries9,000rowsinintermediate

resultsets,slowingdowntheSQLexecutionspeed:

Suboptimalintermediaterowsets.

Ifweweresomehowabletopredictthesizesoftheintermediateresults,wecanresequencethetablejoinorder
tocarrylessintermediatebaggageduringthefourwaytablejoin,inthisexamplecarryingonly3,000
intermediaterowsbetweenthetablejoins:

http://www.dbaoracle.com/t_sql_tuning_cardinality_plan.htm

2/4

6/11/2016

OracleSQLtuningwithcardinalityestimates

Optimalintermediaterowsets.

Oraclehistogramsandcardinality
Histogramsareusedtopredictcardinalityandthenumberofrowsreturnedtoaquery.Let'sassumethatwehave
avehicle_typeindexandthat65percentofthevaluesarefortheCARtype.Wheneveraquerywithwhere
vehicle_type='CAR'isspecified,afulltablescanwouldbethefastestexecutionplan,whileaquerywithwhere
vehicle_type='TRUCK'wouldbefasterwhenusingaccessviaanindex.
HistogramsaffectperformanceandshouldonlybeusedwhentheyarerequiredforafasterCBOexecutionplan.
TheyincuradditionaloverheadduringtheparsingphaseofanSQLquery.Histogramscanbeusedeffectively
onlywhen:
Atablecolumnisreferencedinoneormorequeries:Nevercreatehistogramsifqueriesdon't
referencethecolumn.NoviceDBAsmaymistakenlycreatehistogramsonaskewedcolumn,evenifit's
notreferencedinaquery.

Acolumn'svaluescausetheCBOtomakeanincorrectguess:IftheCBOmakesanincorrect
assumptionregardingthesizeofanintermediateresultset,itmaychooseasuboptimalexecutionplan.A
histogramaddedtothecolumnoftenprovidestheadditionalinformationrequiredfortheCBOtochoose
thebestplan.

Significantskewingexistsinthedistributionofacolumn'sdatavalues:Theskewmustofcourse,be
significantenoughtomaketheCBOchooseadifferentexecutionplan.
ToolstoassistinOptimizercardinalityestimation
TherearemanytoolstoassistwithSQLtuning,butthebesttoolswillexposealloftheinternalmetricsofthe
datadictionary.TheIontooldoesagreatjobataidingSQLtuning:
http://www.dbaoracle.com/t_sql_tuning_cardinality_plan.htm

3/4

6/11/2016

OracleSQLtuningwithcardinalityestimates

IonscreenforSQLtuning
TheIontoolisaneasywaytoanalyzeOracleSQLperformanceandIonalsoallowsyoutospothiddenSQL
performancetrends.

http://www.dbaoracle.com/t_sql_tuning_cardinality_plan.htm

4/4

You might also like