我在Mysql中运行了一个查询,如下所示:
EXPLAIN
SELECT *
FROM(
SELECT * # Select Number 2
FROM post
WHERE parentid = 13
ORDER BY time, id
LIMIT 1, 10
) post13_childs
JOIN post post13_childs_childs
ON post13_childs_childs.parentid = post13_childs.id
结果是:
id |select_type |table |type |possible_keys |key |key_len |ref |rows |Extra
1 |PRIMARY | |ALL | NULL | NULL |NULL |NULL |10 |
1 |PRIMARY |post13_childs_childs|ref |parentid |parentid |9 |post13_childs.id |10 |Using where
2 |DERIVED |post |ALL |parentid |parentid |9 | |153153 |Using where; Using filesort
这意味着它使用索引parentid,但扫描所有行,由于ALL和153153.
为什么索引不能帮助不完全扫描?
虽然如果我单独运行派生查询(选择#2):
Explain
SELECT * FROM post
WHERE parentid=13
ORDER BY time , id
LIMIT 1,10
结果将是希望的:
id |select_type |table |type |possible_keys |key |key_len |ref |rows |Extra
1 |SIMPLE |post |ref |parentid |parentid |9 |const|41 |Using where; Using filesort
编辑:
表格上有以下索引:
> id(PRIMARY)
> parentid
> time,id(timeid)
总行数 – > 141280.
13岁的孩子的计数(parentid = 13) – > 41
计数11523的儿童 – > 10119
当我添加(parent,time,id)的索引时,第一个查询的问题将被解析为13 – >的Explin输出. 40行,类型:参考
对于11523 – > 19538行,类型:ref !!!这意味着检查11423的所有儿童行,而我限制前10行.