I ran a query in Mysql like below:
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
and the result was:
id |select_type |table |type |possible_keys |key |key_len |ref |rows |Extra
1 |PRIMARY |<derived2> |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
This means it used the index parentid
but scaned all rows due to ALL
and 153153
.
Why could not the index help to not Full Scannig
?
Although if i run the derived query (Select #2) alone like below:
Explain
SELECT * FROM post
WHERE parentid=13
ORDER BY time , id
LIMIT 1,10
the result would be desired:
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
Edit:
The table post
has these indexes:
- id (PRIMARY)
- parentid
- time, id (timeid)
count of total rows --> 141280.
count of children of 13
(parentid=13
) --> 41
count of children of 11523
--> 10119
When i add index of (parent,time,id)
, problem of first query would be solved by the explin output for 13
--> 40 rows, type:ref
and for 11523
--> 19538 rows, type:ref!!! this Means all children rows of 11423
is examined while i limited first 10 rows.