溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微課sql優(yōu)化(2)-為什么需要收集統(tǒng)計(jì)信息

發(fā)布時(shí)間:2020-08-08 16:55:50 來(lái)源:ITPUB博客 閱讀:275 作者:orastar 欄目:關(guān)系型數(shù)據(jù)庫(kù)

1、為什么需要收集統(tǒng)計(jì)信息


Cost-based optimizer (CBO),Based on object statistics,優(yōu)化器統(tǒng)計(jì)信息描述數(shù)據(jù)庫(kù)中的對(duì)象的詳細(xì)信息, 查詢優(yōu)化器使用這些統(tǒng)計(jì)信息為每個(gè)SQL語(yǔ)句選擇最佳執(zhí)行計(jì)劃。
數(shù)據(jù)庫(kù)將優(yōu)化器統(tǒng)計(jì)信息存儲(chǔ)在數(shù)據(jù)字典中。您可以使用數(shù)據(jù)字典視圖訪問這些統(tǒng)計(jì)信息。
由于數(shù)據(jù)庫(kù)中的對(duì)象可能會(huì)不斷更改,因此必須定期更新統(tǒng)計(jì)信息,以便準(zhǔn)確描述這些對(duì)象。Oracle數(shù)據(jù)庫(kù)自動(dòng)維護(hù)優(yōu)化器統(tǒng)計(jì)信息。
11g:  Scheduled Maintenance Window Times , In 11g daily maintenance windows are provided. by default these are defined as :
Weeknights: Starts at 10 p.m. and ends at 2 a.m.
Weekends: Starts at 6 a.m. is 20 hours long.
10g: With Oracle Database 10g the default maintenance window is configured to cover the following periods:
10 pm to 6 am every weekday
All weekend (Friday 10 pm to Monday 6 am)
可以使用該DBMS_STATS軟件包手動(dòng)維護(hù)優(yōu)化器統(tǒng)計(jì)信息。

2、哪些表需要收集統(tǒng)計(jì)信息


自動(dòng)統(tǒng)計(jì)任務(wù)能滿足大部分?jǐn)?shù)據(jù)庫(kù)的業(yè)務(wù)需求,但存在特殊情況需要手工收集統(tǒng)計(jì)信息,
1、業(yè)務(wù)表數(shù)據(jù)被delete、truncate、impdp/imp大批量數(shù)據(jù)變理時(shí)。
2、數(shù)據(jù)變更量超過10%時(shí)。
查詢哪些表需要收集統(tǒng)計(jì)信息
col table_name for a30
col object_type for a30
select owner, table_name, object_type, stale_stats, to_char(LAST_ANALYZED,'yyyy-mm-dd hh34:mi') last_analyzed
from dba_tab_statistics
where  owner = 'HT'
and (stale_stats = 'YES' or last_analyzed is null);

3、練習(xí)題1、測(cè)試統(tǒng)計(jì)信息閾值: 變更量超過10%


  • 創(chuàng)建測(cè)試表: 
create table ht.test as select * from dba_objects;
  • 收集統(tǒng)計(jì)信息:
exec DBMS_STATS.GATHER_TABLE_STATS(ownname => 'HT',tabname => 'TEST',method_opt => 'for all columns size repeat',no_invalidate => FALSE,degree => 4,cascade => TRUE);
  • 刪除10%數(shù)據(jù):
SQL> DELETE FROM ht.TEST WHERE ROWNUM<=(select count(1) from ht.TEST)*0.1;
8632 rows deleted.
SQL> commit;
Commit complete.
exec dbms_stats.flush_database_monitoring_info;  --刷新變更信息
  • 查看數(shù)據(jù)變更信息
set line 200
col table_owner for a10
col table_name for a30
col pname for a20
col spname for a20
select table_owner,table_name,partition_name pname,subpartition_name spname,inserts,updates,deletes from dba_tab_modifications
where table_owner='HT';
TABLE_OWNE      TABLE_NAME   PNAME         SPNAME      INSERTS          UPDATES      DELETES
------ --- --- --- ----    ---------------- -----------   --------------    -- -- ----------    -- ----------     -- -- -- ----------
HT                        TEST                                                      0                0                8632
  • 查看統(tǒng)計(jì)信息是否過期
col table_name for a30
col object_type for a30
COL last_analyzed for a20
select owner, table_name, object_type, stale_stats, to_char(LAST_ANALYZED,'yyyy-mm-dd hh34:mi') last_analyzed
from dba_tab_statistics
where  owner = 'HT' AND TABLE_NAME='TEST';
OWNER      TABLE_NAME           OBJECT_TYPE          STALE_STA     LAST_ANALYZED
---------- ---------------------- ------------------------------ ---------           --------------------
HT               TEST                    TABLE                       NO              2017-08-18 21:26
  • 再刪除一行數(shù)據(jù):
DELETE FROM ht.TEST WHERE ROWNUM=1;
exec dbms_stats.flush_database_monitoring_info;  --刷新變更信息
  • 再次查看統(tǒng)計(jì)信息是否過期
col table_name for a30
col object_type for a30
COL last_analyzed for a20
select owner, table_name, object_type, stale_stats, to_char(LAST_ANALYZED,'yyyy-mm-dd hh34:mi') last_analyzed
from dba_tab_statistics
where  owner = 'HT' AND TABLE_NAME='TEST';

4、小結(jié)


哪些情況需要從新收集統(tǒng)計(jì)信息,
1、業(yè)務(wù)表數(shù)據(jù)被delete、truncate、impdp/imp大批量數(shù)據(jù)變更時(shí)。
2、數(shù)據(jù)變更量超過10%時(shí),統(tǒng)計(jì)信息狀態(tài)標(biāo)記為過期 。
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI