sql order by - Need help optimizing mysql query to get it to sort quickly by index -


someone helped me come query still slow; order slowing down , dont think using index i'm hoping can fix me :d yes read manual page can't understand it.

query:

 explain select u.id, u.url, u.title, u.numsaves  urls u  join tags t on t.url_id = u.id  , t.tag = 'osx'  order u.numsaves desc  limit 20    showing rows 20 - 19 ( 20 total, query took 1.5395 sec) [numsaves: 6130 - 2107]    id     select_type     table   type    possible_keys   key     key_len     ref     rows     1  simple  t   ref     tag_id  tag_id  767     const   49432   using where; using index; using temporary; using filesort  1  simple  u   eq_ref  primary,id_numsaves_ix  primary     4   jcooper_whatrethebest_urls.t.url_id     1    

database:

 create table `urls` (  `id` int(11) not null auto_increment,  `url` text not null,  `domain` text,  `title` text not null,  `description` text,  `numsaves` int(11) not null,  `firstsaved` varchar(256) default null,  `md5` varchar(255) not null default '',  primary key (`id`),  unique key `md5` (`md5`),  key `id_numsaves_ix` (`id`,`numsaves`)  ) engine=innodb auto_increment=2958560 default charset=utf8   create table `tags` (  `url_id` int(11) default null,  `hash` varchar(255) not null,  `tag` varchar(255) not null,  unique key `tag_id` (`tag`,`url_id`)  ) engine=innodb default charset=utf8 

i think main problem query choice of indexes.

1) tags has compound unique key on tag , url_id no primary key.

if nothing else, should make primary - may bit performance. also, might want take close if varchar(255) necessary tags. makes index quite big.

2) add separate index on numsaves since you're ordering that. compound index on id , numsaves not going here.

3) explain says have 49432 rows in tags match "osx". quite redundant. may want split tags table two, 1 containing text while other contains n:m link urls.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -