Make WordPress Core

Opened 23 months ago

Closed 23 months ago

Last modified 23 months ago

#62774 closed enhancement (duplicate)

The query in the count_user_posts function within wp-includes/user.php can be optimized to improve performance.

Reported by: deepakrohilla Owned by: deepakrohilla
Priority: normal Milestone:
Component: Database Version:
Severity: normal Keywords: has-patch
Cc: Focuses: performance

Description

Since the wp_posts table's ID column is indexed (as it is the primary key and has a dedicated index), using COUNT(ID) will be faster than COUNT(*) because MySQL can leverage the index for efficient counting. In InnoDB tables, COUNT(*) often requires a full table scan or index traversal since InnoDB does not maintain an exact row count. Therefore, replacing COUNT(*) with COUNT(ID) can improve query performance.

Attachments (3)

Screenshot from 2025-01-03 1.zip​ (62.7 KB ) - added by deepakrohilla 23 months ago.
Screenshot from 2025-01-03 12-26-48.png​ (33.8 KB ) - added by deepakrohilla 23 months ago.
Screenshot from 2025-01-03 11-17-58.png​ (33.8 KB ) - added by deepakrohilla 23 months ago.

Download all attachments as: .zip

Change History (9)

#1 @mukesh27
23 months ago

  • Component Query → Database
  • Keywords needs-patch added; has-patch removed
  • Type defect (bug) → enhancement

Thanks @deepakrohilla for the ticket!

Could you please share performance numbers before/after the changes with posts(10/100/1000)?

This ticket was mentioned in ​PR #8081 on ​WordPress/wordpress-develop by ​@deepakrohilla.


23 months ago
#2

  • Keywords has-patch added; needs-patch removed

#3 @mukesh27
23 months ago

In #39242, It add the caching to count_user_posts() that also help to improve the performance.

#4 @deepakrohilla
23 months ago

@mukesh27: https://ibb.co/q0scX1c .yes thanks for sharing #39242 this, it will help to improve performance of count query.

Last edited 23 months ago by deepakrohilla (previous) (diff)

#5 @swissspidy
23 months ago

  • Milestone Awaiting Review
  • Resolution → duplicate
  • Status assigned → closed

Duplicate of #39242.

Let‘s close this as a duplicate then :-) This can be tackled in the same ticket.

​@narenin commented on ​PR #8081:


23 months ago
#6

@deepakrohillas Good catch! PR looks good to me.

Note: See TracTickets for help on using tickets.