Very simple mysql query not using index
mysql
Solution
- You're selecting all the rows
- You're selecting all columns
Following what I said above - mysql estimates it to be more efficient to use full scan.
To get it using index you need to add some `WHERE` that would limit it to reasonable number of rows returned (say 50)
Problem
Sorting of my mySQL table does not use the index and I don't know why. I've got: ``` CREATE TABLE IF NOT EXISTS `test` ( `a` int(11) NOT NULL, `b` int(11) NOT NULL, KEY `kk` (`a`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; ``` and this: ``` EXPLAIN SELECT * FROM test ORDER BY a ``` as well as this ``` EXPLAIN SELECT * FROM test USE INDEX ( kk ) ORDER BY a ``` gives me this: ``` id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE test ALL NULL NULL NULL NULL 10009 Using filesort ``` I'd like not to see this filesort, and use the key kk to sort my table. What am I doing wrong? Thank you for your posts guys, they answer my question! However, now I do not undestand what is meant by "table scan" and "filesort"? Even if I am selecting all fields and all rows of a table, isn't it faster to sort that table by one column by walking in O(n) the internal tree of the index of that column (and then looking up in the table file the extra columns requested, in O(1) for each row => the index file stores each row's physical position in the table file, or?), than to sort e.g. by quick sort in O(n * log n) the (potentially) randomly stored rows in the table file, without touching the index? I guess my understanding of how indexes work in mySQL is wrong.