标签:dbms_stats

共 1 篇相关文章

IT 累计浏览 3,716

如何使用dbms_stats分析统计信息?

dbms_stats 是 Oracle 8i 新增的程序包,用于生成和处理统计数据,通常被推荐替代 analyze。常用参数包括 estimate_percent(估算抽样百分比)和 method_opt,后者可指定 for table 只统计表、for all indexed columns 只统计有索引的表列、for all indexes 只分析相关索引。

具体操作上,可用 create_stat_table 创建统计信息历史保留表,再用 export_schema_stats 导出整个 scheme 的统计信息作为备份;分析时可用 gather_schema_stats(配合 options => 'GATHER AUTO'、auto_sample_size、degree 等参数)、gather_table_stats、gather_index_stats 分别收集 scheme、表和索引的统计信息。如果发现执行计划走错,可执行 delete_table_stats 删除表的统计信息,或用 import_table_stats / import_index_stats 导入历史统计信息;若分析后大部分表的执行计划都走错,则用 import_schema_stats 导回整个 scheme 的统计信息,最后可通过查询 all_tables 的 num_rows、blocks、last_analyzed 检查导入是否成功。

需要注意 analyze 与 dbms_stats 的差异:analyze 会同时更新表和索引的统计信息,而 dbms_stats 是先更新表、再更新索引,若表统计信息已更新而索引尚未更新,CBO 就可能选择错误的执行计划。