I have a table that contain one primary key.
When I run
EXPLAIN SELECT * FROM news ORDER BY id ASC LIMIT 29
Mysql returns
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE news ALL NULL NULL NULL NULL 640 Using filesort
But by EXPLAIN SELECT *
FROM news
ORDER BY id ASC
LIMIT 28
it returns
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE news index NULL PRIMARY 4 NULL 28
SHOW INDEX FROM news ;
Table, Non_unique, Key_name, Seq_in_index, Column_name, Collation, Cardinality, Sub_part, Packed, Null, Index_type, Comment
'news', 0, 'PRIMARY', 1, 'id', 'A', 640, , '', '', 'BTREE', ''
'news', 1, 'tarix', 1, 'tarix', 'A', 106, , '', '', 'BTREE', ''
'news', 1, 'yayindil', 1, 'yayin', 'A', 3, , '', '', 'BTREE', ''
'news', 1, 'yayindil', 2, 'dil', 'A', 7, , '', '', 'BTREE', ''
I checked it on other tables, they work fine also on limit 4000. What is going wrong? Why only under 29 limitations use indexes?
The optimiser is trying to select the cheapest execution path. With only 640 rows in the table it isn’t strange that the optimiser chooses a full scan. Load the table with 40000 rows and you will find that you have to increase the limit (probably to around 2000) before a full table scan is chosen.