Documente Academic
Documente Profesional
Documente Cultură
by Jim Gillespie ORAMAIN Consulting Services, LLC emailid: jimgillespie@oramain.com website: www.oramain.com Phone: 608-848-8642 Cell : 608-217-4351
Follows a ranking methodology. Fifteen ranking points in RBO. Was the preferred mode. The fifteen ranking points are;
ORAMAIN Consulting Services, LLC 5
10
AVG_LEAF_BLOCKS_PER_KEY - Average number of leaf blocks in which each distinct key appears, should be 1 for unique indexes. AVG_DATA_BLOCKS_PER_KEY - Average number of blocks in the table that are pointed to by a distinct key.
ORAMAIN Consulting Services, LLC 13
GLOBAL_STATS - For partitioned indexes, YES - statistics collected as a whole, NO - statistics are estimated from statistics.
14
Prefers nested-loops.
Best for OLTP.
16
Do not be taken aback if the same query that works perfectly in one database setup is behaving badly in some other database of the same application. This would happen if the setup and statistics differ between the two databases. To prevent such behavior, you may consider using optimizer plan stability, which is covered later.
17
Cost The COST computed in CBO is a unit of expense involved with each operation. The logic as to how the cost is actually derived is not documented or made external. Moreover, this may change across releases. Cardinality The number of rows in the table or number of distinct row links in the index. The cardinality of a query is the number of rows that is expected to be returned by it.
ORAMAIN Consulting Services, LLC 18
19
20
RBO has a limited number of access methods compared to CBO. All new features require CBO. CBO is enabled to identify these features, and how to evaluate their cost. These features will be of importance for any setup; e.g. IOTs, bitmap indexes, Functionbased indexes, reverse-key indexes, Partitioning, Hash joins, Materialized views, parallel query, star joins, etc.
ORAMAIN Consulting Services, LLC 22
23
Valid values are RULE, CHOOSE, ALL_ROWS, FIRST_ROWS (_n). Default: choose. Dynamic
If set to CHOOSE. The optimizer tries to run the query in either CBO or RBO depending on the availability or unavailability of statistics. Therefore, if the tables present in the query have statistics generated on them, CBO (ALL_ROWS only) is preferred else RBO is used.
ORAMAIN Consulting Services, LLC 24
25
OPTIMIZER_MAX_PERMUTATIONS Specifies the maximum number of permutations that should be considered for queries with joins, to choose an execution plan. Influences the parse time of queries.
Set to a lower value.
27
HASH_JOIN_ENABLED
Values, true or false.
28
HASH_AREA_SIZE This specifies the maximum amount of memory in bytes to be used for a hash join per process. Oracle recommends the use of PGA_AGGREGATE_TARGET instead of this parameter from Oracle 9i. Default is 2 times SORT_AREA_SIZE. Dynamic. e.g.: hash_area_size = 2097152 Setting this to a very low number may sometimes result in the following error. ORA-6580: Hash Join ran out of memory while keeping large rows in memory.
ORAMAIN Consulting Services, LLC
29
OPTIMIZER_DYNAMIC_SAMPLING
Is used in situations where tables are not analyzed. As CBO depends heavily on statistics, the parameter tells the optimizer to sample the unanalyzed tables that are being used in a query. A level of 0 to 10 can be specified, the higher the value the more time optimizer spends in sampling.
30
CURSOR_SHARING Determines what kind of SQL statements can share the same cursor. It can be set to FORCE, SIMILAR or EXACT. FORCE will try to squeeze statements that may differ in some literals to share the same cursor. SIMILAR is somewhat the same but will try to maintain the plan optimization for identical statements. EXACT allows statements with exact identical text to share a cursor. Using FORCE may sometimes result in unexpected results. Default is exact. Dynamic e.g.: cursor_sharing = force
ORAMAIN Consulting Services, LLC
31
PGA_AGGREGATE_TARGET Introduced in Oracle 9i, this parameter specifies the aggregate PGA memory available to all server processes attached to an instance. This parameter can be set for automatic sizing of SQL working areas. It replaces other existing parameters like SORT_AREA_SIZE, BITMAP_MERGE_AREA_SIZE and HASH_AREA_SIZE. Default is 0, automatic memory management is off. Dynamic. It can be set to a value between 10 MB to 4096 GB-1, depending on the setup requirement. Determine how much memory you have, subtract the SGA and give the rest to PGA, if possible.
ORAMAIN Consulting Services, LLC 32
The following parameters changed the default from false to true when upgrading from 8i to 9i. They may have impact on response time. Negative or positive.
ORAMAIN Consulting Services, LLC
33
34
35
36
37
Such expressions were mainly used in RBO to prevent the use of indexes.
38
This parameter enables join permutation optimization. New ordering directives have been added to CBO for better processing of joins, setting this parameter will allow use of these directives.
ORAMAIN Consulting Services, LLC 39
This enables un-nesting of correlated sub-queries. Such queries may undergo MERGE join operations.
40
41
42
Default is false.
43
Here we highlight a number of key points to consider when moving to CBO. In addition, we highlight a number of good maintenance practices. Tuning in CBO is an ongoing process and proper analysis should be done. You may encounter scenarios specific to your environment that are not mentioned here. Make it a point to refer to the documentation and check with Oracle support for any kind of anomalies.
44
OPTIMIZER_MODE
FIRST_ROWS(_n) for OLTP.
45
SQL> exec dbms_stats.set_table_stats(ownname => 'SYS', tabname => EMP', numrows => 3000, numblks => 300, avgrlen => 50); PL/SQL procedure successfully completed.
SQL> select num_rows, blocks, avg_row_len, temporary, user_stats from dba_tables where table_name = 'EMP'; NUM_ROWS ---------3000 BLOCKS AVG_ROW_LEN T USE ---------- ----------- --300 50 Y YES
ORAMAIN Consulting Services, LLC 47
48
Most DBAs rely on scripts. These may be outdated. For example, include columns such as LAST_ANALYZED, MONITORING, GLOBAL_STATS and USER_STATS in scripts that look at the object information. Modify your tuning scripts to find out in what mode the database, session or particular queries are running.
49
Your Technical Documentation Guidelines (if you have one) that developers rely on for standards. Liaise with the complete team to update your conventions. In RBO, the last table in the where clause is the driver. The first table is the driver in CBO.
50
Avoid RBO style coding techniques. Techniques used to prevent the use of indexes in RBO should be avoided. CBO has advanced features such as function-based and bitmap indexes. Control processing of queries with proper where clauses and hints.
51
Using stored outlines tells optimizer to consider the execution path specified explicitly.
52
CBO will give more preference to hints than to the statistics present. But this doesnt gaurantee the hints will be used.
53
54
FGAC adds additional predicates to an existing query that may sometimes result in a change of execution plan.
Test your queries with these additional predicates. Make use of hints to direct optimizer to do what is needed.
55
56
57
60
61
62
65
66
SQL> exec dbms_stats.gather_table_stats (ownname => SCOTT', tabname => 'EMP', estimate_percent => 5, cascade => true);
ORAMAIN Consulting Services, LLC
67
e.g.: Estimate statistics in parallel, the following uses 8 threads to complete the task.
SQL> exec dbms_stats.gather_table_stats (ownname => 'SCOTT', tabname => 'EMP_TRX', estimate_percent => 5, degree => 8); When the above process is running; select * from v$px_process; produces -
68
SERV|STATUS P000 | IN USE P001 | IN USE P002 | IN USE P003 | IN USE P004 | IN USE P005 | IN USE P006 | IN USE P007 | IN USE 8 rows selected.
| | | | | | | | |
|SID| SERIAL# | 7| 50586 | 60| 51561 | 17| 2694 | 30| 39243 | 74| 11017 | 48| 4253 | 76| 17 | 68| 1285
69
Call syntax dbms_stats.gather_index_stats(ownname, indname, partname, estimate_percent, stattab, statid, statown); e.g.: SQL> exec dbms_stats.gather_index_stats (ownname => 'SCOTT', indname => EMP_IDX');
70
Call Syntax dbms_stats.gather_schema_stats(ownname, estimate_percent, block_sample, method_opt, degree, granularity, cascade, stattab, statid, options, objlist, statown);
71
GATHER AUTO - same as STALE but will include objects without any statistics.
objlist - table of type DBMS_STATS.OBJECTTAB, returns an empty or stale list. ORAMAIN Consulting Services,
LLC 73
SQL> exec dbms_stats.gather_schema_stats(ownname => 'SCOTT', estimate_percent => 5, cascade => true, options => 'GATHER');
74
75
Call Syntax
dbms_stats.gather_database_stats(estimate_percent, block_sample, method_opt, degree, granularity, cascade, stattab, statid, options, objlist, statown); In 8i this will generate statistics for the SYS schema. In 9i it will not.
77
Call Syntax
dbms_stats.delete_table_stats (ownname, tabname, partname, stattab, statid, cascade_parts, cascade_columns, cascade_indexes, statown);
78
Parameters
cascade_parts - delete statistics for all partitions (partname should be null). cascade_columns - delete column statistics. Default is true. cascade_indexes - delete index statistics. Default is true.
79
e.g.: Delete statistics for a table and its columns and indexes.
SQL> exec dbms_stats.delete_table_stats (ownname => 'SCOTT', tabname => 'EMP'); e.g.: Delete statistics for table only. Column and index statistics will be preserved.
SQL> exec dbms_stats.delete_table_stats (ownname => 'SCOTT', tabname => 'EMP', cascade_columns => false, cascade_indexes => false);
ORAMAIN Consulting Services, LLC
80
Call Syntax
dbms_stats.delete_column_stats(ownname, tabname, colname, partname, stattab, statid, cascade_parts, statown); e.g.: Deleting statistics for one column. SQL> exec dbms_stats.delete_column_stats (ownname => SCOTT', tabname => 'EMP', colname => ENAME');
81
Call Syntax
dbms_stats.delete_index_stats(ownname, indname, partname, stattab, statid, cascade_parts, statown); e.g.: Deleting index statistics. SQL> exec dbms_stats.delete_index_stats (ownname => 'SCOTT', indname => 'EMP_N1');
82
Call Syntax
dbms_stats.delete_schema_stats(ownname, stattab, statid, statown); e.g.: Deleting statistics for schema SCOTT. SQL> exec dbms_stats.delete_schema_stats(SCOTT');
83
Call Syntax dbms_stats.delete_schema_stats(ownname, stattab, statid, statown); e.g.: Deleting statistics for schema SCOTT.
SQL> exec dbms_stats.delete_schema_stats(SCOTT');
DBMS_STATS.DELETE_DATABASE_STATS
Call Syntax dbms_stats.delete_database_stats(stattab, statid, statown);
ORAMAIN Consulting Services, LLC 84
Call Syntax
dbms_stats.set_table_stats(ownname, tabname, partname, stattab, statid, numrows, numblks, avgrlen, flags, statown); Parameters numrows - number of rows. numblks - blocks in the table. avgrlen - average row length. flags - currently for internal use only.
ORAMAIN Consulting Services, LLC
85
e.g.: SQL> exec dbms_stats.set_table_stats (ownname => 'SCOTT', tabname => EMP, numrows => 12422, numblks => 100, avgrlen => 124);
SQL> select owner, num_rows, blocks, avg_row_len from dba_tables where table_name = EMP; OWNER SCOTT | NUM_ROWS| BLOCKS|AVG_ROW_LEN | 12422| 100| 124
ORAMAIN Consulting Services, LLC
86
ALSO
dbms_stats.set_column_stats
dbms_stats.set_index_stats
87
Call syntax
dbms_stats.get_table_stats(ownname, tabname, partname, stattab, statid, numrows, numblks, avgrlen, statown);
88
ALSO
dbms_stats.get_column_stats
dbms_stats.get_index_stats
90
DBMS_STATS has routines for gathering statistics and storing them outside the dictionary. This does not influence the optimizer. Most of the procedures in this package have three common parameters - STATID, STATTAB and STATOWN that are related to user processing of statistics.
Advantages of this feature: 1. Estimated statistics at different percentages could be stored and used for testing. 2. Statistics generated on one database could be transferred to another database.
ORAMAIN Consulting Services, LLC
91
DBMS_STATS.CREATE_STAT_TABLE
Call syntax
92
DBMS_STATS.CREATE_STAT_TABLE (cont.)
e.g.: creating a user statistics table.
SQL> exec dbms_stats.create_stat_table (ownname => 'SYS', stattab => 'STAT_AT_5PC', tblspace => 'SYSTEM'); The table looks like this.
Desc stat_at_5pc
ORAMAIN Consulting Services, LLC
93
DBMS_STATS.CREATE_STAT_TABLE (cont.) Name Null? Type ----------------------------- ----------------------STATID VARCHAR2(30) TYPE CHAR(1) VERSION NUMBER FLAGS NUMBER C1 VARCHAR2(30) C2 VARCHAR2(30) C3 VARCHAR2(30) C4 VARCHAR2(30) C5 VARCHAR2(30)
ORAMAIN Consulting Services, LLC 94
DBMS_STATS.CREATE_STAT_TABLE (cont.) Name Null? Type ----------------------------- ----------------------N1 NUMBER N2 NUMBER N3 NUMBER N4 NUMBER N5 NUMBER N6 NUMBER N7 NUMBER N7 NUMBER N8 NUMBER
ORAMAIN Consulting Services, LLC 95
DBMS_STATS.CREATE_STAT_TABLE (cont.)
Name ---------------------N9 N10 N11 N12 D1 R1 R2 CH1 Null? Type -------- ----------------------NUMBER NUMBER NUMBER NUMBER DATE RAW(32) RAW(32) VARCHAR2(1000)
ORAMAIN Consulting Services, LLC 96
DBMS_STATS.EXPORT_TABLE_STATS
Retrieves table statistics for a particular table and puts it in the user statistics table. Call syntax dbms_stats.export_table_stats(ownname, tabname, partname, stattab, statid, cascade, statown);
97
DBMS_STATS.EXPORT_TABLE_STATS (cont.)
e.g.: exporting EMP stats for testing purpose, including table and indexes. SQL> exec dbms_stats.export_table_stats (ownname => SCOTT', tabname => EMP', stattab => 'STAT_AT_5PC', cascade => true, statown => SYS');
98
DBMS_STATS.EXPORT_COLUMN_STATS
Call syntax
DBMS_STATS.EXPORT_SCHEMA_STATS
Call syntax dbms_stats.export_schema_stats(ownname, stattab, statid, statown); DBMS_STATS.EXPORT_DATABASE_STATS
Call syntax
dbms_stats.export_database_stats(stattab, statid, statown);
ORAMAIN Consulting Services, LLC
100
DBMS_STATS.IMPORT_TABLE_STATS
Retrieves statistics for a table from a user statistics table and stores it in dictionary. Call syntax dbms_stats.import_table_stats(ownname, tabname, partname, stattab, statid, cascade, statown);
101
DBMS_STATS.IMPORT_TABLE_STATS (cont.)
e.g.: importing statistics for table emp, including column and indexes. SQL> exec dbms_stats.import_table_stats (ownname => 'SCOTT', tabname => EMP', stattab => 'STAT_AT_5PC', cascade => true, statown => 'SYS');
102
DBMS_STATS.IMPORT_COLUMN_STATS
Call syntax
DBMS_STATS.IMPORT_SCHEMA_STATS
Call syntax
DBMS_STATS.DROP_STAT_TABLE
Drops a user statistics table.
Call syntax
dbms_stats.drop_stat_table(ownname, stattab);
Used to automate the updating of statistics as tables are updated. When enabled for a table, Oracle monitors the DML changes (including truncates) being done on the table and maintains the details in the SGA. After time SMON wakes up and post the information in the dictionary. In Oracle 9i, this time is 15 minutes. In 8i it is 3 hours. Enable with Alter table, Create table or DBMS_STATS.
106
e.g.: Monitoring option for table EMP. SQL> exec dbms_stats.gather_table_stats(SCOTT', EMP')
107
Statistics on tables that had > 10% modifications can be seen in the DBA_TAB_MODIFICATIONS view.
SQL> select * from dba_tab_modifications; no rows selected --DML activities were carried on the table. SQL> select table_owner, table_name, inserts, updates, deletes, timestamp, truncated from dba_tab_modifications;
ORAMAIN Consulting Services, LLC 108
TABLE_OWNER TABLE_NAME INSERTS UPDATES DELETES TIMESTAMP TRU ----------- ---------- ------- ------- ------- --------- --SCOTT EMP 1028577 0 19999 19-SEP-03 NO
SQL> exec dbms_stats.gather_schema_stats (ownname => SCOTT', options => 'GATHER STALE'); SQL> select * from dba_tab_modifications;
no rows selected
109
Use on tables that are being changed significantly and optimizer results fluctuate significantly.
110
Clues or directives that will assist the optimizer in choosing an execution plan.
Guide the Optimizer to do things in a certain way--the way we would like a statement to run. Not orders but directives to the optimizer.
111
Multiple hints can be provided in a single comment for a statement, each separated with spaces.
Meant for DML statements. Are not case sensitive. If a wrong or invalid hint is provided, the optimizer ignores it and continues with the execution of the statement. The optimizer will not notify the user about such hints.
112
Hints are CBO features. Using them in RBO setup will force the queries to run in cost mode. The exception to this is the RULE hint that invokes the RBO for executing a statement. Hints can be used to influence the mode of the optimizer, the access path, the join order, the join method used etc..
113
SQL statements that are executed directly may behave differently when executed from within PL/SQL. Make use of hints in such cases to achieve the required results. Table hints can be provided with the table name. . If an alias name is provided, use it instead of the table name.
Avoid the use of schema name along with the table name in hints, even if they appear in the FROM clause. Using aliases is a safe bet.
114
As stated by Oracle Documentation, the use of hints involves extra code that must be managed, checked and controlled. Use hints to tame queries that execute with sub-optimal execution plans, but take care to provide the right access paths.
Here is a list of the most widely used hints: ALL_ROWS : for good throughput and resource utilization. FIRST_ROWS : for good response time. RULE : Use rule-based optimization rather than cost. CHOOSE : Decide on rule or cost optimization based on the existence of statistics.
ORAMAIN Consulting Services, LLC 115
FULL : for doing a full table scan on the table. HASH : hash scan, applies only for clustered tables (do not confuse with HASH join). ROWID : table scan by rowid. CLUSTER : cluster scan, applies only for clustered tables. INDEX : index scan, specify the table and the index name. INDEX_ASC : for range scan, scan index in ascending order of values. INDEX_DESC : for range scan, scan index in descending order of values. INDEX_JOIN : use index join as an access path. Two indexes could be joined to return the required values.
ORAMAIN Consulting Services, LLC 116
INDEX_FFS : perform a fast full scan on the index rather than on the table. NO_INDEX : avoid the use of the specified index or all indexes. INDEX_COMBINE : explicitly choose a bitmap access path, make use of bitmap indexes. ORDERED : access and join tables in the order mentioned in the FROM clause, left to right.
117
USE_NL : use Nested Loop for joining tables. USE_HASH : use Hash joins. USE_MERGE : use Sort-Merge joins. The optimizer hints ALL_ROWS, FIRST_ROWS, RULE and CHOOSE affect the Optimizer mode for executing the query, irrespective of what is set at session level. The RULE Hint causes the Optimizer to use rule based optimization to choose the execution path. This is an instant solution for queries that ran perfectly in RBO but have slowed down in CBO.
ORAMAIN Consulting Services, LLC 118
e.g.: improving a queries response time. select /*+ first_rows */ trx_value from jnc_rtl_sales_iface_hdr where trx_no = 1211;
e.g.: Full table scan directive. select /*+ full(a) */ a.shop_no, a.subinventory_code, b.item_code from jnc_shop_mapping a, jnc_rtl_sales_iface_hdr b where b.shop_no = a.shop_no;
119
One issue that has always been in doubts is whether to generate statistics for SYS schema. Generating statistics for dictionary tables owned by SYS is not recommended in Oracle 8i. The dictionary views that reference the SYS tables execute efficiently with the Rule Based Optimizer.
You may generate statistics in Oracle 9i but you will have to evaluate this option for your setup. As per a note I came across, Oracle does not perform any regression testing with dictionaries analyzed and there may be a possibility of performance issues. Oracle 10 and above would require statistic generation for SYS schema as RBO will be desupported.
ORAMAIN Consulting Services, LLC 120
122
Same as RBO.
Use sqltrace with TKPROF.
Use Explain Plan. Two scripts, UTLXPLS.SQL (serial) and UTLXPLP.SQL (parallel executions), are provided by oracle to show the formatted execution plans.
Use autotrace. Set timing on.
ORAMAIN Consulting Services, LLC 123
124
125
- INITORA parameter CREATE_STORED_OUTLINE = TRUE - schema needs CREATE ANY OUTLINE privilege.
- see the CREATE OUTLINE statement in the ORACLE9i SQL Reference.
126
128