Oracle 11g新特性:多列統計資訊(MultiColumn Statistics)
當where子句後指定了一個表的多個列條件時,最佳化器通常會將多個列的選擇性(selectivity)相乘得到where語句的選擇性,導致最佳化器做出錯誤判斷!
Oracle 11g引入了多列統計資訊概念,如果上面情況列關聯性很好,我們可以做多列統計資訊收集,讓最佳化器做出正確判斷。
例如:
SQL> select count(*) from sh.customers where CUST_STATE_PROVINCE = 'CA';
COUNT(*) ---------- 3341
SQL> select count(*) from sh.customers where CUST_STATE_PROVINCE = 'CA' and country_id=52790;
COUNT(*) ---------- 3341 SQL> select count(*) from sh.customers where CUST_STATE_PROVINCE = 'CA' and country_id=52775;
COUNT(*) ---------- 0 |
可以看出表customers的列CUST_STATE_PROVINCE、country_id是有關係的,只有多列統計資訊才能讓最佳化器知道他們的關係,從而有一個更精確的選擇性(selectiveity)。
1、建立Column Group:
DECLARE cg_name varchar2(30); BEGIN cg_name := dbms_stats.create_extended_stats(null,'customers', '(cust_state_province,country_id)'); END; / |
2、檢視column group name:
select sys.dbms_stats.show_extended_stats_name('sh','customers', '(cust_state_province,country_id)') col_group_name from dual;
COL_GROUP_NAME -------------------------------------------------------------------------------- SYS_STU#S#WF25Z#QAHIHE#MOFFMM_ |
3、刪除Column Group
exec dbms_stats.drop_extended_stats('sh','customers','(cust_state_province,country_id)'); |
4、監控Column Groups
--查詢多列統計資訊 Select extension_name, extension from user_stat_extensions where table_name='CUSTOMERS';
EXTENSION_NAME EXTENSION ------------------------------ ---------------------------------------- SYS_STU#S#WF25Z#QAHIHE#MOFFMM_ ("CUST_STATE_PROVINCE","COUNTRY_ID")
--檢視distinct數和柱狀圖使用情況 select e.extension col_group, t.num_distinct, t.histogram from user_stat_extensions e, user_tab_col_statistics t where e.extension_name = t.column_name and e.table_name = t.table_name and t.table_name = 'CUSTOMERS'; COL_GROUP NUM_DISTINCT HISTOGRAM ---------------------------------------- ------------ --------------- ("CUST_STATE_PROVINCE","COUNTRY_ID") 145 FREQUENCY |
5、收集多列統計資訊
1)收集已存在的列組統計資訊
EXEC DBMS_STATS.GATHER_TABLE_STATS('SH','CUSTOMERS',METHOD_OPT =>'FOR ALL COLUMNS SIZE AUTO'); |
2)收集新指定的列組統計資訊
EXEC DBMS_STATS.GATHER_TABLE_STATS('SH','CUSTOMERS',METHOD_OPT =>'FOR ALL COLUMNS SIZE SKEWONLY FOR COLUMNS (CUST_STATE_PROVINCE,COUNTRY_ID) SIZE SKEWONLY'); |
實驗:
1)當不使用多列統計資訊時,真實結果是3341,執行計劃是1107.
SQL> exec dbms_stats.drop_extended_stats('sh','customers','(cust_state_province,country_id)');
PL/SQL procedure successfully completed.
SQL> select count(*) from sh.customers where CUST_STATE_PROVINCE = 'CA' and country_id=52790;
COUNT(*) ---------- 3341
Execution Plan ---------------------------------------------------------- Plan hash value: 296924608
-------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 16 | 406 (1)| 00:00:05 | | 1 | SORT AGGREGATE | | 1 | 16 | | | |* 2 | TABLE ACCESS FULL| CUSTOMERS | 1107 | 17712 | 406 (1)| 00:00:05 | --------------------------------------------------------------------------------
Predicate Information (identified by operation id): ---------------------------------------------------
2 - filter("CUST_STATE_PROVINCE"='CA' AND "COUNTRY_ID"=52790)
Statistics ---------------------------------------------------------- 120 recursive calls 0 db block gets 1588 consistent gets 1454 physical reads 0 redo size 423 bytes sent via SQL*Net to client 420 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 7 sorts (memory) 0 sorts (disk) 1 rows processed |
2)當使用多列統計資訊時,真實結果是3341,執行計劃是3294.
SQL> EXEC DBMS_STATS.GATHER_TABLE_STATS('SH','CUSTOMERS',METHOD_OPT =>'FOR ALL COLUMNS SIZE SKEWONLY FOR COLUMNS (CUST_STATE_PROVINCE,COUNTRY_ID) SIZE SKEWONLY');
PL/SQL procedure successfully completed.
SQL> SQL> select count(*) from sh.customers where CUST_STATE_PROVINCE = 'CA' and country_id=52790;
COUNT(*) ---------- 3341
Execution Plan ---------------------------------------------------------- Plan hash value: 296924608
-------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 16 | 406 (1)| 00:00:05 | | 1 | SORT AGGREGATE | | 1 | 16 | | | |* 2 | TABLE ACCESS FULL| CUSTOMERS | 3294 | 52704 | 406 (1)| 00:00:05 | --------------------------------------------------------------------------------
Predicate Information (identified by operation id): ---------------------------------------------------
2 - filter("CUST_STATE_PROVINCE"='CA' AND "COUNTRY_ID"=52790)
Statistics ---------------------------------------------------------- 8 recursive calls 0 db block gets 1458 consistent gets 1454 physical reads 0 redo size 423 bytes sent via SQL*Net to client 420 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed |
3)即以上情況,使用多列統計資訊能讓最佳化器得到更準確的判斷!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27126919/viewspace-1655727/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle 11g新特性之收集多列統計資訊Oracle
- Oracle 12c新特性之檢測有用的多列統計資訊Oracle
- Oracle多列統計資訊Oracle
- 11G新特性,比較統計資訊
- 11G新特性,待定的統計資訊
- 11g新特性:Pending Statistics
- 11g新特性--pending statistics
- ORACLE 11g新特性-統計值掛起Oracle
- ORACLE19c新特性-實時統計資訊收集Oracle
- 【ORACLE新特性】11G 分割槽新特性Oracle
- ORACLE表統計資訊與列統計資訊Oracle
- Oracle 11g 新特性Oracle
- [轉] oracle統計資訊(statistics)和直方圖(histogram)Oracle直方圖Histogram
- ORACLE表統計資訊與列統計資訊、索引統計資訊Oracle索引
- Oracle 11g的多列統計(Multi Column)值(上)Oracle
- Oracle 11g的多列統計(Multi Column)值(下)Oracle
- 11g新特性--檢視錶的歷史統計資訊差異
- Oracle 11G 統計資訊TaskOracle
- oracle 11g統計資訊收集Oracle
- oracle 11g 的新特性Oracle
- Fixed Objects Statistics統計資訊收集 - 2Object
- Oracle 11g 新特性簡介Oracle
- Oracle 11g 新特性之DRCPOracle
- Oracle 11g新特性:Result CacheOracle
- Oracle 11g 新特性(轉載)Oracle
- Oracle 11g新特性之SecureFilesOracle
- 【Oracle】11g Oracle自動收集統計資訊Oracle
- 新的Oracle時間資訊特性Oracle
- Oracle多列統計資訊與直方圖對有關聯多列查詢影響Oracle直方圖
- 使用11g多列統計量提高多列group by操作估算值
- Oracle 11g手工收集表統計資訊Oracle
- Oracle 11g 統計資訊收集指令碼Oracle指令碼
- oracle 11g 待定釋出統計資訊Oracle
- ORACLE 11g 自動收集統計資訊Oracle
- oracle DG 11g新特性彙總Oracle
- ORACLE 11G新特性之列新增操作Oracle
- Oracle 11g 新特性 -- SecureFiles 說明Oracle
- oracle 11g 新特性 表壓縮Oracle