indexing - Why is my MySQL SELECT statement with ORDER BY so slow even though an INDEX is on the column? -


i have movies table. has 1.3 million rows in it.

the table has index on title column, order asc, length 255.

the title column varchar(1000).

even setup, following query takes 8 second run. ideas or shots in dark why may be? i'm stumped because seems such basic problem solve.

select title movies  order title limit 150000, 50000 

when take out order by, query super fast (0.05 seconds):

select title movies  limit 150000, 50000 

edit: prefix index better name partial index used.

since index partial index, mysql may not use order , still have sort values full lengths.

let's try small sample:

 create table o1 (a varchar(10));   insert o1 values('test1'),('test2'),('test3'),('tes1');  create index oindex on o1 (a);  explain select o1 order a; 

mysql using index order by.

     # id, select_type, table, type, possible_keys, key, key_len, ref, rows,      '1', 'simple', 'o1', 'index', null, 'oindex', '103', null, '8', 'using index' 

now, re-create partial index:

 drop index oindex on o1;  create index oindex on o1 (a (2) );  explain select o1 order a; 

mysql trying "filesort".

 # id, select_type, table, type, possible_keys, key, key_len, ref, rows,  '1', 'simple', 'o1', 'all', null, null, null, null, '8', 'using filesort' 

for searching, partial index helpful since mysql can drop values not matched. order by, mysql may have no such luck. in above case, create "partial index" max. length of column, mysql still not using index order by.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -