简体   繁体   中英

Most efficient way to query MySQL for latest comments + total number of comments

On Facebook, when a wall post has a lot of comments, you see only the last two comments. Right above them, however, is a link that says something like "View all 15 comments."

In MySQL, would this require two queries? One to get the total comment count, and the other to get the contents of the last two comments? Then, clicking that "View all" link requires a third in order to get the content of the remaining comments.

If this is the case, wouldn't it be more efficient to simply get the contents of all the comments in a single MySQL query? This would allow you to find the total number using PHP, and you wouldn't need to make the third query if you simply output the contents of all the comments to the page, using css to hide those that you don't want initially shown and JavaScript to show them when the user clicks the "View All" link.

I assume I'm wrong. Surely the Facebook developers are better at this than I am. But I'm developing a site that has a similar requirement and I'm trying to understand the most efficient way to achieve this kind of functionality.

In MySQL you could use a limited select to retrieve the latest comments specifying SQL_CALC_FOUND_ROWS followed by a FOUND_ROWS() query to fetch the count of all comments.

ie. First do :

select SQL_CALC_FOUND_ROWS title, description from comments order by post_date desc limit 2;

to retrieve the 2 most recent comments, then do :

select FOUND_ROWS();

to retrieve the total number of comments.

Refer to the FOUND_ROWS() documentation on mysql.com for more information.

As far as performance is concerned, it should be faster then executing another COUNT(*) query (according to the docs :p)...

They probably know that something like 90% of all page viewers will never click on the "view all" link, so by doing it that way, they conserve the extra database traffic, and the extra web server to client usage.

Performing two database queries back-to-back between the webserver the the database has neglible overhead. There is no need to set up another connection or for the db server to create another thread to service the connection. This isn't really something ripe for optimization. Reducing the amount of data retrieved is prudent use of resources.

Ultimately, the best/fastest way to get the comments count is to store them within the news post table, your almost caching the "COUNT(*)" query to the comments column.

You can then chose to load the comments with a second query if you need to or skip it completely if there are no comments.

You could also opt for a LEFT JOIN query which will count the comments within a subquery, and return 0 if it needs to, keeping it as 1 query instead of 2.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM