I am trying to build a page where a list of messages, which a member has received, is displayed. I came up with this mySQL query but it runs extremely slow. It takes at least 10 seconds to execute it.
SELECT senderid,
receiverid
FROM messages
WHERE ( receiverid, sentdate ) IN (SELECT receiverid,
Max(sentdate)
FROM messages
WHERE receiverid = '1'
GROUP BY senderid)
ORDER BY sentdate DESC
This is the database I'm using:
`autoID` mediumint(11) unsigned NOT NULL AUTO_INCREMENT `senderID` mediumint(11) unsigned DEFAULT '0' `receiverID` mediumint(11) unsigned DEFAULT '0' `sentDate` datetime DEFAULT '0000-00-00 00:00:00' `message` longtext PRIMARY KEY (`autoID`) KEY `receiverID` (`receiverID`) KEY `senderID` (`senderID`)
The database has only 150,000 entries. I'm running my own dedicated server with only that mySQL database on it.
Any help is greatly appreciated.
G-Nugget, I've added an index on sentDate but no speed increase. Here's the EXPLAIN (sorry the way is shown. not sure how else I can do it):
id:1
select_type:PRIMARY
table:messages
type:ALL
possible_keys:null
keys:null
key_len:null
ref:null
rows:149433
Extra:Using where; Using filesort
id:2
select_type:DEPENDENT SUBQUERY
table:messages
type:ref
possible_keys:receiverID
key:receiverID
key_len:4
ref:const
rows:20
Extra:Using where; Using temporary; Using filesort