说明,在调优SQL的时候,验证统计信息对执行计划的影响,可以考虑先收集统计信息而不发布,让优化器在会话中使用未发布的统计信息,
从而比较收集统计信息后对执行计划的影响,待确认完毕后,再发布或者删除掉Pending的统计信息。
--DBMS_STATS.GET_PREFS 获取指定参数的缺省默认值
SH@test>SELECT DBMS_STATS.GET_PREFS('PUBLISH') PUBLISH FROM DUAL;
PUBLISH
--------------------------------------------------------------------------------
TRUE
SH@test>select dbms_stats.get_prefs('METHOD_OPT') from dual;
DBMS_STATS.GET_PREFS('METHOD_OPT')
--------------------------------------------------------------------------------
FOR ALL COLUMNS SIZE AUTO
SH@test>
-- 上面查询到,优化器的全局publishing设置是true
-- 查看pending 状态 ,没有的
SH@test>SELECT * FROM USER_TAB_PENDING_STATS;
no rows selected
SH@test>
-- 将sh.customers表的统计信息发布设置为pending
SH@test>exec DBMS_STATS.SET_TABLE_PREFS('sh', 'customers', 'publish', 'false');
PL/SQL procedure successfully completed.
SH@test>
-- 收集统计信息,并查看pending情况
SH@test>exec DBMS_STATS.GATHER_TABLE_STATS('sh','customers');
PL/SQL procedure successfully completed.
SH@test>SELECT TABLE_NAME, NUM_ROWS FROM USER_TAB_PENDING_STATS;
TABLE_NAME NUM_ROWS
---------- --------
CUSTOMERS 55500
SH@test>
-- 指示优化器在session中使用pending的统计信息,此时,session中使用了pending的统计信息
SH@test>ALTER SESSION SET OPTIMIZER_USE_PENDING_STATISTICS = true;
Session altered.
SH@test>
SH@test>UPDATE sh.customers
SET cust_email='ChalmersB@company.com'
WHERE cust_first_name = 'Bruce'
AND cust_last_name = 'Chalmers'; 2 3 4
16 rows updated.
SH@test>commit;
Commit complete.
SH@test>
-- 发布sh.customers表的统计信息
SH@test>exec DBMS_STATS.PUBLISH_PENDING_STATS('SH','CUSTOMERS');
PL/SQL procedure successfully completed.
SH@test>
-- 更改sh.sales表的publish统计信息为false
SH@test>exec DBMS_STATS.SET_TABLE_PREFS('sh', 'sales', 'publish', 'false');
PL/SQL procedure successfully completed.
SH@test>
-- 收集sh.sales表的统计信息,并查看pending的统计信息的情况
SH@test>exec DBMS_STATS.GATHER_TABLE_STATS('sh','sales');
PL/SQL procedure successfully completed.
SH@test>
SH@test>SELECT TABLE_NAME, NUM_ROWS FROM USER_TAB_PENDING_STATS;
TABLE_NAME NUM_ROWS
---------- --------
SALES ######
SALES 0
SALES 0
SALES 0
SALES 0
SALES 43687
SALES 64186
SALES 62197
SALES 60608
SALES 0
SALES 0
TABLE_NAME NUM_ROWS
---------- --------
SALES 35758
SALES 54233
SALES 55515
SALES 63292
SALES 0
SALES 0
SALES 50515
SALES 67138
SALES 58950
SALES 65769
SALES 0
TABLE_NAME NUM_ROWS
---------- --------
SALES 0
SALES 48874
SALES 62388
SALES 55984
SALES 69749
SALES 0
SALES 0
29 rows selected.
SH@test>
-- 删除掉pending统计信息,再次查看pending的统计信息情况,发现没有了。
SH@test>exec DBMS_STATS.DELETE_PENDING_STATS('sh','sales');
PL/SQL procedure successfully completed.
SH@test>SELECT TABLE_NAME, NUM_ROWS FROM USER_TAB_PENDING_STATS;
no rows selected
SH@test>
-- 设置sh.customers、sh.sales表的pending统计信息情况为默认状态,并查看结果
SH@test>exec DBMS_STATS.SET_TABLE_PREFS('sh', 'customers', 'publish', null);
PL/SQL procedure successfully completed.
SH@test>exec DBMS_STATS.SET_TABLE_PREFS('sh', 'sales', 'publish', null);
PL/SQL procedure successfully completed.
SH@test>
SH@test>select dbms_stats.get_prefs('PUBLISH','sh','sales') from dual;
DBMS_STATS.GET_PREFS('PUBLISH','SH','SALES')
--------------------------------------------------------------------------------
TRUE
SH@test>select dbms_stats.get_prefs('PUBLISH','sh','customers') from dual;
DBMS_STATS.GET_PREFS('PUBLISH','SH','CUSTOMERS')
--------------------------------------------------------------------------------
TRUE
END