您的位置:首页 > 数据库 > Oracle

复合索引不同建法的选择

2010-10-28 14:19 323 查看
create index ind_old on test(a,b,c,d) tablespace tbsindex compress 2;
select segment_name,bytes,blocks from user_segments where segment_name='IND_OLD';
set autotrace trace;
--重建复合索引中列的排列情况选择
--查询各个列的distinct值
select count(distinct b) from test;
338965
select count(distinct a) from test;
1148
select count(distinct c) from test;
2
select count(distinct d) from test;
5
--页索引里列平均存储长度。
select avg(vsize(b)) from test;
7字节
select avg(vsize(c)) from test;
2字节
select avg(vsize(a)) from test;
5.5字节
select avg(vsize(d)) from test;
1.67字节

---根据以上信息计算页索引所需要的空间
index1:(b,c,a,d) compress 2
B:distinct number---338965
C:distinct number---2
index size=338965*2*(7+2)+537351*(1.7+5.5+6)=14603998

index2:(c,a,b,d) compress 2
B:distinct number---2
A:distinct number---1148
index size=2*1148*(2+5.5)+537351*(7+1.7+6)=7916279

index3:(c,d,a,b) compress 2
C:distinct number---2
D:distinct number---5
index size=2*5*(2+1.7)+537351*(7+5.5+6)=9941030
--可见index2的索引最小。
--这是一个估算公式,索引的空间大小还可以通过user_segments等视图查询,compress 2的时候页索引:size=前导列1数*前导列2数(前导列1位长+前导列2位长)+所有记录数*(第3列位长+第4列位长+6)+overhead,这里的6是rowid在索引里面的存储位长。
create index idx_auction_browse on test(c,a,b,d) tablespace tbsindex compress 2;

————《Oracle数据库性能优化》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息