有时候创建索引的时候不走索引,排除了字段数据问题和sql写法问题,索引失效的问题以外,还得考虑是统计信息过旧,得从新收集。 查看表的统计信息,看 user_index 的last_analyze(索引), 看user_tab_col_statistics 的last_analyze(字段)
一:解锁统计信息
sql
为了稳定执行计划,通常统计信息都会被锁住的,在更新统计信息的时候得先解锁。 ①按用户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','表名');
二:收集统计信息方法:
ide
1.分析表code
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.分析索引it
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');