An Introduction To Oracle SQL Tuning

Chris Adkin 30th May 2008

31/01/2012

1

Some Inspirational Thoughts Before We Begin . . . 
   

The following Ask Tom excerpt comes in response to a ³You Asked Can u give a methodology of tuning the sql statements.´ question. The link to the full answer is at: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_I D:8764517459743 Despite being five years old, in the intervening time, artificial intelligence has not been built into the RDBMS where by it can re-write your SQL such that it will run in the most efficient manner possible. Advisors take some of the leg work out of tuning and the tools such as DBMS_XPLAN, v$ views etc constantly change, evolve and improve, however SQL tuning and writing efficient SQL is not a prescriptive process that can be captured in a process. I will however try to present useful techniques and good practise to µdemystify¶ some of the µart¶ behind this.
2

Some Inspirational Thoughts Before We Begin . . . 

1.1 Efficient SQL This was probably the hardest part of the book to write - this chapter. That is not because the material is all that complex, rather because I know what people want - and I know what can be delivered. What people want: The 10 step process by which you can tune any query. What can be delivered: Knowledge about how queries are processed, knowledge you can use and apply day to day as you develop them. Think about it for a moment.
3

there are many programs that actually try to do this . . SQL Navigator and others. 4 . offer to add hints to the query to try other access plans. What they do is primarily recommend indexing schemes to tune a query. we would write a program to do it. Oh don't get me wrong.000. suggest materialized views.Oracle Enterprise Manager with its tuning pack.  If there were a 10 step or even 1. .000 step process by which any query can be tuned (or even X% of queries for that matter).Some Inspirational Thoughts Before We Begin .

5 . . In fact.  They show you different query plans for the same statement and allow you to pick one. These tuning tools use a very limited set of rules that sometimes can suggest that index or set of indexes you really should have thought of during your design.it rewrites our queries all of the time.Some Inspirational Thoughts Before We Begin .the optimizer would do it as a matter of fact. the cost based optimizer does that already . . They offer "rules of thumb" (what I generally call ROT since the acronym and the word is maps to are so appropriate for each other) SQL optimizations .which if they were universally applicable .

Some Inspirational Thoughts Before We Begin .if there were an N step process to tuning a query. to writing efficient SQL . understand the question being asked and process the question . 6 . It is like the search for the holy grail .  I'll close this idea out with this thought .maybe someday the software will be sophisticated enough to be perfect in this regards.the optimizer would incorporate it all and we would not be having a discussion about this topic at all. .rather then syntax. it will be able to take our SQL. .

Section 1 Mechanics Of The Cost Based Optimizer 31/01/2012 7 .

Devises the best plan for executing a query based on cost. except when the µwrong¶ plan is selected.What Is The Optimizer      I will focus on the Cost Based Optimizer. Transparent to application and users. Around since Oracle 7. 8 . The stages of optimisation will be covered on the next set of slides.

9 . a hard parse or optimisation takes place (a gross over simplification. A parsed SQL statement (cursor) can have multiple child cursors. prior releases use a combination of SQL address and hash value. but basically what happens). but basically what happens).Stages Of Optimisation     On 10g parsed SQL statements are assigned unique identifiers called sql ids. Oracle can also perform optimisations at run time. If a query does not exist in parsed form in the shared pool. these are affectively different plans for the same SQL text when different variables are supplied (an over simplification.

Stages Of Optimisation  Establish the environment Ascertain what parameters are set  Ascertain what bug fixes are in place based on the setting of the compatible parameter   Query transformation Sub query un-nesting  Complex view merging  Set Join Conversion  Predicate move around  10 .

Join order consideration.Stages Of Optimisation    Establish the base statistics of all the relevant tables and indexes. Single table access cardinality estimation. 11 .  Record will be kept of the best join order maintained so far.

refer to:v$sys_optimizer_env  v$ses_optimizer_env  V$sql_optimizer_env  12 .What Influences The Cost Based Optimizer ?    Object statistics Object data types Oracle initialisation parameters.

aux_stats$ SQL> / SYSSTATS_INFO STATUS SYSSTATS_INFO DSTART SYSSTATS_INFO DSTOP SYSSTATS_INFO FLAGS SYSSTATS_MAIN CPUSPEEDNW SYSSTATS_MAIN IOSEEKTIM SYSSTATS_MAIN IOTFRSPEED SYSSTATS_MAIN SREADTIM SYSSTATS_MAIN MREADTIM SYSSTATS_MAIN CPUSPEED SYSSTATS_MAIN MBRC SYSSTATS_MAIN MAXTHR SYSSTATS_MAIN SLAVETHR 13 .What Influences The Cost Based Optimizer ?  System statistics:COMPLETED 11-26-2006 19:53 11-26-2006 19:53 1 1081.76881 10 4096 1* select * from sys.

What Influences The Cost Based Optimizer ?       System statistics facilitate something called the ³CPU costing model´ introduced in Oracle 9i. Stats that are not populated in aux_stats$ only appear after system statistics have been gathered. 14 . set this to IO or CPU. Until this came along the optimizer did not take into account CPU performance nor the difference in performance between single and multi block reads. In 10g ³out of the box´ statistics called ³no workload´ statistics are provided. In Oracle 9i. The optimizer cost model can be set via _optim_cost_model. no system statistics are present.

oracle.html   15 .  If you have any I/O sub system with a cache you can end up getting single block and multi block I/O times that reflect the speed of the cache rather than the under lying disks.com/technology/software/tech/orion/index.What Influences The Cost Based Optimizer ?  !!! Warning !!! You have to be very careful when gathering system statistics. Oracle supply an ³I/O calibration´ tool called Orion which might help here: http://www.  This can seriously skew a plan¶s costings.

col_usage$ in the data dictionary will be updated whenever a statement is parsed.  Oracle can get confused with what to do for columns where the LIKE and col_usage$ and hence not create histograms when size auto is specified.What Influences The Cost Based Optimizer  SQL Usage Unless the NO_MONITOR hint is used.  16 .  DBMS_STATS uses this in order to determine whether a histogram should be created on a column when size = AUTO is specified in the method_opt.

. constraints etc . .What Influences The Cost Based Optimizer  Hints and profiles SQL hints  Stored out lines (essentially hints)  SQL profiles as created by the Oracle 10g tuning advisor   Objects. 17 . partitioning. presence of indexes.

Section 2 A Plan Of Attack For Investigating A Bad Plan 31/01/2012 18 .

Identify The SQL To Be Tuned  Many ways of doing this:Automatic Database Diagnostic Manager $ORACLE_HOME/rdbms/admin/addmrpt  Automatic Workload Repository reports $ORACLE_HOME/rdbms/admin/awrrpt  Toad  SQL Tuning Advisor $ORACLE_HOME/rdbms/admin/sqltrpt  19 .

A Simple Script For Identifyng SQL To Be Tuned  SELECT * FROM (SELECT sql_id. As a first pass tuning try running the SQL tuning advisor on the query. elapsed_time. my preferred way of doing this is via the sqltrpt script in $ORACLE_HOME/rdbms/admin. concurrency_wait_time FROM v$sql ORDER BY elapsed_time DESC) WHERE rownum <= 10 In 10 onwards SQL statements are uniquely identified by sql ids. user_io_wait_time. for previous releases this a combination of hash value and sql address.   20 . cpu_time.

SQL that is inefficient by design. Lack of appropriate indexes to support your query Bugs in the optimizer.0. more on this later.4.0.Causes Of Poor Performance       Stale or missing statistics (includes missing histograms). there are the best part of 100 bug fixes related to the optimizer in 10.2. Optimizer flaws. such as the predicate independence assumption. 21 . SQL that is ³optimizer unfriendly´. more on this later.

Causes Of Poor Performance     Index. 22 . Misuse or poor use of Oracle features. Poor schema design. table type or partitioning schemes that are not conducive to good performance with the query mix you are running. Abuse of the Oracle optimizer environment.

 Is the part of the application running slowly because it is not designed to deal with the shape and / or size of the data it is processing ?.The Tuning Ethos  Ask the following questions:Has what I need to tune ever ran µfast¶.  Do not confuse something that is running slow because it has an abnormally high workload for something that is inefficient at what it does.  23 . if so what has changed in the environment.

Try to make the scope of any changes match the scope of the problem. Use realistic test data.g. e. if you have a problem with one query. Try to use a clean environment without factors that may skew your results. a global change such as a change to an initialisation parameter is unlikely to address the root cause of the issue and it may cause performance regression for queries that are currently performing well. 24 .The Tuning Ethos  When tuning     Only make one change at a time so as to be able to accurately gauge what affects your query.g. e. for accurate and consistent response times avoid database and servers which are being used by other users if possible.

g. e.  The use of hints.  Schema design changing.  25 . heap to index organized tables.The Tuning Ethos  Tuning a piece of SQL may result in:Indexes being added or modified.  Statistic gathering regimes being modified.  Object types being changed. extra columns added to tables in order to reduce numbers of joins.  SQL being re-written. e.g.

.g.Tuning Is Dead We Have The Tuning Advisor !!!  Based on my experience this:Recommends indexes  Identifies stale statistics  Notifies you of Cartesian joins  Spots predicates not conducive to the efficient use of indexes.  26 . .  Identifies parts of statements where view mergining cannot take place. e. col <>. . 2. col NOT IN (1.  Recommends SQL profiles.

27 . SQL profiles are enhanced hints that provide the optimizer with extra information with which to get cardinality estimates correct with. it is not artificially intelligent and it smacks of the µROT¶ tools that Tom Kyte mentioned. I am yet to see it.Tuning Is Dead We Have The Tuning Advisor !!!      If the tuning advisor does anything else. This is good for a first pass at identifying more obvious causes of a query running sub optimally. The use of SQL profiles can cause a problem if your base data changes such that they are no longer relevant. It is the best tool of its type that Oracle have produced to date.

A Simple ¶Methodology· For Query Tuning     Despite what the Oracle marketing machine may say. Some people instantly assume that because a plan contains a full table scan that the execution plan is therefore bad. This is an out dated method of tuning with origins based around the rule based optimizer. tuning is not a prescriptive process. The tuning advisor in its 10g incarnation is no substitute for a knowledgeable DBA at best it semi automates the task of tuning. 28 .

e. The table is µsmall¶ in size¶. 29 . I. Reasons for full table scans might include:    Queries that use un-selective predicates.Good And Bad Plans   Do not automatically jump to the conclusion that a bad plan contains full table scans and a good plan only uses index range scans. Queries return most of the columns in a table. result in all or a large proportion of a tables data being returned. The indexes you think the query should use have poor clustering factors.

you will not always be asked for the same information or to use the same diagnostic tools depending on who the service request is assigned to.A Simple ¶Methodology· For Query Tuning     What I will outline is a plan of attack for tackling query issues. aka the SQL test case builder. There is no such thing as a definitive tuning methodology as alluded to by Tom Kyte. Even when Oracle support is engaged with a tuning related service request. This may change with the DBMS_SQLDIAG package in 11g. 30 .

31 . are in place.A Simple ¶Methodology· For Query Tuning  My ³plan of attack´ will not cover the writing of efficient SQL. Oracle has produced sub optimal plans due to discrepancies between predicated cardinalities and actual cardinalities. partitioning schemes etc . . Nothing daft has been done to abuse the optimizer environment. . I will assume:    All reasonable indexes. this would require a different presentation entirely.

I.e.display_cursor(µsqlid¶)) Sql ids are new to Oracle 10g and uniquely identify parsed SQL statements 32 .A Simple ¶Methodology· For Query Tuning 1.display)  A better way of explaining the execution plan is by taking it straight from the shared pool:SQL> SELECT * FROM table(dbms_xplan. Obtain the execution plan for your query  If you run an explain plan from the SQL prompt this will give you the plan that Oracle predicts the statement will use.:SQL> EXPLAIN PLAN FOR SELECT * FROM dual SQL> SELECT * FROM table(dbms_xplan.

this may not necessarily but what the plan is when the query runs due to such things as bind variable peeking.A Simple ¶Methodology· For Query Tuning 1. 33 . NULL. Obtain the execution plan for your query   The DBMS_XPLAN package is the best tool here.DISPLAY_CURSOR(<your SQL id goes here>)  Obtain the plan from the workload repository SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL. Where possible obtain the plan after running the query: SELECT * FROM TABLE(DBMS_XPLAN. this will give you what the optimizer predicts the plan will be.DISPLAY_AWR(<your SQL id goes here>)  If you use explain plan. µALLSTATS LAST¶)  Obtain the plan from the shared pool SELECT * FROM TABLE(DBMS_XPLAN.

"SAL"<="SALGRADE"."LOSAL") 6 .filter("EMP".access("EMP"."DEPTNO") 34 .Understanding A Basic Execution Plan PLAN_TABLE_OUTPUT ----------------------------------------------------------------| Id | Operation |Name |Rows|Bytes|Cost | ----------------------------------------------------------------| 0 | SELECT STATEMENT | | | | | | 1 | NESTED LOOPS | | | | | | 2 | NESTED LOOPS | | | | | | 3 | TABLE ACCESS FULL | SALGRADE| | | | |* 4 | TABLE ACCESS FULL | EMP | | | | | 5 | TABLE ACCESS BY INDEX ROWID| DEPT | | | | |* 6 | INDEX UNIQUE SCAN | DEPT_PK | | | | ----------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------4 ."HISAL" AND "EMP"."SAL">="SALGRADE"."DEPTNO"="DEPT".

Is the access for the driving table via a full table scan. . Visually sanity check your execution plan    Does the plan contain any Cartesian joins ?. Is the most restricted table the µdriving¶ table.e. 35 .A Simple ¶Methodology· For Query Tuning 2. I. not always an issue. the table for which the filter predicates will reduce the most. . but could indicate that the join order is incorrect Continued . this is the first table in the join order and usually the table that is furthest indented into the plan.

VISIT_DATE_FROM < to_date(:6.ID_JOB )) ---------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost | ---------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 151 | 1187 | | 1 | HASH UNIQUE | | 1 | 151 | 1187 | | 2 | NESTED LOOPS | | 1 | 151 | 1176 | | 3 | MERGE JOIN CARTESIAN | | 1 | 104 | 684 | | 4 | NESTED LOOPS | | 1 | 51 | 665 | |* 5 | INDEX FAST FULL SCAN | TR_CONSUMPTION_HISTORY_IX2 | 1 | 5 | 663 | |* 6 | TABLE ACCESS BY INDEX ROWID| TR_JOB | 1 | 46 | 2 | |* 7 | INDEX UNIQUE SCAN | TR_JOB_ID_PK | 1 | | 1 | | 8 | BUFFER SORT | | 32474 | 1680K| 682 | | 9 | TABLE ACCESS FULL | TR_OFFCYCLE_JOB_DETAILS | 32474 | 1680K| 19 | |* 10 | TABLE ACCESS FULL | TR_CYCLIC_JOB_DETAILS | 1 | 47 | 492 | ---------------------------------------------------------------------------------------------- Continued .'yyyy-MM-dd hh24:MI:SS') AND F.APPMT_START_TIME < to_date(:4.'yyyy-MM-dd hh24:MI:SS') AND F. TR_CYCLIC_JOB_DETAILS C. an example:SELECT DISTINCT A. 36 .'yyyy-MM-dd hh24:MI:SS') AND C.VISIT_DATE_TO > to_date(:7.ID_JOB=A. TR_CONSUMPTION_HISTORY B WHERE A.'yyyy-MM-ddhh24:MI:SS') AND C.ID_JOB AND A.ID_JOB ) OR ( C.ID_JOB FROM TR_JOB A.ID_SERVICEPOINT =:1 AND A. . Visually sanity check your execution plan  Are you battling against the schema design. TR_OFFCYCLE_JOB_DETAILS F.SD_JOB_STATUS = :2 AND B.A Simple ¶Methodology· For Query Tuning 2. .APPMT_END_TIME > to_date(:5.ID_JOB = B.ID_JOB=A.SD_READING_TYPE = :3 AND (( F.

A Simple ¶Methodology· For Query Tuning 2. 37 . We have an index fast full scan (index equivalent of a full table scan) on TR_CONSUMPTION_HISTORY_IX2. specifically if one of the branches evaluates to TRUE the join predicates do not get performed for two tables. note that the only reason we are going to the TR_CONSUMPTION_HISTORY table is to get the contents of the sd_reading_type column. Visually sanity check your execution plan In the previous example there are two issues:  There is a Cartesian join due to the OR condition.

A Simple ¶Methodology· For Query Tuning 3. 38 . Check the µenvironment¶ is correct for the query:  Are statistics up to date for all the objects used by the query. this will also highlight any tables with stale statistics. . . supply the sql id for your query and the tuning advisor will go to work for you. you can run the sqltrpt script from $ORACLE_HOME/rdbms/admin. Continued . Do appropriate indexes exists.

A Simple ¶Methodology· For Query Tuning
3. Checking the µenvironment¶ is correct for the query From Greg Rahn of the Oracle Real World performance group:I think it¶s important to understand what variables influence the Optimizer in order to focus the debugging effort. There are quite a number of variables, but frequently the cause of the problem ones are: (1) non default optimizer parameters and (2) nonRepresentative object/system statistics. Based on my observations I would say that the most abused Optimizer parameters are: 
 

OPTIMIZER_INDEX_CACHING OPTIMIZER_INDEX_COST_ADJ DB_FILE_MULTIBLOCK_READ_COUNT
39

A Simple ¶Methodology· For Query Tuning
3. Checking the µenvironment¶ is correct for the query
Many see setting these as a solution to get the Optimizer to choose an index plan over a table scan plan,but this is problematic in several ways:  This is a global change to a local problem Although it appears to solve one problem, it is unknown how many bad execution plans resulted from this change.  The root cause of why the index plan was not chosen is unknown, just that tweaking parameters gave the desired result  Using non-default parameters makes it almost impossible to correctly and effectively troubleshoot the root cause

40

A Simple ¶Methodology· For Query Tuning
4. Understand your data, how µSelective¶ is it ?  

Are the predicates and variables used in your statement going to retrieve a µsignificant¶ or µsmall¶ proportion of rows from the queries base tables ?. The following predicates come from a query I have investigated the performance of:UPPER (a.walkroute_reviewed_flag) = 'Y' ( ( a.sd_service_type = '10000' AND a.sd_servicepoint_status = '10001' ) OR ( a.sd_service_type = '10001' AND a.sd_occupied_status = '10001' AND a.sd_servicepoint_status = '10001' ) OR a.sd_servicepoint_status = '10000' OR a.sd_servicepoint_status = '10003µ

It returns a third of the data in the AD_SERVICE_POINT table, therefore if the clustering factors of the appropriate indexes are not particularly good, a full table scan is probably the most efficient way to retrieve the relevant data.41

 If predicted cardinalities are accurate. will be out by an order of magnitude and µripple¶ throughout the rest of the place.A Simple ¶Methodology· For Query Tuning 5. detailed analysis of access path costing may need to be performed. Are the predicted cardinalities for for the execution plan accurate  If the predicted cardinalities for the row sources in the plan are accurate and the appropriate access paths (indexes) exist. when bad. 42 .  Bad cardinality estimates.  This method of trouble shooting is sometimes referred to as ³Tuning by cardinality feedback´. the optimizer will usually come up with a µgood¶ plan.

´Tuning By Cardinality Feedbackµ    Generally speaking if the predicated cardinalities and actual cardinalities for a row source are close. the DBMS_XPLAN package. the optimizer will pick the best plan for the query. To start down this route of tuning we need to obtain the SQL text for the query in question along with any bind variable values it uses. The best way of doing this is via one of the most useful tools in our tuning µtoolbox¶. 43 .

I would not advocate playing around with density settings as this may fix queries suffering from the ³Predicate Independence Assumption´ but cause bad plans for other queries.pdf. Other Oracle professionals may call this something different.org/2007/11/21/troubleshooting-bad-executionplans/ With the GATHER_PLAN_STATISTICS hint and ³ALLSTATS LAST´ format string in DBMS_XPLAN (Oracle 10g).com/Tuning by Cardinality Feedback. 44 .centrexcc. this is a really easy method to use. This is also endorsed by the Oracle Real World Performance Group http://structureddata.´Tuning By Cardinality Feedbackµ      ³Tuning by cardinality feedback´ is borrowed from one of the reviewers of Jonathan Lewis¶s Cost Based Fundamental¶s book: www.

:1 (NUMBER): 80310011 Predicate Information (identified by operation id): --------------------------------------------------2 .access("ID_PACK"=:1) Note ----.cpu costing is off (consider enabling it) 45 .0.display_cursor('ft33c3agapy0k'. child number 0 ------------------------------------UPDATE TR_CYCLIC_WORK_PACK SET PACK_JOB_COUNTER =PACK_JOB_COUNTER +1 WHERE ID_PACK=:1 Plan hash value: 115273857 -------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost | -------------------------------------------------------------------------| 0 | UPDATE STATEMENT | | | | 2 | | 1 | UPDATE | TR_CYCLIC_WORK_PACK | | | | |* 2 | INDEX UNIQUE SCAN| TR_CYC_WORKPACK_PK | 1 | 9 | 1 | -------------------------------------------------------------------------Peeked Binds (identified by position): -------------------------------------1 .'TYPICAL +PEEKED_BINDS')) 2 / SQL_ID ft33c3agapy0k.Obtaining Your SQL Text + Binds  A trivial example to whet the appetite SQL> select * from table(dbms_xplan.

NULL.gather_table_stats(user.01 | 4 | |* 2 | INDEX UNIQUE SCAN| TR_CYC_WORKPACK_PK | 1 | 1 | 1 |00:00:00.01 | 2 | ---------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------2 .Checking Predicated Versus Estimated Cardinalities SQL> SQL> SQL> SQL> 2 3 SQL> var b1 number exec :b1 := 803100113 exec dbms_stats.display_cursor(NULL.'TR_CYCLIC_WORK_PACK'.CASCADE=>TRUE).cpu costing is off (consider enabling it) 46 . UPDATE /*+ GATHER_PLAN_STATISTICS */ TR_CYCLIC_WORK_PACK SET PACK_JOB_COUNTER =PACK_JOB_COUNTER +1 WHERE ID_PACK=:b1 / select * from table(dbms_xplan.access("ID_PACK"=:B1) Note ----.'ALLSTATS LAST')). child number 0 ------------------------------------UPDATE /*+ GATHER_PLAN_STATISTICS */ TR_CYCLIC_WORK_PACK =PACK_JOB_COUNTER +1 WHERE ID_PACK=:b1 Plan hash value: 115273857 SET PACK_JOB_COUNTER ---------------------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | ---------------------------------------------------------------------------------------------------| 1 | UPDATE | TR_CYCLIC_WORK_PACK | 1 | | 1 |00:00:00.ESTIMATE_PERCENT=>NULL. SQL_ID cqnxyqmp08rtu.

this inaccuracy in will µripple¶ throughout the rest of the plan and lead to poor performance. Line 2 in the execution plan is what is known as a row source. On the next slide a more complex example will be provided. If nested loop joins are used:starts x E-Rows = A-Rows otherwise E-Rows = A-Rows   Usually for a bad plan the estimated cardinalities will differ from the predicated cardinalities by orders of magnitude.Checking Predicated Versus Estimated Cardinalities     The example on the previous slide was the simplest example that can be provided. 47 . Note that for line 2 the values in the E-Rows and A-Rows columns matched.

In 10g dynamic sampling or hints can help here.Reasons For Estimated Cardinalities Being Inaccurate  Statistics gathering limitations:    Columns with skewed containing more than 254 distinct values => can be rectified with the DBMS_STATS API. more on this later. Not possible until 11g to gather statistics on functions unless function based indexes are used. 48 . this is due to what is sometimes called ³data correlation´ or the ³predicate independence assumption´. Not possible until 11g to gather statistics on correlated columns. DBMS_STATS in 10g can produce statistically incorrect estimates of distinct values when auto sampling / sampling is used.

a particular problem with µscratch¶ data. During the course of processing data changes such that it no longer reflects the statistics in the data dictionary. Lack of histograms or histograms with too few buckets on columns with skewed data. Sampled statistics taken with too small a sample size. in 11g auto sampling gives 100% statistically accurate statistics. dynamic sampling can help here. 49 .Reasons For Estimated Cardinalities Being Inaccurate  None representative statistics:    Statistics are missing or stale.

50 .Reasons For Estimated Cardinalities Being Inaccurate  Optimizer unfriendly SQL  Using values in the WHERE clauses with data types which are different to those used by the columns comparisons are being made against.

Reasons For Estimated Cardinalities Being Inaccurate  Other factors: Optimizer bugs. 51 .

#2 write SQL that gives the optimizer a fighting chance of gets cardinality estimates correct. as soon as the data changes. the plan you have forced via hints may no longer be the µbest¶ plan. 52 .e. watch out for the use of functions and expressions. #1 In the first place your stats should be correct. #3 If you run into the ³Predicate independence assumption´ issue this is a tough nut to crack. however. look at using dynamic_sampling.Fixing Bad Cardinality Estimates     Hints may work fine for the data in your database at the time of testing. read this article first:http://structureddata. I.org/2008/03/05/there-is-no-time-like-now-to-use-dynamic-sampling/  #4 Use hints and profiles when all else fails.

Unless a hint is for DYNAMIC_SAMPLING only use access path hints as a last resort. Only create histograms where they are required. 53 . they can have the side affect of increased hard parsing through bind peeking.Fixing Bad Cardinality Estimates     Histograms should be present for columns with skewed data with the correct number of buckets. !!! A histogram with only two end points stores no information on data distribution !!!.

Section 3 Worked Examples Of Tuning BY ³Cardinality Feedback´ 31/01/2012 54 .

b. c. b. g. c.out_post_code || ' ' || g.id_customer. 10011) AND sd_job_type IN (10002. g.id_servicepoint NOT IN ( SELECT id_servicepoint FROM tr_job WHERE sd_job_status IN (10003. c.address_line9. a.in_post_code ELSE g.address_line8.address_line12. b. g. g.trading_name. a. ad_personal_details d. g. g.better_address_flag. 10003)) AND UPPER (b.middlename.address_line12 AS best_address_line12.ID AS customerid.address_line1 AS best_address_line1.address_line10.address_line5.id_personal_details = d.forename. ROWNUM rnum FROM (SELECT DISTINCT a. a.better_address_flag = 'N' THEN b. g. a. b.sd_customer_type = f.address_line5 AS best_address_line5. d. b.id_best_address = g. g. b.address_line9 AS best_address_line9.sd_customer_type. b.address_line11 AS best_address_line11. 'WALKROUTEREFID' AS walkrouterefid.servicepoint_refid.business_name.incomp_postcode.ID AND b.out_post_code AS best_out_post_code.out_post_code.incomp_postcode IN ('n'.in_post_code AS best_in_post_code. ad_customer c. g.out_post_code || ' ' || b. f.id_customer AND a.out_post_code) = 'FY1' ORDER BY out_in_postcode) a WHERE ROWNUM <= 26) WHERE rnum > 0 55 .customer_ref.address_line4.address_line7.address_line6. g.in_post_code END AS out_in_postcode FROM ad_service_point a.*. b.address_line3. g. f.address_line1. ad_best_address g WHERE a. g.address_line8 AS best_address_line8.address_line11. b.do_not_visit_flag. b.surname. d.id_portfolio_address AND a. b. b. b.address_line6 AS best_address_line6. d.in_post_code. CASE WHEN b.id_personal_details AND c.address_line2.description. b. g.address_line3 AS best_address_line3. c.id_personal_details. sd_customer_type f.address_line4 AS best_address_line4.id_servicepoint. b.id_customer = c.id_best_address(+) AND c. 'N') AND a.A ¶Simple· Example Of Bad Cardinality Estimate Trouble Shooting SELECT * FROM (SELECT a.address_line10 AS best_address_line10.target_read_date.address_line2 AS best_address_line2. b.id_portfolio_address = b. g. c. ad_portfolio_address b.address_line7 AS best_address_line7.

"ID_CUSTOMER"="C"."ID") 20 .58 |* 11 | HASH JOIN | | 1 | 26626 | 43983 |00:00:23.36 | 7 | NESTED LOOPS | | 1 | 1 | 43983 |00:00:37.13 |* 15 | INDEX UNIQUE SCAN | AD_BESTADD_PK | 43983 | 1 | 825 |00:00:00."ID_PORTFOLIO_ADDRESS") 12 .access("C".90 | | 3 | VIEW | | 1 | 1 | 26 |00:00:38.75 | 6 | NESTED LOOPS | | 1 | 1 | 43983 |00:00:38."INCOMP_POSTCODE"))) 15 .access("A".access("A".access("A".90 | |* 2 | COUNT STOPKEY | | 1 | | 26 |00:00:38.90 | |* 4 | SORT ORDER BY STOPKEY | | 1 | 1 | 26 |00:00:38.filter("RNUM">0) 2 ."ID_PORTFOLIO_ADDRESS"="B"."SD_CUSTOMER_TYPE"="F".filter(ROWNUM<=26) 4 ."ID_CUSTOMER") 19 .04 | 13 | TABLE ACCESS FULL | AD_SERVICE_POINT | 1 | 6762K| 6762K|00:00:20.40 |* 12 | INDEX FAST FULL SCAN | AD_PORTADD_PK | 1 | 26626 | 43983 |00:00:13.60 | 10 | NESTED LOOPS OUTER | | 1 | 26626 | 43983 |00:00:23.filter((INTERNAL_FUNCTION("SD_JOB_STATUS") AND INTERNAL_FUNCTION("SD_JOB_TYPE"))) 17 ."ID_BEST_ADDRESS"="G".11 | 18 | TABLE ACCESS BY INDEX ROWID | SD_CUSTOMER_TYPE | 43983 | 1 | 43983 |00:00:00.30 | 14 | TABLE ACCESS BY INDEX ROWID| AD_BEST_ADDRESS | 43983 | 1 | 825 |00:00:00.A ¶Simple· Example Of Bad Cardinality Estimate Trouble Shooting ---------------------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time ---------------------------------------------------------------------------------------------------|* 1 | VIEW | | 1 | 1 | 26 |00:00:38.92 | 8 | NESTED LOOPS | | 1 | 1 | 43983 |00:00:37.filter(ROWNUM<=26) 9 ."OUT_POST_CODE")='FY1' AND INTERNAL_FUNCTION("B".access("C"."ID_PERSONAL_DETAILS"="D".90 | 5 | HASH UNIQUE | | 1 | 1 | 43983 |00:00:38."ID_SERVICEPOINT"="ID_SERVICEPOINT") PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------11 .43 |* 17 | INDEX UNIQUE SCAN | AD_CUST_ID_PK | 43983 | 1 | 43983 |00:00:01.39 ---------------------------------------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------1 .filter((UPPER("B"."ID_PERSONAL_DETAILS") | Buffers | Reads | | | | | | | | | | | | | | | | | | | | 385K| 385K| 385K| 385K| 385K| 385K| 297K| 253K| 165K| 148K| 147K| 564 90832 1 826 | 16759 | 87968 | 2 | | Writ 637 | 637 | 637 | 637 637 0 0 0 | | | | | 139K| 139K| 139K| 139K| 139K| 138K| 138K| 138K| 124K| 107K| 107K| 12M 0 | 0 | 0 | 9005K 6444K 1 16 56 .access("A".06 |* 16 | INDEX FAST FULL SCAN | TR_JOB_IX13 | 1 | 266K| 3 |00:00:04."ID_BEST_ADDRESS") 16 .09 |* 20 | INDEX UNIQUE SCAN | AD_PERDET_ID_PK | 43983 | 1 | 43983 |00:00:00.66 | |* 9 | HASH JOIN ANTI | | 1 | 1 | 43983 |00:00:36.22 |* 19 | INDEX UNIQUE SCAN | SD_CUSTTYPE_ID_PK | 43983 | 1 | 43983 |00:00:00.

COUNT(DISTINCTSD_JOB_TYPE) COUNT(DISTINCTSD_JOB_STATUS) -------------------------.-------SD_JOB_STATUS 5 SD_JOB_TYPE 2 57 . count(*) from user_tab_histograms where column_name in ('SD_JOB_TYPE'.---------------------------3 5  1 Now lets look at the histograms on these columns:- select column_name. 'SD_JOB_STATUSµ) 2* group by column_name SQL> / COLUMN_NAME COUNT(*) ---------------.A ¶Simple· Example Of Bad Cardinality Estimate Trouble Shooting   Lets create a histogram on sd_job_type Lets look at the distinct values in the sd_job_type and sd_job_status columns:- SQL> select count(distinct sd_job_type). count(distinct sd_job_status) from tr_job.

'TR_JOB'. 58 SQL> exec dbms_stats.    .gather_table_stats(user.method_opt=>'FOR COLUMNS SD_JOB_TYPE SIZE 3'.A ¶Simple· Example Of Bad Cardinality Estimate Trouble Shooting  The estimated cardinality is going badly wrong on line 16 of the plan. We¶ll flush the shared pool. but this illustrates the general principle of tuning by this approach. We now need to look at line 12 on the plan. re run the query and get the estimated and actual cardinalities from the shared pool with DBMS_STATS. You will see that on line 13 of the plan that the estimated and actual cardinalities are closer.estimate_percent=>NULL).

access("A".01 |* 14 | HASH JOIN RIGHT OUTER | | 1 | 6762K| 6762K|00:00:28.filter(ROWNUM<=26) PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------10 .access("C"."ID_PORTFOLIO_ADDRESS") filter((UPPER("B"."OUT_POST_CODE")='FY1' AND INTERNAL_FUNCTION("B".filter("RNUM">0) 2 .14 | 8 | NESTED LOOPS | | 1 | 1 | 43983 |00:02:35.50 ---------------------------------------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------1 .access("C".access("A".30 |* 20 | INDEX UNIQUE SCAN | SD_CUSTTYPE_ID_PK | 43983 | 1 | 43983 |00:00:00."ID_PERSONAL_DETAILS") | Buffers | Reads | | | | | | | | | | | | | | | | | | | | | 13M| 13M| 13M| 13M| 13M| 13M| 13M| 13M| 13M| 91941 | 409 | 409 | 34 | 91532 | 700 | 9083 13M| 87968 2 | | W 637 | 637 | 637 | 148K| 148K| 148K| 148K| 148K| 147K| 147K| 147K| 131K| 91844 | 376 | 37 91468 | 637 0 0 0 0 | | | | 0 | 59 .35 | 15 | TABLE ACCESS FULL | AD_BEST_ADDRESS | 1 | 102K| 102K|00:00:00.66 | 7 | NESTED LOOPS | | 1 | 1 | 43983 |00:02:36.81 |* 18 | INDEX UNIQUE SCAN | AD_CUST_ID_PK | 43983 | 1 | 43983 |00:00:05.access("A".86 | 6 | NESTED LOOPS | | 1 | 1 | 43983 |00:02:36."SD_CUSTOMER_TYPE"="F".access(("SD_JOB_TYPE"=10002 OR "SD_JOB_TYPE"=10003)) 14 ."ID_CUSTOMER") 20 .filter(ROWNUM<=26) 4 ."ID") 21 .A ¶Simple· Example Of Bad Cardinality Estimate Trouble Shooting ---------------------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time ---------------------------------------------------------------------------------------------------|* 1 | VIEW | | 1 | 1 | 26 |00:03:24."ID_PORTFOLIO_ADDRESS"="B"."ID_CUSTOMER"="C".access("A"."ID_PERSONAL_DETAILS"="D".06 |* 13 | INDEX RANGE SCAN | TR_JOB_IX3 | 2 | 45251 | 36201 |00:00:00.06 |* 12 | TABLE ACCESS BY INDEX ROWID| TR_JOB | 2 | 8237 | 3 |00:00:00.00 | |* 2 | COUNT STOPKEY | | 1 | | 26 |00:03:24.filter(("SD_JOB_STATUS"=10003 OR "SD_JOB_STATUS"=10011)) 13 .11 |* 21 | INDEX UNIQUE SCAN | AD_PERDET_ID_PK | 43983 | 1 | 43983 |00:00:00.78 | 9 | NESTED LOOPS | | 1 | 1 | 43983 |00:02:29."ID_BEST_ADDRESS"="G".00 | |* 4 | SORT ORDER BY STOPKEY | | 1 | 1 | 26 |00:03:24.00 | 5 | HASH UNIQUE | | 1 | 1 | 43983 |00:03:23.18 |* 17 | INDEX UNIQUE SCAN | AD_PORTADD_PK | 6762K| 1 | 43983 |00:02:32."INCOMP_POSTCODE"))) 18 .11 | 11 | INLIST ITERATOR | | 1 | | 3 |00:00:00.76 |* 10 | HASH JOIN RIGHT ANTI | | 1 | 1 | 6762K|00:00:35.00 | 3 | VIEW | | 1 | 1 | 26 |00:03:24."ID_BEST_ADDRESS") 17 ."ID_SERVICEPOINT"="ID_SERVICEPOINT") 12 .50 | 19 | TABLE ACCESS BY INDEX ROWID | SD_CUSTOMER_TYPE | 43983 | 1 | 43983 |00:00:00.01 | 16 | TABLE ACCESS FULL | AD_SERVICE_POINT | 1 | 6762K| 6762K|00:00:14.

running statements with the GATHER_PLAN_STATISTICS hint may not be practical without taking the query apart. In cases where queries may take hours to run. We will use a divide and conquer strategy to work out where the predicted cardinality is going wrong. 60 .A More Complex Example    Lets turn the complexity setting up a notch.

11 |* 4 | VIEW | | 1 | 1 | 26 |00:00:02.63 | 22 | VIEW | | 1 | 5945 | 26 |00:01:31.02 |* 25 | HASH JOIN | | 1 | 5945 | 1799K|00:01:24. . . .93 | 9 | NESTED LOOPS | | 1 | 1 | 107 |00:00:02.20 ---------------------------------------------------------------------------------------------------| Buffers | Reads | | | | | | | | | | | 199K| 199K| 199K| 4301 | 4301 | 4301 | 4301 4290 | 3967 | 3751 | 3749 | 304K| 304K| 304K| 3703 | 3703 | 3703 | 3692 | 3674 | 3547 3546 62937 62937 0 | 0 0 | | | | | | | | | | | 195K| 195K| 195K| 195 159K| 159K| 159K| 102K| 300K| 300K| 300K| 300K| 300K| 300K| 137K| 629 6 6 6 | continued .01 |* 17 | INDEX UNIQUE SCAN | AD_CUST_ID_PK | 107 | 1 | 107 |00:00:00.62 | 26 | TABLE ACCESS FULL | AD_WALKROUTE | 1 | 10061 | 10061 |00:00:00.6 |* 32 | INDEX UNIQUE SCAN | SD_FIELDREG_ID_PK | 1799K| 1 | 1799K|00:00:03.74 | 3 | UNION-ALL | | 1 | | 52 |00:00:02.11 | 6 | VIEW | | 1 | 1 | 26 |00:00:02.14 | 10 | NESTED LOOPS | | 1 | 1 | 107 |00:00:01.52 |* 18 | TABLE ACCESS BY INDEX ROWID | AD_BEST_ADDRESS | 107 | 1 | 107 |00:00:00. |* 19 | INDEX UNIQUE SCAN | AD_BESTADD_PK | 107 | 1 | 107 |00:00:00.0 |* 16 | INDEX UNIQUE SCAN | SD_FIELDREG_ID_PK | 107 | 1 | 107 |00:00:00.01 |* 20 | VIEW | | 1 | 26 | 26 |00:01:31.11 |* 7 | SORT UNIQUE STOPKEY | | 1 | 1 | 26 |00:00:02. .74 | 2 | SORT UNIQUE | | 1 | 27 | 52 |00:01:33.04 |* 15 | INDEX UNIQUE SCAN | AD_WALKROUTE_PK | 107 | 1 | 107 |00:00:00. |* 13 | INDEX FULL SCAN | AD_SERVICE_POINT_IX6 | 1 | 1 | 1109 |00:00:00.54 |* 12 | TABLE ACCESS BY INDEX ROWID| AD_SERVICE_POINT | 1 | 1 | 107 |00:00:00.01 |* 27 | HASH JOIN | | 1 | 5945 | 1799K|00:01:20.11 | 8 | NESTED LOOPS | | 1 | 1 | 107 |00:00:02.99 |* 28 | HASH JOIN | | 1 | 5945 | 1800K|00:00:34. |* 31 | INDEX FAST FULL SCAN | AD_PORTADD_PK | 1 | 67137 | 6668K|00:00:06.63 |* 21 | COUNT STOPKEY | | 1 | | 26 |00:01:31.63 | 24 | NESTED LOOPS | | 1 | 5945 | 1799K|00:01:30.63 |* 23 | SORT UNIQUE STOPKEY | | 1 | 5945 | 26 |00:01:31.85 |* 29 | TABLE ACCESS FULL | AD_SERVICE_POINT | 1 | 5945 | 2043K|00:00:16. 61 .55 | 11 | NESTED LOOPS | | 1 | 1 | 107 |00:00:01.28 | 14 | TABLE ACCESS BY INDEX ROWID| AD_WALKROUTE | 107 | 1 | 107 |00:00:00.11 |* 5 | COUNT STOPKEY | | 1 | | 26 |00:00:02.A ¶Complex· Example Of Bad Cardinality Estimate Trouble Shooting I said I would provide a more involved example of plan for working through bad cardinality estimate trouble shooting . :---------------------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time ---------------------------------------------------------------------------------------------------| 1 | VIEW | | 1 | 27 | 52 |00:01:33.37 | 30 | INDEX FAST FULL SCAN | AD_CUSTOMER_IX1 | 1 | 6296K| 6296K|00:00:00.

"ID_PORTFOLIO_ADDRESS") 28 .filter("A".access("A"."ID_WALKROUTE") 16 .filter(("A"."ID_WALKROUTE"="C"."ID_CUSTOMER") 29 ."ID_CUSTOMER"="D"."ID_BEST_ADDRESS" IS NULL AND UPPER("A".access("A"."ID_CUSTOMER"="D".cpu costing is off (consider enabling it) PLAN_TABLE_OUTPUT 62 ."SD_SERVICE_TYPE"=10000) OR ("A"."INCOMP_POSTCODE")='N') 19 ."SD_FIELD_REGION"="ID") Note ----."SD_SERVICEPOINT_STATUS")))) 31 ."ID_WALKROUTE"="C".access("A"."INCOMP_POSTCODE")='N') 32 ."ID_BEST_ADDRESS" IS NOT NULL) 15 ."WALKROUTE_REVIEWED_FLAG")='Y' AND (("A" PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------("A".filter(UPPER("B"."ID_CUSTOMER") 18 ."ID_PORTFOLIO_ADDRESS"="B".access("A"."SD_SERVICEPOINT_STATUS"=10001 AND "A".filter(ROWNUM<=26) 25 ."SD_OCCUPIED_ INTERNAL_FUNCTION("A"."ID_WALKROUTE") PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------27 ."ID_BEST_ADDRESS") 20 ."ID_WALKROUTE" IS NOT NULL AND "A".filter("RNUM">0) 21 ."SD_SERVICE_TY 13 .access("C"."SD_FIELD_REGION"="ID") 17 .access("C"."SD_SERVICEPOINT_STATUS"=10001 AND "A"."SD_OCCUPIED_STATUS"=10001 AND "A".access("A".filter(UPPER("B"."ID_WALKROUTE" IS NOT NULL AND UPPER("A".A ¶Complex· Example Of Bad Cardinality Estimate Trouble Shooting Predicate Information (identified by operation id): --------------------------------------------------4 5 7 12 filter("RNUM">0) filter(ROWNUM<=26) filter(ROWNUM<=26) filter(("A".access("A"."ID_BEST_ADDRESS"="B".filter(ROWNUM<=26) 23 ."WALKR AND "A".

A ¶Complex· Example Of Bad Cardinality Estimate Trouble Shooting   Start by looking for the row source which is the furthest into the execution plan for where the cardinality is out by an order of magnitude."SD_SERVICEPOINT_STATUS"=10001 AND "A"."SD_OCCUPIED_ INTERNAL_FUNCTION("A".85 | 102K| 137K| |* 28 |   We have an estimated cardinality of 5945 versus an actual cardinality of 1."ID_BEST_ADDRESS" IS NULL AND UPPER("A".000. Oracle thinks that the predicates associated with line 28 are for line 29."SD_SERVICEPOINT_STATUS"))))   Due to a possible bug. 63 . lets look at these 29 . this means that there are predicates associated with this table. Now lets use a ³Divide and conquer´ strategy for working out where exactly in this list of predicates that are OR-ed and AND-ed together the estimate is going wrong.800. This is line 28 in our plan:HASH JOIN | | 1 | 5945 | 1800K|00:00:34. Notice the µ*¶ at the beginning of the line."SD_SERVICE_TYPE"=10000) OR ("A".filter(("A"."WALKR AND "A"."ID_WALKROUTE" IS NOT NULL AND "A".

'ALLSTATS LAST')).sd_servicepoint_status = '10001' ) OR ( a.Trouble Shooting ² 1st Pass 1 SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) ad_service_point a a.42 | 92450 | 9 ---------------------------------------------------------------------------------------------------. child number 0 ------------------------------------SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) FROM ad_service_point a WHERE a.sd_servicepoint_status = '10003' ) 2 3 FROM 4 WHERE 5 AND 6 AND 7 8 9 10 11 12 13* 14 / COUNT(*) ---------2043175 SQL> select * from table(dbms_xplan.walkroute_reviewed_flag) = 'Y' ( ( a.sd_occupied_status = '10001' AND a.id_best_address IS NULL AND UPPER (a.sd_servicepoint_status = '10000' OR a.sd_service_type = '10001' AND a. . .sd_servicepoint_status = '10001' ) OR ( a. PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------SQL_ID 9qvfwxgnv1scj.sd_servicepoint_status = '10001' ) OR a.sd_service_type = '10001' AND a.walkroute_reviewed_flag) = 'Y' AND ( ( a. 64 .sd_service_type = '10000' AND a.sd_servicepoint_status = '10003' ) Plan hash value: 185642247 PLAN_TABLE_OUTPUT ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads | ---------------------------------------------------------------------------------------------------| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:17.display_cursor(NULL.id_best_address IS NULL UPPER (a.NULL.sd_service_type = '10000' AND a.sd_servicepoint_status = '10000' OR a.sd_occupied_status = '10001' AND a.77 | 92450 | 92426 | |* 2 | TABLE ACCESS FULL| AD_SERVICE_POINT | 1 | 19524 | 2043K|00:00:18.sd_servicepoint_status = '10001' ) OR a. .

65 .walkroute_reviewed_flag) = µY¶ and a load of stuff relating to standing data columns within brackets.id_best_address IS NULL UPPER (a.Analysis Of 1st Pass  We have two sets of predicates the id_best_address IS NULL and UPPER(a.sd_servicepoint_status = '10000' OR a.sd_occupied_status = '10001' AND a.sd_servicepoint_status = '10003' ) 2 3 FROM 4 WHERE 5 AND 6 AND 7 8 9 10 11 12 13* 14 /  For our second pass lets work which one of these sections is causing the cardinality estimate to go wrong. 1 SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) ad_service_point a a.walkroute_reviewed_flag) = 'Y' ( ( a.sd_service_type = '10000' AND a.sd_servicepoint_status = '10001' ) OR ( a.sd_servicepoint_status = '10001' ) OR a.sd_service_type = '10001' AND a.

sd_occupied_status = '10001' AND a.sd_servicepoint_status = '10003' ) 2 3 FROM 4 WHERE 5 AND 6 AND 7 8 9 10 11 12 13* 14 /  For our second pass lets work which one of these sections is causing the cardinality estimate to go wrong.sd_servicepoint_status = '10001' ) OR ( a.walkroute_reviewed_flag) = 'Y' ( ( a.id_best_address IS NULL UPPER (a. 66 .sd_service_type = '10001' AND a.Analysis Of 1st Pass  We have two sets of predicates the id_best_address IS NULL and UPPER(a.sd_servicepoint_status = '10000' OR a.walkroute_reviewed_flag) = µY¶ and a load of stuff relating to standing data columns within brackets. 1 SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) ad_service_point a a.sd_servicepoint_status = '10001' ) OR a.sd_service_type = '10000' AND a.

NULL.37 | 92450 | 92426 | PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------|* 2 | TABLE ACCESS FULL| AD_SERVICE_POINT | 1 | 6712K| 6712K|00:00:13. 1* select * from table(dbms_xplan.display_cursor(NULL.filter("A".'ALLSTATS LAST')) /*+ GATHER_PLAN_STATISTICS */ COUNT(*) ad_service_point a a.46 | 92450 | 9 ---------------------------------------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------2 .id_best_address IS NULL Plan hash value: 185642247 ---------------------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads | ---------------------------------------------------------------------------------------------------| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:16.NULL. child number 0 ------------------------------------SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) FROM ad_service_point a WHERE a.Trouble Shooting ."ID_BEST_ADDRESS" IS NULL) Note ----.cpu costing is off (consider enabling it) 67 .display_cursor(NULL.id_best_address IS NULL SQL> SELECT 2 3 FROM 4 WHERE 5 / COUNT(*) ---------6712561 SQL> select * from table(dbms_xplan.2nd Pass  It appears that the cardinality estimate is going wrong in the section in brackets. PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------SQL_ID 2545fjyq0m5nm.'ALLSTATS LAST')).

03 | 6 | ---------------------------------------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------3 .03 | 6 | 5 | PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------| 2 | INLIST ITERATOR | | 1 | | 282 |00:00:00.'ALLSTATS LAST'))."SD_SERVICEPOINT_STATUS"=10003)) Note ----- 5 | 5 | 68 .NULL.sd_servicepoint_status = '10003' Plan hash value: 2502039880 ---------------------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads | ---------------------------------------------------------------------------------------------------| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:00. SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) ad_service_point a a.sd_servicepoint_status = '10003' 2 3 FROM 4 WHERE 5* OR SQL> / COUNT(*) ---------282 SQL> select * from table(dbms_xplan.display_cursor(NULL.02 | 6 | |* 3 | INDEX RANGE SCAN| AD_SERVICE_POINT_IX5 | 2 | 282 | 282 |00:00:00."SD_SERVICEPOINT_STATUS"=10000 OR "A". child number 0 ------------------------------------SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) FROM ad_service_point a WHERE a.Trouble Shooting ² 3rd Pass  1 Lets break down the section in brackets. PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------SQL_ID 4rjg28wg48zv7.access(("A".sd_servicepoint_status = '10000' a.sd_servicepoint_status = '10000' OR a.

access(("A".03 | 6 | 5 | PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------| 2 | INLIST ITERATOR | | 1 | | 282 |00:00:00."SD_SERVICEPOINT_STATUS"=10003)) Note ----- 5 | 5 | 69 .sd_servicepoint_status = '10003' Plan hash value: 2502039880 ---------------------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads | ---------------------------------------------------------------------------------------------------| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:00.03 | 6 | ---------------------------------------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------3 .display_cursor(NULL.sd_servicepoint_status = '10000' a.'ALLSTATS LAST')).sd_servicepoint_status = '10000' OR a.Trouble Shooting ² 4th Pass  1 Lets break down the section in brackets. SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) ad_service_point a a.sd_servicepoint_status = '10003' 2 3 FROM 4 WHERE 5* OR SQL> / COUNT(*) ---------282 SQL> select * from table(dbms_xplan.NULL."SD_SERVICEPOINT_STATUS"=10000 OR "A".02 | 6 | |* 3 | INDEX RANGE SCAN| AD_SERVICE_POINT_IX5 | 2 | 282 | 282 |00:00:00. PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------SQL_ID 4rjg28wg48zv7. child number 0 ------------------------------------SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) FROM ad_service_point a WHERE a.

"SD_OCCUPIED_STATUS"=10001 AND "A".Trouble Shooting ² 5th Pass 1 SELECT 2 3 FROM 4 WHERE 5 6 7 8* SQL> / COUNT(*) ---------2070646 SQL> select * from table(dbms_xplan.sd_servicepoint_status OR ( a. PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------SQL_ID 17vy5ysvdqgxd.sd_service_type AND a.sd_servicepoint_status = '10001' ) ) Plan hash value: 185642247 ---------------------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads /*+ GATHER_PLAN_STATISTICS */ COUNT(*) ad_service_point a ( ( a.filter(("A". child number 0 ------------------------------------SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) FROM ad_service_point a WHERE ( ( a.sd_servicepoint_status = = = = = '10000' '10001' ) '10001' '10001' '10001' ) ) | PLAN_TABLE_OUTPUT ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:17.sd_occupied_status AND a.sd_service_type = '10000' AND a."SD_SERVICEPOINT_STATUS"=10001 AND ("A".NULL.sd_occupied_status = '10001' AND a.display_cursor(NULL.sd_servicepoint_status = '10001' ) OR ( a.'ALLSTATS LAST')).67 | 92450 | 9 ---------------------------------------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------2 .90 | 92450 | 92426 | |* 2 | TABLE ACCESS FULL| AD_SERVICE_POINT | 1 | 1952K| 2070K|00:00:18."SD_SERVICE_TYPE"=10000 OR ("A"."SD_SERVICE_TYPE"=10001)))) 70 .sd_service_type AND a.sd_service_type = '10001' AND a.

NULL.sd_service_type = '10000' AND a.display_cursor(NULL. child number 0 ------------------------------------SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) FROM ad_service_point a WHERE ( ( a.sd_servicepoint_status = '10001' ) ) 71 .74 | 18921 | 18859 | |* 2 | INDEX FAST FULL SCAN| AD_SERVICE_POINT_IX11 | 1 | 1679K| 1679K|00:00:01.'ALLSTATS LAST')).sd_servicepoint_status = '10001' ) ) Plan hash value: 1416185116 ---------------------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads | ---------------------------------------------------------------------------------------------------| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:04.Trouble Shooting ² 6th Pass 1 SELECT 2 3 FROM 4 WHERE 5* SQL> / COUNT(*) ---------1679148 SQL> select * from table(dbms_xplan.71 | 18 ---------------------------------------------------------------------------------------------------/*+ GATHER_PLAN_STATISTICS */ COUNT(*) ad_service_point a ( ( a. PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------SQL_ID 2qd1zzbj3qgf7.sd_service_type = '10000' AND a.

17 | 92450 | 9 ---------------------------------------------------------------------------------------------------1 72 .47 | 92450 | 92426 | |* 2 | TABLE ACCESS FULL| AD_SERVICE_POINT | 1 | 364K| 391K|00:00:18.display_cursor(NULL.sd_occupied_status = '10001' AND a.sd_service_type = '10001' 5 AND a.sd_occupied_status = '10001' 6* AND a.Trouble Shooting ² 7th Pass SELECT /*+ GATHER_PLAN_STATISTICS */ 2 COUNT(*) 3 FROM ad_service_point a 4 WHERE ( ( a. PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------SQL_ID 186k84m0t9rmx.sd_servicepoint_status = '10001' ) ) SQL> / COUNT(*) ---------391498 SQL> select * from table(dbms_xplan.'ALLSTATS LAST')).sd_service_type = '10001' AND a. child number 0 ------------------------------------SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) FROM ad_service_point a WHERE ( ( a.sd_servicepoint_status = '10001' ) ) Plan hash value: 185642247 ---------------------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads | ---------------------------------------------------------------------------------------------------| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:16.NULL.

Analysis Of 7th Pass It appears that the cardinality estimate goes awry when Oracle is combining the cardinalities of:1 2 3 4 5 6* and:1 SELECT 2 3 FROM 4 WHERE 5* SQL> / /*+ GATHER_PLAN_STATISTICS */ COUNT(*) ad_service_point a ( ( a.sd_servicepoint_status = '10001' ) ) 73 .sd_service_type = '10000' AND a.sd_servicepoint_status = '10001' ) ) SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*) FROM ad_service_point a WHERE ( ( a.sd_service_type = '10001' AND a.sd_occupied_status = '10001' AND a.

inserts. updates. If table monitoring is enabled.Analysis Of 7th Pass  To rule out statistics:  We could re-compute statistics on the AD_SERVICE_POINT table. this may ok for a test / development environment but it may not be practical to do this on a production environment on ad-hoc basis.FLUSH_DATABASE_MONITORING_INFO  Then run:- SELECT num_rows. which it should be by default you flush the monitoring information:- SQL> execute DBMS_STATS. user_tab_modifications m WHERE t. deletes FROM user_tables t.table_name AND table_name = < your table name > 74 .table_name = m. last_analyzed.

If table monitoring is enabled.table_name = m. this may ok for a test / development environment but it may not be practical to do this on a production environment on ad-hoc basis.FLUSH_DATABASE_MONITORING_INFO  Then run:- SELECT t.table_name. SUM(updates). 75 . which it should be by default you flush the monitoring information:- SQL> execute DBMS_STATS. last_analyzed / You can see from this that it wouldn¶t be that difficult to produce something similar that works for indexes.Analysis Of 7th Pass  To rule out statistics:  We could re-compute statistics on the AD_SERVICE_POINT table. SUM(inserts). last_analyzed. SUM(deletes) FROM user_tables t. user_tab_modifications m WHERE t.table_name.table_name AND timestamp > last_analyzed AND t.table_name = <your table name> GROUP BY t.

 The exact nature of the problem is that if there are predicates on the same table for which the data in the relevant columns is related. the optimizer always assumes that these are independent.Analysis Of 7th Pass  This particular issue is down to what:  people from Oracle call ³data correlation´ and what some people from outside of Oracle coin ³the predicate independence assumption´. This culminates in:=> incorrect selectivitys  => incorrect cardinality estimates => sub-optimal plans 76 .

and we assume even distribution of data ± selectivity is 1/12.200 * 1/144 = 8. . . Jonathan Lewis describes this as:³ . If I ask all the people born under Aries to raise their hands.´ ³How many people will raise their hands if I ask for all the people born under Aries and in December to raise their hands? What about all the people born under Aries in March? What about the people born under Aries in April ? According to Oracle the answer will be the same for all three questions:   Selectivity (month AND star sign) = selectivity (month) * selectivity (star sign) = 1/12 * 1/12 = 1 /144 Cardinality = 1. . there are 12 star signs. I expect to see 100 hands.Assume everyone in the audience knows which star sign they were born under . .200 / 12 = 100.Analysis Of 7th Pass  This issue has been covered in great depth by the Oracle community that focus on the CBO. . .´ 77 . cardinality is 1.5 (rounded to 8 or 9 depending on version of Oracle).

the level of sampling performed depends on the sampling level.Analysis Of 7th Pass  Solutions to the data correlation / predicate independence assumption issue include: dynamic sampling (9i onwards). this causes Oracle to sample the data being queried. 78 .  Hints to force the optimizer down the appropriate execution path. Oracle uses what is known as offline optimization to sample data and partially execute the query in order to create a profile containing cardinality scaling information.  Oracle 11g automatic tuning ?.  Extended statistics (Oracle 11g onwards) allows you to gather statistics on columns containing related data.  SQL profiles (Oracle 10g onwards).

Section 4 Summary & ³Wrap Up´ 31/01/2012 79 .

This will be covered on the remaining slides.    80 .Summary Having good technical knowledge will only get you so far. To be affective at tuning. your technical knowledge needs to be augmented with good practise and knowledge of common pit falls.

Tuning Objectives    Set clear and well defined performance goals. 81 . concurrent users. CPU / IO loading. How do you know you have met your goals without targets ?. timings. in terms of data volume. Remember that timings are ultimately what is important to the business.

Do not assume you can use the tuning advisor to solve all your ills because Oracle have mentioned this.Tuning By Magic Bullets       Do not tune using µmagic bullets¶. Etc . Do not pick up text book or article from the internet describing a performance enhancing feature and apply it blindly across an application. be aware of likely causes. !!! ONE SIZE DOES NOT ALWAYS FIT ALL !!! 82 . . . Do not use dynamic_sampling all over the place because I have mentioned this. when faced with a problem. but appreciate that one size does not fit all.

uk.co.Tuning By Hit Ratios     Hit ratios can hide a multitude of sins.oracledba. Refer to custom hit ratio from www. A piece of SQL performing meaningless work can perform lots of logical reads and hence lead to a good BCHR. Take the buffer cache hit ratio (bchr) for example. 83 .

Uday 84 . . what steps/guidelines are to be followed. while coding. .Hit Ratios According To Tom Kyte The following was posted on Ask Tom 4 December 2002 . what is a "OK" number of the LIOs (some percentage of SGA or PIO etc ?) If possible. 1). inorder to reduce/avoid LIOs ? 3). . is it possible to reduce the LIOs programatically ? 2). can you please demonstrate with an example ? (I will be using PL/SQL for coding) Thanks. You Asked If LIOs are bad. If yes.

I have a theory that systems with high cache hit ratios. Yet. that is that.cache hit. If you are joining a 1.it is very doubtful that an index should be used.000. I have no ratios for you.000 row table and trying to get 2. it must be broken". their DBA's sit there and say "well. 3) ratios STINK ( i have stronger words but this is a public forum after all ).are among the most poorly tuned systems.000. setting environmental things like sort_area_size or db_file_multi_block_read_count or the optimize_index_* parameters. if that query could be executing without doing 1. 96% -. There is no magic "good number".000.000 LIO's -.000 row table to a 2. . Massive nested loop joins are the main culprit of this.000 LIO's then we need to fix it. It should be near 100 for most systems.000 rows back -. All other ratios -.000. They are Experiencing excessive LIO's due to massive nested loop joins.Hit Ratios According To Tom Kyte And we said . . over 95. However. There is one ratio I use -. 85 .000. If you have a query that you need to execute and the best we can do is 1. tuning your SQL. It is like the stupidest ratio of them all . 1) by rewriting your SQL.soft parse ratio (the ratio of soft to hard parses). my cache hit is 99% so all is well in the world". 2) the piece of advice I have for everyone: totally and forever forget the concept that "if my query ain't using an index.forget about them.then SO BE IT.

least (1.pdf 100 * ( 1 . desired response time / actual response time ) ) 86 .jlcomp.The Fan Hit Ratio  This is a meaningful hit ratio that I first came across in a Jonathan Lewis presentation http://www.co.uk/hit_ratio.demon.

html. .com/2008/03/oraclesupport-keeps-closing-my-tar.blogspot. 87 . every feature has it¶s quirks and boundary cases under which things can break. New optimizer features are constantly introduced. . 11g will be no exception. digest and the list of bugs fixed in the optimizer .Tuning By Oracle Marketing      Oracle 11g is self tuning => tuning is dead !!! So why has Oracle produced an SQL test case packager in 11g => http://optimizermagic. When 11.0.7 comes out. Every Oracle release has bugs and flaws.1. .

Tom Kyte and Jonathan Lewis.Tuning By The Internet   Some organisations publish on the internet are more interested in dominating search engine searches and advertising their books than disseminating advice grounded on well documented test cases and evidence. 88 .g. e. Prefer web sites and µexperts¶ that provide worked examples.

histogram than a system wide parameter change. 89 . For example.Tuning By ´Problem Scopeµ    The scope of a solution to a performance problem should match the scope of the problem. index change. there are DBAs out there who when faced with a performance issue will look at adjusting obscure parameters which most DBAs in their professional careers will never have any need to touch. an issue is caused by one particular SQL statement is more likely to be resolved by something such as a new index. Yes.

Understand the ³flight envelope´ of you application. find where the time is going to first and then apply tuning techniques accordingly. pick the bottle necks off one by one. Do not blindly pick an Oracle feature conducive to good performance. Refer to Cary Milsap¶s presentation on performance and skew from Oracle Open World 2008.Tuning What You Know     Always tune what you know. 90 . how it behaves under load.

in 11g release 1 alone we have:Extended statistics  Null aware joins  Adaptive cursors  Plan base lining  A new method of calculating density   Protect yourself from this by thorough testing 91 .Tuning Is A Moving Target  The RDBMS is constantly evolving.

Understand Response Time      Response time = wait time + service time Service time = CPU time => joining. parsing etc Wait time = waiting on an event I/O. Avoid blind scatter gun techniques such as looking at your software with a monitoring tool and saying ³hey there is a lot of latching taking place. therefore that must be my problem´. 92 . work off the top waits events and drill down. contention etc Understand this basic equation and its context with your application.

when was it last serviced.  93 . The mechanic will probably ask:When did first start happening  Under what driving conditions does this happen  Does this happen consistently  Have there been any changes to your car. you say there is a performance issue with the engine.Tuning Via Flat Statistics   You get a mechanic to look at your car. it is doing 3000 Rpm at 60 Mph.

Tuning Via Flat Statistics  

The moral of this story is that a single statistic in isolation is not that useful. In order to put statistics into context you need to also understand things such as:where is most of the time going in your application ?  What is the normal performance base line or ³Flight envelope´ for your application  Is you think you are seeing strange and anomalous behaviour, what conditions does it occur under ? 

94

Know Your Tool Kit 
 

  

Performance views 10g tuning infra structure, ASH, advisors, ADDM and the time model. SQL trace, SQL extended trace, tkprof, trcsess. O/s tools, iostat, sar etc . . . DBMS_XPLAN Your tuning arsenal should consist of more than explain plan and the ability to spot full table scans.
95

Understand Key Object Statistics   

   

Histograms, frequency and height balanced, MetaLink note 72539.1. Index quality => clustering factors, many DBAs do not understand this, this gets a whole chapter in Jonathan Lewis¶s Cost Based Fundamentals book. MetaLink notes 39836.1 and 223117.1 Predicate selectivity, MetaLink note 68992.1 System statistics aux_stats$, MetaLink note 153761.1 Sample sizes Numbers of distinct values. Etc . . .
96

org Jonathan Lewis¶s blog http://jonathanlewis.Useful Internet Resources       The Oracle Real World performance group blog http:/www.doc 97 .structureddata.wordpress.com General Oracle papers.com/ Wolfgang Breitling¶s web site (a reviewer of Jonathan Lewis¶s CBO book) www.com/en/know-how-community/download-area. look out for those in particular from Chritian Antognini http://www.centrexcc.com/ The Oracle Optimizer Group Blog http://optimizermagic.trivadis.html ³The Search For Intelligent Life In The Cost-Based Optimizer´ by Anjo Kolk: http://www.evdbt.blogspot.com/SearchIntelligenceCBO.

Sign up to vote on this title
UsefulNot useful