實驗表未更新造成的錯誤
*******************
create table t as select 1 id,object_name from dba_objects; /建立測試表
update t set id=99 where rownum=1; /更新資料列
select id,count(*) from t group by id;
exec dbms_stats.gather_table_stats('SYS','T',cascade=>true); /分析統計資訊
set autotrace traceonly;
select * from t where id=1; /實際id=1的筆數有50954筆
執行計畫估算的筆數是50955筆,非常接近實際筆數
set autotrace off;
update t set id=99 where id=1; /更改資料表資料,實際id=1的筆數為1筆
update set id=1 where rownum=1; /更改資料表資料,實際id=99的筆數為50954筆
set autotrace traceonly;
select * from t where id=1; /再次執行SQL指令看執行計畫,發現ID=1的資料筆數還是50955筆,沒有更新成1筆,表示因為修改資料表,分析資料過時了,執行計畫錯誤
exec dbms_stats.gather_table_stats('SYS','T',cascade=>true); /再次分析統計資訊,執行計畫更新了
小結:
在實際的資料庫環境中,若CBO執行計畫選擇錯誤,DBA要先考慮到是否有地方失誤,比如說此範例