温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ORACLE索引失效,更新统计信息

发布时间:2020-06-23 22:09:15 来源:网络 阅读:6767 作者:神谕03 栏目:关系型数据库
有时候建立索引的时候不走索引,排除了字段数据问题和sql写法问题,索引失效的问题之外,还得考虑是统计信息过旧,得重新收集。
  查看表的统计信息,看 user_index 的last_analyze(索引),
    看user_tab_col_statistics 的last_analyze(字段)

一:解锁统计信息

为了稳定执行计划,一般统计信息都会被锁住的,在更新统计信息的时候得先解锁。
①按用户schema解锁:
EXEC DBMS_STATS.UNLOCK_schema_STATS('user_name');

②按表模式解锁:先查出被锁定的表
select table_name from user_tab_statistics where stattype_locked is not null;
然后exec dbms_stats.unlock_table_stats('user_name','表名');

二:收集统计信息方法:

1.分析表

begin

    dbms_stats.gather_table_stats (

    ownname          => 'TEST',

    tabname          => 'STUDENT',

    estimate_percent => dbms_stats.AUTO_SAMPLE_SIZE,

    degree           => 4,

    cascade          => TRUE);

end;

2.分析用户

begin

    dbms_stats.gather_schema_stats(

ownname          => 'TEST',

estimate_percent => dbms_stats.AUTO_SAMPLE_SIZE,

degree           => 4,

cascade          => TRUE);

end;

3.分析索引

begin

    dbms_stats.gather_index_stats(

    ownname          => 'TEST',

    indname          => 'IDX_STUDENT_BIRTH',

    estimate_percent => dbms_stats.AUTO_SAMPLE_SIZE,

    degree           => 4);

end;

还可以用analyze 来分析,例如:

ANALYZE TABLE  (table_name) COMPUTE STATISTICS;  --分析表
ANALYZE TABLE  (table_name) COMPUTE STATISTICS FOR ALL INDEXED COLUMNS;   --分析索引列
ANALYZE TABLE  (table_name) COMPUTE STATISTICS FOR ALL INDEXES FOR ALL COLUMNS;  --分析索引和索引列

三:更新完统计信息后得重新锁住。

CALL DBMS_STATS.LOCK_TABLE_STATS('user_name','table_name');
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI