I have a website where visitors can post comments. I want to add the ability to respond to comments (i.e. Nested comments).
At first, this query was quick, but after I populated the table with existing comments (about 30,000) with a simple query like:
SELECT c.id, c2.id
FROM (SELECT id
FROM swb_comments
WHERE pageId = 1411
ORDER BY id DESC
LIMIT 10) AS c
LEFT JOIN swb_comments AS c2 ON c.id = c2.parentId
took more than 2 seconds without any child components (!).
How to optimize such a request? A possible solution would be http://www.ferdychristant.com/blog//articles/DOMM-7QJPM7 (go to the “Flat Table Model Made Right”), but this makes pagination (how do I do this) difficult to limit to 10 parent comments in one request?)
The table has 3 indexes, id, pageId and ParentId.
Thanks in advance!
EDIT:
. SELECT (.. PageId numberId, )
CREATE TABLE `swb_comments` (
`id` mediumint(9) NOT NULL auto_increment,
`userId` mediumint(9) unsigned NOT NULL default '0',
`numberId` mediumint(9) unsigned default NULL,
`orgId` mediumint(9) unsigned default NULL,
`author` varchar(100) default NULL,
`email` varchar(255) NOT NULL,
`message` text NOT NULL,
`IP` varchar(40) NOT NULL,
`timestamp` varchar(25) NOT NULL,
`editedTimestamp` varchar(25) default NULL COMMENT 'last edited timestamp',
`status` varchar(20) NOT NULL default 'publish',
`parentId` mediumint(9) unsigned NOT NULL default '0',
`locale` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `userId` (`userId`),
KEY `numberId` (`numberId`),
KEY `orgId` (`orgId`),
KEY `parentId` (`parentId`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=34748 ;