[wp-trac] [WordPress Trac] #37703: Optimise `wp_delete_comment` as called from `wp_delete_post`

WordPress Trac noreply at wordpress.org
Sat Sep 21 01:40:56 UTC 2019


#37703: Optimise `wp_delete_comment` as called from `wp_delete_post`
-------------------------------------------------+-------------------------
 Reporter:  peterwilsoncc                        |       Owner:  (none)
     Type:  enhancement                          |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
                                                 |  Review
Component:  Comments                             |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  needs-patch needs-unit-tests         |     Focuses:
  reporter-feedback                              |  performance
-------------------------------------------------+-------------------------

Comment (by peterwilsoncc):

 @Mjte90 Please accept my apologies for not seeing your follow up question
 so long ago.

 Step 1: Optimise call within `wp_delete_post()`
 - sort `$comment_ids` from highest to lowest [https://github.com/WordPress
 /wordpress-develop/blob/0bd3d78db1e54e9ba221322ec8fe8feec6f692ff/src/wp-
 includes/post.php#L2988-L2990 before deleting each comment in this loop].
 - this allows the [https://github.com/WordPress/wordpress-
 develop/blob/0bd3d78db1e54e9ba221322ec8fe8feec6f692ff/src/wp-
 includes/comment.php#L1386-L1389 reparenting step] to be skipped when
 deleting each comment.

 Step 2: Accept array of comments when calling `wp_delete_comment()`
 - when deleting an array of comments, calculate reparenting in bulk.
 - if a comment's parent is to be deleted, reparent to the first ancestor
 not being deleted

 The SQL queries may become something like this:

 {{{#!sql
 -- Comment threads
 -- 1 -> 3 -> 5, 6 -> 11
 -- 2 -> 7 -> 9
 -- 8 -> 4 -> 10
 -- 12 -> 13 -> 14

 -- wp_delete_comment( [ 5, 6, 7, 8, 11, 12, 13 ] );

 -- Determine if a parent comment is being deleted from a comment that is
 not being deleted

 SELECT comment_ID, comment_parent
   FROM wp_comments
   WHERE comment_parent IN (5, 6, 7, 8, 11, 12, 13)
     AND comment_ID NOT IN (5, 6, 7, 8, 11, 12, 13);

 -- result: (4, 8), (9, 7), (14, 13)
 -- Comments 8, 7, 13 are being deleted and parents of comments 4, 9, 14.

 -- Determine parents of 8, 7, 13 to determining grandparents of 4, 9, 14
 SELECT comment_ID, comment_parent FROM wp_comments WHERE comment_ID IN (8,
 7, 13);

 -- result (8, 0), (7, 2), (13, 12)
 -- No parent comment 8 thus no grandparent for comment 4. Comment 4 should
 be reparented to 0
 -- Parent of comment 7 is 2, thus comment 7 should be reparented to 2
 -- Parent of comment 13 is 12 (grandparent comment 14), 12 will be
 deleted.

 -- Repeat above step for comment 12 to determine comment 14's closest
 ancestor
 SELECT comment_ID, comment_parent FROM wp_comments WHERE comment_ID IN
 (12);

 -- result: (12, 0)
 -- 12 has no parent comment, thus comment 14 should be reparented to 0

 -- No further looping required to determine reparenting

 -- Reparent as follows
 -- Comment 4, reparent to 0
 -- Comment 7, reparent to 3
 -- Comment 14, reparent to 0
 }}}

 Step 2 is the more difficult step, as it requires fired hooks to remain
 unchanged.

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


More information about the wp-trac mailing list