[wp-trac] [WordPress Trac] #8071: Refrain from querying all the comments on a post when paged

WordPress Trac noreply at wordpress.org
Thu Sep 17 20:13:51 UTC 2015


#8071: Refrain from querying all the comments on a post when paged
------------------------------------+-----------------------------
 Reporter:  markjaquith             |       Owner:  wonderboymusic
     Type:  enhancement             |      Status:  assigned
 Priority:  normal                  |   Milestone:  4.4
Component:  Comments                |     Version:  2.7
 Severity:  normal                  |  Resolution:
 Keywords:  has-patch dev-feedback  |     Focuses:
------------------------------------+-----------------------------

Comment (by wonderboymusic):

 [attachment:8071.3.diff 8071.3.diff] scales a post that has 100,371
 approved comments / 400,000 total comments bound to the post ID.

 Without the patch, the page doesn't even load, even when paginated.

 tl;dr Force comments pagination on posts that have either:
 * 2 times `comments_per_page`
 * 100 comments (50 is the default, 2 pages)

 If your comments are already paginated, and you have an extraordinary # of
 comments, these queries now scale. If you are not paginating comments, and
 all of a sudden, you get 100,000 comments, and you're not using an object
 cache, your page will now load.

 **Details**

 * Split The Query - get IDs first, then query for all fields for all
 relevant comments using an `IN` query
 * In `WP_Comment_Query`, mimic the `->found_rows` implementation of
 `WP_Query` by using `SQL_CALC_FOUND_ROWS` and adding a
 `->set_found_comments()` method and `->max_num_pages` prop. This allows us
 to paginate our queries, rather than paginating in Walker after querying
 every single comment.
 * Set `$wp_query->max_num_comments_pages` equal to the new
 `$wp_comments->max_num_pages`
 * When checking for `get_option( 'page_comments' )`, also check for
 `$post->comment_count > $paging_threshold` - this is a filterable value
 equal to (See Above)
 * When getting comment IDs for threaded comments, get them one depth level
 at a time, versus having Walker sort through a gigantic list that causes
 memory exhaustion. This can be done using the `parent__in` query var,
 which is now possible via [34205]
 * After getting all of the comment IDs via the passed query args, query
 all of the relevant objects at once via `IN` and then order the results
 via `comment__in`, ditching the extra sorting values. This is now possible
 via [34212].

--
Ticket URL: <https://core.trac.wordpress.org/ticket/8071#comment:18>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list