oracle 11g 擴充套件統計資訊extended_stats
<span style="font-size:16px;"><strong>oracle 11g在統計資訊收集方面增加了擴充套件統計資訊的特性,它可以收集一個表中相關列上的統計資訊,也可以收集函式表示式上的</strong></span><br />
<span style="font-size:16px;"><strong>統計資訊.使選擇率,成本的估計更加準確,也更容易走正確的執行計劃.在相關列上收集統計資訊,好處還是很明顯的.例如兩列在邏輯</strong></span><br />
<span style="font-size:16px;"><strong>上有一定的關係,但如果只是對這兩個列單獨做統計資訊的收集,根據多條件的選擇率計算{(A AND B的選擇率為:OPSEL[a]*OPSEL[b]);</strong></span><br />
<span style="font-size:16px;"><strong>(A OR B 的選擇率為:OPSEL[a]+OPSEL[b]-OPSEL[a]OPSEL[b]);(NOT A的選擇率為:1-OPSEL[a])},估算出來的選擇率就可能偏差很大.</strong></span><br />
<br />
<span style="font-size:16px;"><strong>以下測試:</strong></span><br />
<strong>DB Version:11.2.0.4</strong><br />
<span style="font-size:16px;"><strong>----產生測試資料</strong></span><br />
<span style="font-size:16px;">drop table scott.test01 purge;</span><br />
<span style="font-size:16px;">create table scott.test01</span><br />
<span style="font-size:16px;">as select * from dba_objects;</span><br />
<br />
<span style="font-size:16px;"><strong>--把object_name 更新為和object_type一樣,用於測試.</strong></span><br />
<span style="font-size:16px;">update scott.test01</span><br />
<span style="font-size:16px;">set object_name=object_type;</span><br />
<span style="font-size:16px;">commit;</span><br />
<br />
<span style="font-size:16px;"><strong>1.收集單列統計資訊,檢視執行計劃</strong></span><br />
<span style="font-size:16px;"><strong>--收集單列統計資訊</strong></span><br />
<span style="font-size:16px;">begin</span><br />
<span style="font-size:16px;">dbms_stats.gather_table_stats('scott','test01');</span><br />
<span style="font-size:16px;">end;</span><br />
<span style="font-size:16px;"><strong>--檢視錶的行數</strong></span><br />
<span style="font-size:16px;">select table_name,num_rows from dba_tables</span><br />
<span style="font-size:16px;">where owner = 'SCOTT' and table_name = 'TEST01';</span><br />
<span style="font-size:16px;">/*</span><br />
<span style="font-size:16px;">TABLE_NAME NUM_ROWS</span><br />
<span style="font-size:16px;">TEST01 87212</span><br />
<span style="font-size:16px;">*/</span><br />
<span style="font-size:16px;"><strong>--產生語句的執行計劃</strong></span><br />
<span style="font-size:16px;">explain plan for select * from scott.test01 where object_name='INDEX' and object_type='INDEX';</span><br />
<br />
<span style="font-size:16px;">SELECT lpad(' ', 2 * (LEVEL - 1)) || operation operation,</span><br />
<span style="font-size:16px;"> options,</span><br />
<span style="font-size:16px;"> object_name,</span><br />
<span style="font-size:16px;"> cardinality,</span><br />
<span style="font-size:16px;"> bytes,</span><br />
<span style="font-size:16px;"> io_cost,</span><br />
<span style="font-size:16px;"> cpu_cost,</span><br />
<span style="font-size:16px;"> cost,</span><br />
<span style="font-size:16px;"> time</span><br />
<span style="font-size:16px;"> FROM plan_table</span><br />
<span style="font-size:16px;"> START WITH id = 0</span><br />
<span style="font-size:16px;">CONNECT BY PRIOR id = parent_id;</span><br />
<span style="font-size:16px;">/*</span><br />
<span style="font-size:16px;">OPERATION OPTIONS OBJECT_NAME CARDINALITY BYTES IO_COST CPU_COST COST TIME</span><br />
<span style="font-size:16px;">SELECT STATEMENT 41 3362 347 35338490 348 5</span><br />
<span style="font-size:16px;"> TABLE ACCESS FULL TEST01 41 3362 347 35338490 348 5</span><br />
<span style="font-size:16px;">*/</span><br />
<span style="font-size:16px;"><strong>這裡可以看到,估算的返回行數是41,顯然和實際相差很遠</strong></span><br />
<span style="font-size:16px;">rollback;</span><br />
<br />
<span style="font-size:16px;"><strong>--行數估算</strong></span><br />
<span style="font-size:16px;"> select rpad(column_name, 30, ' ') column_name,</span><br />
<span style="font-size:16px;"> rpad(num_distinct, 8, ' ') num_distinct,</span><br />
<span style="font-size:16px;"> rpad(utl_raw.cast_to_varchar2(low_value), 15, ' ') low_value,</span><br />
<span style="font-size:16px;"> rpad(utl_raw.cast_to_varchar2(high_value), 10, ' ') high_value,</span><br />
<span style="font-size:16px;"> rpad(num_nulls, 8, ' ') num_nulls,</span><br />
<span style="font-size:16px;"> rpad(avg_col_len, 6, ' ') avg_col_len,</span><br />
<span style="font-size:16px;"> rpad(density, 20, ' ') density,</span><br />
<span style="font-size:16px;"> histogram</span><br />
<span style="font-size:16px;"> from dba_tab_col_statistics</span><br />
<span style="font-size:16px;"> where owner = 'SCOTT'</span><br />
<span style="font-size:16px;"> and table_name = 'TEST01'</span><br />
<span style="font-size:16px;"> and column_name in ('OBJECT_NAME','OBJECT_TYPE');</span><br />
<span style="font-size:16px;">/* </span><br />
<span style="font-size:16px;">COLUMN_NAME NUM_DISTINCT LOW_VALUE HIGH_VALUE NULLABLE NUM_NULLS AVG_COL_LEN DENSITY HISTOGRAM</span><br />
<span style="font-size:16px;">OBJECT_NAME 46 CLUSTER XML SCHEMA Y 0 9 .0217391304347826 NONE</span><br />
<span style="font-size:16px;">OBJECT_TYPE 46 CLUSTER XML SCHEMA Y 0 9 .0217391304347826 NONE</span><br />
<span style="font-size:16px;">*/</span><br />
<span style="font-size:16px;"><strong>估算的返回行數是41,是由兩個列的density相乘再乘以表的行數得到,.0217391304347826*.0217391304347826*87212=41.2155009451796=41</strong></span><br />
<span style="font-size:16px;"><strong> </strong> </span><br />
<span style="font-size:16px;"><strong>2.收集多列擴充套件統計資訊,檢視執行計劃</strong></span><br />
<span style="font-size:16px;"><strong>--收集多列擴充套件統計資訊</strong></span><br />
<span style="font-size:16px;"> begin</span><br />
<span style="font-size:16px;"> dbms_stats.gather_table_stats('scott','test01',method_opt =>'for columns (object_name,object_type)');</span><br />
<span style="font-size:16px;"> end;</span><br />
<br />
<span style="font-size:16px;"><strong>--產生語句的執行計劃 </strong> </span><br />
<span style="font-size:16px;"> explain plan for select * from scott.test01 where object_name='INDEX' and object_type='INDEX';</span><br />
<br />
<span style="font-size:16px;">SELECT lpad(' ', 2 * (LEVEL - 1)) || operation operation,</span><br />
<span style="font-size:16px;"> options,</span><br />
<span style="font-size:16px;"> object_name,</span><br />
<span style="font-size:16px;"> cardinality,</span><br />
<span style="font-size:16px;"> bytes,</span><br />
<span style="font-size:16px;"> io_cost,</span><br />
<span style="font-size:16px;"> cpu_cost,</span><br />
<span style="font-size:16px;"> cost,</span><br />
<span style="font-size:16px;"> time</span><br />
<span style="font-size:16px;"> FROM plan_table</span><br />
<span style="font-size:16px;"> START WITH id = 0</span><br />
<span style="font-size:16px;">CONNECT BY PRIOR id = parent_id;</span><br />
<span style="font-size:16px;">/*</span><br />
<span style="font-size:16px;">OPERATION OPTIONS OBJECT_NAME CARDINALITY BYTES IO_COST CPU_COST COST TIME</span><br />
<span style="font-size:16px;">SELECT STATEMENT 5303 498482 347 36285951 348 5</span><br />
<span style="font-size:16px;"> TABLE ACCESS FULL TEST01 5303 498482 347 36285951 348 5</span><br />
<span style="font-size:16px;">*/</span><br />
<span style="font-size:16px;"><strong>這裡可以看到,估算的返回行數是5303,已經基本上和實際返回行數相近.</strong></span><br />
<br />
<span style="font-size:16px;"><strong>PS:</strong></span><br />
<span style="font-size:16px;"><strong>1.擴充套件統計資訊的收集,可以用select dbms_stats.create_extended_stats('scott','test01','(object_name,object_type)')from dual</strong></span><br />
<span style="font-size:16px;"><strong>建立擴充套件統計列,然後dbms_stats.gather_table_stats('scott','test01')收集統計資訊,也可以直接在</strong></span><br />
<span style="font-size:16px;"><strong>dbms_stats.gather_table_stats中的method_opt屬性同時建立擴充套件統計又收集統計資料.</strong></span><br />
<span style="font-size:16px;"><strong>2.oracle 11g不僅可以收集多列擴充套件統計資訊,還可以收集函式和表示式的擴充套件統計資訊.</strong></span><br />
<br />
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28211342/viewspace-2146447/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle優化案例-擴充套件統計資訊(十四)Oracle優化套件
- Oracle 11g對AWR的擴充套件Oracle套件
- ORACLE 擴充套件Oracle套件
- Oracle 12C 擴充套件統計資訊是否自動收集? (文件 ID 2102495.1)Oracle套件
- oracle logfile 擴充套件Oracle套件
- [外掛擴充套件]通用網站統計套件網站
- kotlin 擴充套件(擴充套件函式和擴充套件屬性)Kotlin套件函式
- windows系統磁碟擴容/擴充套件Windows套件
- WCF擴充套件:行為擴充套件Behavior Extension套件
- Oracle 11G 統計資訊TaskOracle
- oracle 11g統計資訊收集Oracle
- 【Kotlin】擴充套件屬性、擴充套件函式Kotlin套件函式
- aix擴充套件檔案系統AI套件
- aix 擴充套件檔案系統AI套件
- [外掛擴充套件]百度統計外掛套件
- 用Shell擴充套件實現原始碼統計程式套件原始碼
- Sanic 擴充套件套件
- 擴充套件工具套件
- 擴充套件歐幾里得套件
- DOM擴充套件套件
- 擴充套件ACL套件
- Lua擴充套件套件
- 照片擴充套件套件
- 擴充套件篇套件
- disable or 擴充套件套件
- 擴充套件表套件
- Mybatis擴充套件MyBatis套件
- centos安裝php的oracle擴充套件CentOSPHPOracle套件
- JMeter 擴充套件開發:擴充套件 TCP 取樣器JMeter套件TCP
- 【Oracle】11g Oracle自動收集統計資訊Oracle
- 學習PHP中統計擴充套件函式的使用PHP套件函式
- PHP 系統樹圖擴充套件元件PHP套件元件
- OPENWRT擴充套件系統到U盤套件
- 擴充套件系統的磁碟空間套件
- Linux 檔案系統擴充套件Linux套件
- ?用Chrome擴充套件管理器, 管理你的擴充套件Chrome套件
- ASP.NET Core擴充套件庫之Http通用擴充套件ASP.NET套件HTTP
- php7安裝redis擴充套件和memcache擴充套件PHPRedis套件