Understanding Oracle Optimizer: RBO versus CBO

Tantra Invedy UBSW Energy


List of discussion
Query execution overview Optimizer basics Understanding RBO Understanding CBO Understanding Hints Best practices

Query Execution Overview


Parse a statement <<< sql optimization 3. Execute the statement 8. Parallelize the statement 7. Bind variables <<< after sql optimization 6. Fetch rows of a query 9. Close the cursor Query Execution Overview 4 . Describe query results 4. Create a cursor 2. Define query output 5.1.

. Unique handle and particular cursor can only be opened by 1 process at a time It contains information for statement processing Programs must have an open cursor to process a SQL statement.5 Step 1: Create Cursor A cursor is a handle or name for a private SQL area.

It involves: Translation and verification Table and column check Parse lock to prevent structural changing.Step 2: Parse the Statement Parse statement from user process. Check privileges on referenced objects Determine optimal execution path Load statement into shared SQL area Route distributed statement correctly 6 .

7 .. Parsing is only necessary if there is no identical SQL in shared area A new shared SQL area is allocated and statement is parsed if identical SQL exist then this can be reused.Step 2: Parse the statement cont..

.8 SGA Buffer Cache PGA Non shareable is stored in PGA • user run time information Shared Pool Log buffer Shared pool maintains shareable parts of the cursor: • query text • Execution plan • Bind variable data types and lengths Shareable cursors saves resources from unnecessary parsing.

and data type information required to store fetched values in variables. size. it is relevant when entering dynamic queries through an OCI application The define step defines location.Steps 3 and 4: Describe and Define Describe provides information about the select list items. 9 .

Steps 5 and 6: Bind and Parallize Bind any bind values: Memory address to store data values  Values do not have to be in place yet  Allows shared SQL even though bind values may change  10 Parallize the statement  Parse stage has determined if the SQL can be parallized. . and parallel plan already built.

 Array fetch mechanism  Close the cursor .11 Steps 7 through 9 Execute: Puts values into all bind variables  Drives the SQL statement to produce the desired results  Fetch rows (for select statement only) Into defined output variables.

which is before binding variables.12 To remember …. out of scope Next what is query optimization .. . Cost of Non-sharable SQL.. Query optimization is in parsing stage.

Optimizer Basic 13 .

Two kind of Optimizer on Oracle: RBO or Rule Based Optimizer since v6  CBO or Cost Based Optimizer since v7  .14 Optimizer Basic It is an Oracle „engine‟ that would choose the most efficient method on retrieving data on a given query. The steps chosen is called execution plan.

QUERY PROCESSING Query OPTIMIZER Query Optimazation RBO / CBO 15 Parse Query rewrite Result Query Execution QEP Generation .

Parse phase simple transformation     16 Convert query to an equivalent but more efficient expression lastname like („JONES‟) >>> lastname in („AL‟. .‟WATT‟) >>> lastname=„JONES‟ lastname=„AL‟ OR lastname=„WATT‟ salary between 10 and 100 >>> salary >= 10 AND salary>=100 NOT(salary<100 and JOB IS NULL) >> salary>=100 and JOB IS NOT NULL  Transform OR into UNION ALL if the derived query is cheaper.

Query Rewrite within Optimizer View Merging Subquery Merging Transitivity (CBO only) Materialized views 17 .

(VIEW and FILTER operator on explain plan) .18 View Merging View merging rewrites queries containing views so that only base tables remain. then view must be executed separately. It is only done when a correct results is guaranteed. If it cannot be merged. or query is pushed into the view. Either view is pushed out to query.

19 Non-merge-able Views Group by clause All aggregate functions Rownum reference Start with / connect by clause All set operation (union. minus. …) Distinct (unless query also has distinct) Has join with tables in original query .

20 Sub-query Merging Merging sub-query to open up new access path and new join order Single row subqueries. (Oracle will evaluate subquery and store the result)  „IN‟ or subqueries might be converted into EXISTS or NOT EXISTS or in line view  .

emp_id = <evaluated_value>.emp_id = select … from … Select … from emp e where e. select … from courses where dev_id in (select emp_id from emp) This would be flattened or converted into a join 21 .Sub-query Merging Example Select … from emp e where e.


CBO performs transitivity as the first step in optimization. It generates additional predicates based on existing predicates. This would open more access paths. Transitivity is NOT done for join predicates.


Transitivity Example
A=5, A=B then it is concluded that B=5
A=B , B=C then oracle would NOT conclude that A=C

Understanding RBO


it chooses the path from right to left in the from clause. then it will run under CBO. it outperforms CBO . Using an ordered list of access methods and join methods on relative cost or each operation. On some complex queries. Will be removed from the Oracle database Server Normally.25 Understanding RBO Released with Oracle 6. Has a very limited input in determining access paths. If hint (except RULE hint) is supplied.

Order by on indexed columns 15. Unbounded range search on indexed columns 12. Max or Min of indexed columns 14. 5. 3. Bounded range search on indexed columns 11. Hash Cluster key. Single row by cluster join. Single row by ROWID 2. Indexed cluster key. Cluster join. 6. (cluster) 26 (cluster) (cluster) (cluster) (cluster) . 9. 8. Single row by hash cluster key with unique key.RBO ranking 1. Single-column non-unique index. 4. Composite key. 10. 7. Sort-merge join 13. Full table-scan. Single row by unique index.

starting from the last to the first. Pick the lowest score. If there were ties. and evaluate the next. then it would choose arbitrarily based on :   27 Order based on the FROM clause Age of an index . Assign scores to each of the predicates.RBO: Determining Access path First it would evaluate join predicates and filtering predicates.

Index scan will always better than table scan. regardless of the data stored in the table.Not so good things about RBO. RBO uses poor information to break frequently occurring ties. hash join. All new features require implementation of CBO. bitmap index. (can be good and most of the time is not so good) Always ranks execution plan based on relative cost in the list... Coding for the RBO is halted. . RBO has a small number of possible access 28 (it does not recognize IOT. method. which is not true. …) It will process the tables based on how they are ordered on the query.

Understanding CBO 29 .

then simple choose the lowest one .000 permutations) Evaluate the costs for each access plan. CBO uses Oracle „intelligent‟ formula to calculate the COST of a SQL statement. It examines all possible access methods (defaulted to 80. Dictionary . statistics. Constantly improving from version to version.30 Understanding CBO It uses all available information. histogram. supplied parameter setting.

chain_cnt. avg_space. high_value. num_buckets)  DBA_INDEXES (blevel. num_rows. avg_leaf_block_per_key) . avg_space_freelist_blocks)  DBA_TAB_COLUMNS or DBA_TAB_COL_STATISTICS (num_distinct.Available information to CBO 31 Database statistics (partial list …)  DBA_TABLES (num_rows. distinct_keys. clustering_factor. avg_row_len. last_analyzed. empty_blocks. low_value. sample_size. num_nulls. blocks. leaf_blocks. density.

3. or bind variables are used.3 an above         32 selectivity for relations on indexed columns .004 multiblock read factor 8 remote table average row length 100 # of blocks 100 Scan cost 13 Index levels 1 number leaf blocks/key 1 ... If there is no statistics.What if there is no statistics .009 selectivity for = on indexed columns . then CBO may use default statistics Default statistics for Oracle 7.

Available information to CBO Initialization parameters that influence CBO cost computation (partial list …) db_file_multiblock_read_count  hash_multiblock_io_count  hash_area_size  sort_area_size  bitmatp_merge_area_size  33 .

Available information to CBO Parameters affecting Cost computation      34 sort_multiblock_read_count optimizer_index_caching optimizer_index_cost_adj optimizer_percent_parallel optimizer_mode (choose defaulted to all_rows)  First_row applies a heuristic bias to the cost model to promote the use of indexes and nested loop) .

Network operations that a statement requires. CPU cost (parse) has little impact on a SQL tuning on most cases.CBO: Cost of a SQL statement 35 Cost is the estimated number of I/O. hash_multiblock_io_count. sort_area_size. While I/O is the most. Difference between logical and physical I/O It is affected by some parameters:  db_file_multiblock_read_count. bitmap_merge_area_size . hash_area_size. CPU.

875 => 13 .36 Cost of Accessing Data Table scan cost Number of blocks below HWM  Multiblock read factor (default 8)  Number of extents is also taken into consederation  Example  Cost: 103 / 8 = 12.

37 Cost of Accessing Data Index Scan Costs are based on:  Index access number or levels in the B*-tree  number of leaf blocks to examine   Consequent table lookup (optional)  number of blocks accessed Cost Fast full scan depends on db_file_multiblock_read_count .

38 Cost of Sorting Data may need to be sorted for:    Order by aggregation Join operation Sorts are typically CPU intensive. Cost depends on sort_area_size and # rows. . and can be I/O bound if the sort can not fit in memory.

39 Cost of a Join (SM) Rows from row source 1 are sorted Rows from row source 2 are then sorted by the same sort key Sorted rows from both sides are then merged Cost: sorting. reading tables. I/O for temporary segments MERGE Sort Sort Row source 1 Row source 2 .

Performance is very dependent on index on inner table Inner Loop Check for a match Nested Loop Access A (Full) Access B (ROWID) Index Access .40 Cost of a Join (NL) Outer Loop Row source 1 is scanned (outer /driving table) Each row returned drives a lookup in row source 2 (inner) Joining rows are then returned Cost: Read driving table and access on inner table.

It requires single pass for each row source . and more efficient than sorting and merging Row source 1 (build input) Row source 2 (probe) Hash table and bitmap filter in MEMORY Output rows DISK .41 Cost of a Join (HJ) In theory. The smaller row source is used to build a hash table and a bitmap The second row source is hashed and checked against the hash table The bitmap is used as a quick lookup to check if rows are in the hash table. it is the most efficient joint method.

(running out of permutation limits ?) . keep the current and the best permutation so far. Defaulted to 80. as listed in where clause predicates.42 Join Order Evaluation The initial permutation is generated by sorting the join order in ascending order of their computed cardinality. For each permutation. but sometimes it does matter.000 permutation Most of times the order will not matter. At any time. compute the cost and keep the one with the lowest cost.

ORA settings 30% 25% 20% 20% < 5% < 5% .43 Common CBO problem The skewness problem Analyzing with wrong data Mixing optimizer in joins Choosing inferior index Joining too many tables Incorrect INIT.

Using HINT 44 .

.HASH. Hints can be used to influence:    The optimization approach (RULE.... INDEX. Hint is supplied as a directive and may get ignored. ALL_ROWS.. use_hash . …use_nl.) .) The join order and method (ordered. star. Developers know more about the data. FIRST_ROWS) The access path for a table accessed (FULL . . Sometimes the optimizer may give an optimal plan.45 Why use hints . use_merge.. leading.

. (prefer SMJ)  Returns the first row of a statement as quickly as possible ===> FIRST_ROWS. (prefer NLJ)  46 Optimizer has limited information available to determine the “BEST” plan.Hint: Optimization approach CHOOSE defaulted to ALL_ROWS “BEST” plan may mean: Use minimal resource to process all rows affected by the statement ===> ALL_ROWS.

Rule. batch oriented application. session level . First_rows returns the first rows as quick as possible. new on Oracle 9i 47 . All_rows is for optimal throughput. statement level.Choosing which optimization System level. some cases outperform CBO on complex queries First_rows(xxx) or First_rows _nnn .





because view can be used in different context.52 Hint and View Not recommended to use hint inside a view. Different behavior on Merge-able view and non merge-able views .

. ROWID hint will force a checkpoint on the object Be cautious on setting parallel degree. bypassing buffer Cache Will be using ROWID hint. as there are producers and consumers processes for parallel query processes.53 Hint and Parallel query Parallel query will perform direct I/O.

Best Practices 54 .

peeking bind variables before generating explain plan . Dynamic plan based on statistics CBO behavior changes from version to version.55 Best practices: General Know how CBO and RBO choose its plan.     View / subquery merging. Set by optimizer_features_enable Know the impact of using bind variables   Do not compare with using literals Oracle 9i. transitivity FIRST_ROWS vs ALL_ROWS vs RULE Different „logic‟ from version to version.

Set INIT.ORA parameter appropriately. to stabilize explain plan. We know more information than CBO .56 Best practices: General Option to use OUTLN.

low ratio for distinct values / num rows For composite index. Unnecessarily using views. Unique scan for unique index. and sparse indexes .Best practices: Database Objects Avoid abusing views:   57 Nested views and multi view joins. ( … as select * from snp_) Use index effectively     Drop bad indexes . use all index columns Generate Histogram for skewwed indexes High water mark on often deleted tables. start with column that has more distinct values.

    58 Up-to-date statistics High water mark of a table Optimal setting for init.Best practices: Statistics Representative Statistics.ora parameter Use Histogram if necessary for skewed data Bind variables Missing object statistics on some tables Remotely join tables Know when you are using default statistics    Statistics can be exported to different environment Use DBMS_STATS instead of analyze command .

Syntax error will be ignored. Ordered hint might prevent CBO to find an alternative plan as data distribution change. Cautious with parallel degree. Understand the impact of putting HINT inside a view. separate with space . /*+ <HINT> */ Multiple HINTS can be applied.Best practices: Using HINT Use hint appropriately   59  Guide optimizer to start with the most selective predicates. and not overloading DB servers. Make sure the load is no contention. Use RULE hint on complex query if it performs better.

Most cases. order on from and where clause don't matter. CBO evaluates list of predicates (where) clause to start evaluating explain plan By default it is 80.000 permutation generated Index scan is NOT always outperform Full table scan Join involves more than 5 table often confuse CBO. In some complex query.60 Best practices: SQL Avoid using RBO techniques  Suppressing indexes ( || „‟ or + 0) on the where clause. . it may influence CBO access path.

oracle.7 documentation.1: Cost Based Optimizer . The Search for Intelligent Life in The Cost-Based Optimizer. performance tuning guide. Muthu Ramaswamy. Inside the Oracle Cost Based Optimizer. GERS Inc.com Sources from Oracle 8.1. S&P Solutions Note:35934. Richard M.Common Misconception and Issues.A Technology Change or a New Feature?. by Mark Gurry. Everything You Always wanted to know about optimizer: Student guide. Tim Gorman. Inc Cost Based Optimizer (CBO). by Oracle University. Metalink. . Slavik. Database Technologies.61 References Oracle SQL Tuning: Pocket Reference.

Q&A Thank you 62 .

06:075071472/70.7.06:07 !.7. 39.9.3/!.

0 -0:83# 39 # 39147.89070.438:2078 574.7.883-:1107..0.543943 904-0.:94:843809935.9 0.70574/:..0/0700  .  -5.3/..08808 .

9.08  ..089!7.

3     '0. 344 .4480985.080307..9. 08957.3/# .

8:-6:072073 97.7..8#& 1107039 4.47.8*# $.3 5.9305..-08   4349.-0 349025.80/4389.9:708*03.389.07843 3.8 7.3081742.-08-014700307.7.0 5003-3/.989.2..3 ..5.709:83907.941:83-3/.8  -0.3-.425. 1742.0784394..07843 $09 -459207*10.0784394.9 #$%*# $..

20907.557457.7.90 034247031472.9.-0 05. 08957.35.3 $09% #5.3 .080307.9439. 594394:80 &% 9489..

0     745-.80 -0.98 .08.9 1742835* &803/00110.-.-:83..:08.0438 &330.08 .9.9.7:83./3/008 47.94147/893.8800.9.4/. 08957.08   0890/.9..3/2:9.088.08   .

3/85.780 3/008 .:08 &36:08.82470/893..42548903/0 89.3147:36:03/0 :80.9072.799.9 ..908947.4:238 0307.74341903/0090/9.-08 .9.3:2748 47.4:239.3/0.2147800/3/008 .

.7432039 &80$*$%%$3890.9072.989.9.3/ .9089.8 .20907 &808947.741. 08957.70:83/01.989..989.422./41.989.-08 34034:.8    $9.3-0054790/94/110703903.989.989.9.0.84384209.5..088.989.8     &5 94 /. 3/.-08 8834-0.08 $9.7147800//.-0 592.9.9.8 #05708039.2130.8099314739 #02490439.7.:989.0$9.

./3807.9.0/0700 .9.07747-03470/ ..30 .08&83 % &8039./897-:943.557457.039 9413/.3 .3.9073.:94:895.9.3/9025.908 7/070/3929570.9...05.7./834 .0 $39.08:70904..3/3494.0570/.8/.42506:071950714728-09907 &3/0789.074.9415:993%38/0.90    :/04592079489. 08957.43903943 .078 &80#&3943.799902489800.

% .

3-0.90985.7.. :950%$..0 .550/ 805.

31:03.0  .3/070.431:80 .84:95071472:9..:80 489..38 %....0824709..:.9088941570/.9305.42506:07 92.39...908 070 .9.-041903.36:08  $:55708833/008  47  4390070.-08.9907 38420.3 433.:.808 47/07431742..790.35.0885.3 /01.:998 5072:9..:8094 89.:80/43 9 2.4..08$" .9430307..9  0.4/:83# 90. 08957..90/ 3/08.

05943.2 #$3..0&3.2.3 47.42 $4:7.9. #010703.-.80/ 59207  %0.147390039103%0489 .8.0  /4.0 -.0789 %0$0.7:77 .3047.-4:9459207$9:/039:/0  - 7.0$"%:33!4.3 ..7.09:33:/0 . 489.08 7.9:70 :9:#..344.80%0. $ ! $4:9438 490 489. 38/090 7..081742 7.09#010703.07934:.34408 3.943 5071472.43..8..0489..0 0.3/ 88:08 09.3.7/ $.390/9434.80/ 59207 %2 472.80/ 59207 #.80/ 59207 422438.0 .:2039.

34:  ."  %.

Sign up to vote on this title
UsefulNot useful