I am trying to optimize a query for selecting all the feeds related to a particular tag(tagid) then ordering them by Score. My tag table is around 100 million rows and my feed table is around 3 million rows.
This is what i have:
select feeds.FeedID, feeds.FeedName, feeds.Score
from feeds
inner join tags on feeds.FeedID = tags.FeedID
where tags.TagDirID = '123456'
order by feeds.Score desc
limit 500
However, when i do an explain on it i am getting this:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE tags ref FeedID,TagDirID TagDirID 5 const 228120 Using where; Using temporary; Using filesort
1 SIMPLE feeds eq_ref PRIMARY PRIMARY 4 FeedID 1
When I remove “order by Score” i lose the filesort and the query is very quick(<1 second), with it the query is around 45 seconds. I have an index on feeds.Score but it doesn’t appear to be being used when I try to sort on it. Any ideas how to speed this up? I read some stuff about leading tables and indexes but i couldn’t figure out how to fix it.
Here’s what the table structure is like:
CREATE TABLE `feeds` (
`FeedID` int(8) NOT NULL AUTO_INCREMENT,
`FeedName` varchar(100) DEFAULT NULL,
`Score` decimal(3,1) DEFAULT NULL,
PRIMARY KEY (`FeedID`),
KEY `Score` (`Score`)
)
CREATE TABLE `tags` (
`TagID` int(10) NOT NULL AUTO_INCREMENT,
`TagDirID` int(10) DEFAULT NULL,
`FeedID` int(8) DEFAULT NULL,
PRIMARY KEY (`TagID`),
KEY `FeedID` (`FeedID`),
KEY `Status` (`Status`),
KEY `TagDirID` (`TagDirID`)
)