Oracle索引碎片檢查及定期重建常用表的索引

2021-10-11 17:07:46 字數 2573 閱讀 9216

頻繁對索引字段進行delete、update操作,會對索引造成大量碎片,從而極大地影響索引的使用效率,並造成索引io的增加。

索引碎片分析:

analyze index index_name validate structure online;

select name,del_lf_rows_len,lf_rows_len,(del_lf_rows_len/lf_rows_len)*100 as "索引碎片率" from index_stats;

如果索引碎片率超過20%,則說明索引碎片已經非常嚴重。

可以編寫乙個檢測所有索引碎片率的指令碼,定期執行該指令碼,保持對索引碎片率的監控。

索引碎片整理:

重建 alter index index_name rebuild;

alter index index_name rebuild online;

定期重建索引指令碼:

create or replace type strsplit_type as table of varchar2(32676);

create or replace function strsplit(p_value varchar2, p_split varchar2 := ',')

--usage: select * from table(strsplit('1,2,3,4,5'))

return strsplit_type

pipelined is

v_idx integer;

v_str varchar2(500);

v_strs_last varchar2(4000) := p_value;

begin

loop

v_idx := instr(v_strs_last, p_split);

exit when v_idx = 0;

v_str := substr(v_strs_last, 1, v_idx - 1);

v_strs_last := substr(v_strs_last, v_idx + 1);

pipe row(v_str);

end loop;

pipe row(v_strs_last);

return;

end strsplit;

create or replace procedure up_check_to_rebuild_index

( tbnames varchar)is

sqlstr varchar2(100);

idx_ratio int;

begin

--declare sqlstr varchar2(100);

-- idx_ratio int;

begin

for idx in (select t.index_name from user_indexes t

where t.index_type = 'normal' and t.status = 'valid' and t.temporary = 'n' and t.leaf_blocks > 100

and t.table_name in (select upper(trim(column_value)) from table(strsplit(tbnames)))

order by t.table_name, t.index_name

)loop

dbms_output.put_line(idx.index_name || ' analyze start ' || to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'));

sqlstr := 'analyze index ' || idx.index_name || ' validate structure';

execute immediate sqlstr;

select trunc((t.del_lf_rows/t.lf_rows) * 100) into idx_ratio

from index_stats t where t.name=idx.index_name and rownum=1;

if (idx_ratio >= 20) then

dbms_output.put_line(' reindex ' || to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') || ' ratio: ' || idx_ratio);

sqlstr := 'alter index ' || idx.index_name || ' rebuild';

execute immediate sqlstr;

end if;

end loop;

end;

end up_check_to_rebuild_index;

sql>exec up_check_to_rebuild_index('tkk29, muser');

begin

up_check_to_rebuild_index('tkk29, muser');

end;

MySQL優化 定期清理索引碎片

在長期的資料更改過程中,索引檔案和資料檔案,都將產生空洞,形成碎片.我們可以通過乙個nop操作 不產生對資料實質影響的操作 來修改表.比如 表的引擎為innodb 可以 alter table engine innodb。也可以使用optimize table 表名 來進行修復.注意 修復表的資料及...

Oracle解決索引碎片功能

我們開始時向乙個空的帶索引的表中插入大量資料後,是不會產生碎片問題的,但是,資料庫經過很長一段時間的增刪改查後,難免會出現碎片問題,影響資料庫的效能,oracle對於這一問題有自己的解決方案。下面介紹解決這一問題的方案 首先要對索引進行分析 analyze index ind 1 validate ...

MySQL表的狀態檢查 索引修復 碎片整理

mysql 的optimizer 優化元件 在優化sql語句時,首先需要收集一些相關資訊,其中就包括表的cardinality 可以翻譯為 雜湊程度 它表示某個索引對應的列包含多少個不同的值,比如性別,正常就有2個值,男 女 如果cardinality大大少於資料的實際雜湊程度,那麼會影響 sql ...