I have a website where visitors can leave comments. I want to add the ability to respond to comments (i.e. nested comments)
At first this question was fast, but after the table population like a simple question with the current observations (about 30000):
SELECT c.id, C2.id FROM (SELECT id swb_comments WHERE page id = 1411 by order ID DESC LIMIT 10) Skip the form swb_comments c2 at c.id = c2.parentId It took 2 seconds to not even have a child. notes (!).
How do I optimize a query like this? Scroll to the potential solution ("Flat Table Model Corrected") but it makes Pagegang difficult (How do I limit 10 basic comments within 1 query?)
In the table 3 index ID, Page id and Parent ID
Thanks in advance!
Edit:
Added table definition. This is a complete definition with some differences in the SELECT query above (i.e. page id instead of numberId to avoid confusion)
create tab 'swb_comments` (`id` middle finger (9) Null Auto_increment 'User ID' Secondary (9) Unsigned Not Default Default '0', `Number Id` Medium Indent (9) Unsigned Default Zero,` Argid` Middle Indoor (9) Unsigned Default Zero, `Author 'varchar (100) Default Faucet , Email `Varchar (255) No tap,` message` text Not 'ipt' varchar (40) No 'timestamp' varchar (25) No tap, 'edited thamestamp' varchar (25) Default tap comment 'Last edited timestamp', `position` varchar (20) not default Default 'published', 'parent ID' moderate (9) unsigned, zero default '0', 'locale' varchar (10) no primary key (`id`), key` user id (`user id`), key `Number id '(` idid`), key `organ' (` orgiaed), key` parent IDID (`Parent ID ')) Engine = MySQL Default Charge = UTF8 Outangement = 34748;
The problem is that the MySQL index can not be applied if its result One Derivative Query Required (This is the reason that you have zero in the possible_keys column). So I recommend filtering out ten comments:
SELECT * FROM swb_comments WHERE page ID = 1411 order ID DESC limit 10 and its Afterward send a separate request to get an answer for each comment ID:
SELECT * FROM swb_comments WHERE in parentId IN ($ commentId1, $ commentId2, ..., $ commentId10) In this case database engine will be able to apply page id and parent ID index efficiently.
Comments
Post a Comment