日期:2014-05-16  浏览次数:20693 次

mysql查询优化相关技巧
本文参考自 《Mysql技术内幕(第4版)》
mysql查询优化的相关操作

  • 使用EXPLAIN语句检查优化器操作

+----+-------------+----------+-------+---------------+------+---------+------+------+-----------------
| id | select_type | table     | type    | possible_keys |   key   | key_len|   ref    | rows |             Extra                      
+----+-------------+----------+-------+---------------+------+---------+------+------+-----------------
| 1  |SIMPLE        | car_info | range | name   | name | 768    | NULL | 9      | Using where; Using index |
+----+-------------+----------+-------+---------------+------+---------+------+------+----------------

EXPLAIN输出解释
  • select_type 有如下几种类型:
SIMPLE:未使用连接查询或者子查询的简单select语句
explain select * from car_info;

PRIMARY:最外层的select语句
explain select * from (select name from car_info where name like '凯迪拉克%') as a;
+----+-------------+------------+-------+---------------+------+---------+------+------+-------------
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | 
+----+-------------+------------+-------+---------------+------+---------+------+------+---------------
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL |