中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

oracle 11g 擴展統計信息extended_stats

發布時間:2020-08-09 06:15:20 來源:ITPUB博客 閱讀:346 作者:kunlunzhiying 欄目:關系型數據庫
<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&nbsp; 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&nbsp; NUM_ROWS</span><br /> <span style="font-size:16px;">TEST01&nbsp; 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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; options,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; object_name,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cardinality,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bytes,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; io_cost,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cpu_cost,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cost,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time</span><br /> <span style="font-size:16px;">&nbsp; FROM plan_table</span><br /> <span style="font-size:16px;">&nbsp;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&nbsp; OPTIONS&nbsp; OBJECT_NAME&nbsp; CARDINALITY&nbsp; BYTES&nbsp; IO_COST&nbsp; CPU_COST&nbsp; COST&nbsp; TIME</span><br /> <span style="font-size:16px;">SELECT STATEMENT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 41&nbsp; 3362&nbsp; 347&nbsp; 35338490&nbsp; 348&nbsp; 5</span><br /> <span style="font-size:16px;">&nbsp; TABLE ACCESS&nbsp; FULL&nbsp; TEST01&nbsp; 41&nbsp; 3362&nbsp; 347&nbsp; 35338490&nbsp; 348&nbsp; 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;">&nbsp; select rpad(column_name, 30, ' ') column_name,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rpad(num_distinct, 8, ' ') num_distinct,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rpad(utl_raw.cast_to_varchar2(low_value), 15, ' ') low_value,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rpad(utl_raw.cast_to_varchar2(high_value), 10, ' ') high_value,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rpad(num_nulls, 8, ' ') num_nulls,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rpad(avg_col_len, 6, ' ') avg_col_len,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rpad(density, 20, ' ') density,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; histogram</span><br /> <span style="font-size:16px;">&nbsp; from dba_tab_col_statistics</span><br /> <span style="font-size:16px;">&nbsp;where owner = 'SCOTT'</span><br /> <span style="font-size:16px;">&nbsp;&nbsp; and table_name = 'TEST01'</span><br /> <span style="font-size:16px;">&nbsp;&nbsp; and column_name&nbsp; in ('OBJECT_NAME','OBJECT_TYPE');</span><br /> <span style="font-size:16px;">/*&nbsp; &nbsp;</span><br /> <span style="font-size:16px;">COLUMN_NAME&nbsp; NUM_DISTINCT&nbsp; LOW_VALUE&nbsp; HIGH_VALUE&nbsp; NULLABLE&nbsp; NUM_NULLS&nbsp; AVG_COL_LEN&nbsp; DENSITY&nbsp; HISTOGRAM</span><br /> <span style="font-size:16px;">OBJECT_NAME&nbsp;&nbsp; 46&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CLUSTER&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XML SCHEMA&nbsp; Y&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .0217391304347826&nbsp;&nbsp;&nbsp;&nbsp; NONE</span><br /> <span style="font-size:16px;">OBJECT_TYPE&nbsp;&nbsp; 46&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CLUSTER&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XML SCHEMA&nbsp; Y&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .0217391304347826&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;</strong> &nbsp;</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;">&nbsp; begin</span><br /> <span style="font-size:16px;">&nbsp; dbms_stats.gather_table_stats('scott','test01',method_opt =&gt;'for columns (object_name,object_type)');</span><br /> <span style="font-size:16px;">&nbsp; end;</span><br /> <br /> <span style="font-size:16px;"><strong>--產生語句的執行計劃 </strong>&nbsp;</span><br /> <span style="font-size:16px;">&nbsp; 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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; options,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; object_name,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cardinality,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bytes,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; io_cost,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cpu_cost,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cost,</span><br /> <span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time</span><br /> <span style="font-size:16px;">&nbsp; FROM plan_table</span><br /> <span style="font-size:16px;">&nbsp;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&nbsp; OPTIONS&nbsp; OBJECT_NAME&nbsp; CARDINALITY&nbsp; BYTES&nbsp; IO_COST&nbsp; CPU_COST&nbsp; COST&nbsp; TIME</span><br /> <span style="font-size:16px;">SELECT STATEMENT&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;5303&nbsp;&nbsp; &nbsp;498482&nbsp;&nbsp; &nbsp;347&nbsp;&nbsp; &nbsp;36285951&nbsp;&nbsp; &nbsp;348&nbsp;&nbsp; &nbsp;5</span><br /> <span style="font-size:16px;">&nbsp; TABLE ACCESS&nbsp;&nbsp; &nbsp;FULL&nbsp;&nbsp; &nbsp;TEST01&nbsp;&nbsp; &nbsp;5303&nbsp;&nbsp; &nbsp;498482&nbsp;&nbsp; &nbsp;347&nbsp;&nbsp; &nbsp;36285951&nbsp;&nbsp; &nbsp;348&nbsp;&nbsp; &nbsp;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 />
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

博乐市| 柳林县| 积石山| 乌拉特中旗| 台中县| 蒙自县| 滁州市| 丹寨县| 河曲县| 当阳市| 马公市| 调兵山市| 汶川县| 湟源县| 新竹市| 西丰县| 静乐县| 通州市| 集安市| 正蓝旗| 沧州市| 日土县| 成都市| 永新县| 神池县| 香格里拉县| 称多县| 宣恩县| 句容市| 新闻| 保靖县| 无锡市| 得荣县| 辽源市| 留坝县| 凤城市| 峡江县| 毕节市| 白沙| 惠安县| 即墨市|