主要內容:explain的possible_key、key、key_len、ref、rows入門。mysql
一、possible_keysql
表示在查詢過程當中可能用到的索引。查詢涉及到的字段上若是有索引,則該索引會出如今possible_key列中表示可能用到,但實際上並不必定會用到。例:數據庫
mysql> explain select * from t_blog where id = 1; +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+ | 1 | SIMPLE | t_blog | const | PRIMARY | PRIMARY | 4 | const | 1 | | +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+ 1 row in set
由於id是t_blog的主鍵,查詢時涉及到了該字段,possible_key列中有主鍵字樣。ide
二、key優化
表示再查詢過程當中實際用到的索引,若是爲null則表示沒有用到索引blog
如上例,key爲主鍵,表示查詢過程當中用到了主鍵。完整解讀:理論上要用到主鍵,實際上確實用到了主鍵。
索引
存在理論上應該用到某索引,但實際上沒有用到,即索引失效;it
若是查詢中使用了覆蓋索引(select子句與符合索引順序和字段徹底相同),possible_key爲null,key爲覆蓋索引。入門
三、key_lentable
索引中使用的字節數,越短越好。這個值表示最大可能使用長度而不是實際使用長度,例如:
mysql> explain select * from t_blog where title = 'C語言精講'; +----+-------------+--------+------+---------------+---------+---------+-------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+------+---------------+---------+---------+-------+------+--------------------------+ | 1 | SIMPLE | t_blog | ref | index_1 | index_1 | 153 | const | 1 | Using where; Using index | +----+-------------+--------+------+---------------+---------+---------+-------+------+--------------------------+ 1 row in set
此時,查詢條件多一個:
mysql> explain select * from t_blog where title = 'C語言精講' and typeId = 2; +----+-------------+--------+------+---------------+---------+---------+-------------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+------+---------------+---------+---------+-------------+------+--------------------------+ | 1 | SIMPLE | t_blog | ref | index_1 | index_1 | 158 | const,const | 1 | Using where; Using index | +----+-------------+--------+------+---------------+---------+---------+-------------+------+--------------------------+ 1 row in set
查詢條件變多,精度變高,同時key_len也在變大。
四、ref
顯示索引的哪一列被使用了,可能的話是一個常量。
一共有兩種格式:
1>const:常量,where <索引>='常量'
2><數據庫名>.<表名>.<字段名> :某數據庫中的某表中的某列被使用
五、rows
每張表有多少行被優化器查詢