Someone helped me come up with this query but its still too slow; The order by is slowing it down and I dont think its using my index I'm hoping someone can fix it for me :D Yes I read the manual page but I can't understand it.
Query:
EXPLAIN SELECT u.id, u.url, u.title, u.numsaves
FROM urls u
JOIN tags t ON t.url_id = u.id
AND t.tag = 'osx'
ORDER BY 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 Extra
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 the main problem with your query is your choice of indexes.
1) tags
has a compound UNIQUE KEY
on tag
and url_id
but no PRIMARY KEY
.
If nothing else, you should make it primary - this may help a bit with performance. Also, you might want to take a close look if VARCHAR(255)
is really necessary for your tags. It makes the index quite big.
2) add a separate index on numsaves
since you're ordering by that. The compound index on id
and numsaves
is not going to help here.
3) EXPLAIN
says that you have 49432 rows in tags
that match "osx"
. This is quite redundant. You may want to split your tags table into two, one containing the text while the other contains the N:M link to urls
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.