explain执行计划分析,是SQL优化的重要手段。本节内容:
- explain各属性代表的含义。
EXPLAIN SELECT * FROM department d ; id|select_type|table|partitions|type|possible_keys|key|key_len|ref|rows|filtered|Extra| --|-----------|-----|----------|----|-------------|---|-------|---|----|--------|-----| 1|SIMPLE |d | |ALL | | | | | 2| 100| |
id表示查询中执行 select 子句或操作表的顺序。id的执行顺序规则:
id相同
EXPLAIN SELECT * FROM employee e ,department d ,customer c WHERE e.cus_id = c.id AND e.dep_id = d.id ;
id不同
EXPLAIN SELECT * FROM department WHERE id = ( SELECT id FROM employee WHERE id =( SELECT id FROM customer WHERE id = 1 ));
EXPLAIN SELECT * FROM department d, (SELECT dep_id FROM employee GROUP BY dep_id) t WHERE d.id = t.dep_id;
EXPLAIN SELECT * FROM employee e , department d WHERE e.id = d.id;
type 最好能达到index之上。