比如说在table1 中建立了 col1,col2,col3 3个字段的复合索引,
但是查询用还比较多用到下面的语句select * from table1 where col1=?
select * from table1 where col2=?
select * from table1 where col3=?在这种情况下,是否还需要为col1,col2,col3 这3个字段建单独索引?
但是查询用还比较多用到下面的语句select * from table1 where col1=?
select * from table1 where col2=?
select * from table1 where col3=?在这种情况下,是否还需要为col1,col2,col3 这3个字段建单独索引?
select * from table1 where col1=?
select * from table1 where col2=?
select * from table1 where col3=?
col1,col2,col3
col2
col3
如果1与2
1与3 有必然的逻辑关系的话,就不需要创建多个索引。不过你在使用where条件的时候必须的用上 1列。例如
where col2 = 'A'
and col1 = 'B'
等价于
where col2 = 'A'
这样可以只是创建一个即可,否则必须创建多个。
只有一个复合索引也是可以的,在查询的时候可以考虑使用HINT