有表如下:
create table test2(col1 int,col2 int,col3 int,col4 int,col5 int);
alter table test2 add constraint pk_test2 primary key (col1, col2, col3);
主键索引有三个字段:col1,col2,col3
只有sql中where条件中出现组合索引的第一个列是索引才会被使用到
测试如下:
生成数据:
begin
for i in 1 .. 100000 loop
insert into test2
values
(i,
DBMS_RANDOM.value(1, 1000),
DBMS_RANDOM.value(1, 1000),
DBMS_RANDOM.value(1, 1000),
DBMS_RANDOM.value(1, 1000));
end loop;
end;
测试结果:
select * from test2 where col1<=1000;--索引会被使用到;

select * from test2 where col2<=1000;--索引不会被使用到;

select * from test2 where col3<=1000;--索引不会被使用到;

本文介绍了一个包含三个字段的复合主键索引,并通过测试案例说明了在SQL查询中如何利用这种索引以提高查询效率。实验证明,只有当查询条件包含索引的第一个字段时,该索引才能被有效地利用。
227

被折叠的 条评论
为什么被折叠?



