MySQL性能分析及explain的使用

MySQL性能分析及explain用法的知识是本文我们主要要介绍的内容,接下来就让我们通过一些实际的例子来介绍这一过程,希望能够对您有所帮助。

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分析的话,只用到了normal_key索引,但只对where子句起作用,而后面的order by需要排序。

http://database.51cto.com/art/201108/284783.htm