是不是三个联合主键了,使用两个就没有了主键索引了???

解决方案 »

  1.   


    create table test(c1 varchar(20) , c2 varchar(20) , c3 varchar(30)) ;
    create index test_ix on test(c1,c2,c3) ;-- 会用到索引的查询
    select * from test where c1 = '1' 
    select * from test where c1 = '1' and c2 = '2'
    select * from test where c1 = '1' and c2 = '3'
    select * from test where c1 = '1' and c2 = '2' and c3 = '3'-- 不会用索引的查询
    select * from test where c2 = '2' 
    select * from test where c2 = '3' 
    select * from test where c2 = '2' and c3 = '3'-- 个人意见,欢迎讨论。
      

  2.   

    我通过执行计划 发现 每个查询都用了索引这可能与数据库的版本有关。
    一般情况下,要想复合索引起作用基本条件就是要从第一个索引字段开始用起,保持索引定义时的顺序。你是想稳定查询效率的话,就这样写SQL好些。如果确认需要从中间索引字段开始,可能就要考虑是否要创建新的索引(或复合索引)。
    另外,会不会使用索引上面只是基本条件,还与条件语句中有没有使用计算,函数,字段中是否有NULL值等等也有关,这些在网上可以找到相应资料,你可以了解下。