1.使用explain语句去查看分析结果
如explain select * from test1 where id=1;会出现:id selecttype table type possible_keys key key_len ref rows extra各列。
其中,
type=const表示通过索引一次就找到了;
key=primary的话,表示使用了主键;
type=all,表示为全表扫描;
key=null表示没用到索引。type=ref,因为这时认为是多个匹配行,在联合查询中,一般为REF。
2.MYSQL中的组合索引
假设表有id,key1,key2,key3,把三者形成一个组合索引,则
如:
- where key1=....
- where key1=1 and key2=2
- where key1=3 and key3=3 and key2=2
根据最左原则,这些都是可以使用索引的,如from test where key1=1 order by key3,用explain分析的话&#