Make WordPress Core

Opened 15 months ago

Closed 14 months ago

Last modified 13 months ago

#55360 closed enhancement (fixed)

Add limit to query in WP_Tax_Query

Reported by: spacedmonkey's profile spacedmonkey Owned by: spacedmonkey's profile spacedmonkey
Milestone: 6.0 Priority: normal
Severity: normal Version: 4.7
Component: Taxonomy Keywords: has-patch has-unit-tests commit has-dev-note
Focuses: performance Cc:


The call to get_terms in WP_Tax_Query, does not currently have a limit set. This because some taxonomies are hierarchical and could have nested terms. But for none hierarchical taxonomies, a limit can and should be set. This limit will improve query performance and will improve likely of hitting an existing term query cache.

Attachments (2)

Screenshot 2022-03-10 at 00.19.50.png (1.2 MB) - added by spacedmonkey 15 months ago.
After patch
Screenshot 2022-03-10 at 00.20.13.png (1.2 MB) - added by spacedmonkey 15 months ago.
Before patch

Change History (10)

This ticket was mentioned in PR #2396 on WordPress/wordpress-develop by spacedmonkey.

15 months ago

  • Keywords has-patch added

#2 @spacedmonkey
15 months ago

  • Keywords has-unit-tests added

I recommend testing this ticket along side #55352.

With this change in place, it means that the early call to get_term_by populates the cache for the taxonomy query. This means one less query per page on tag pages.

Even without this benefit, limiting the query, makes the query return first, as SQL, stop processing once the limit is meet.

#3 @spacedmonkey
14 months ago

  • Milestone changed from Awaiting Review to 6.0

#4 @peterwilsoncc
14 months ago

  • Keywords commit added

Linked pull request approved, @spacedmonkey are you able to take the honors for commit?

#5 @spacedmonkey
14 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 53037:

Taxonomy: Improve performance by taxonomy queries by adding a limitting requested terms.

Add a limit to the number of terms requested in taxonomy queries. This improves the performance of the query and the likelihood of hitting an existing term query cache.

Props Spacedmonkey, peterwilsoncc, flixos90.
Fixes #55360.

spacedmonkey commented on PR #2396:

14 months ago

Merged into core.

#7 @spacedmonkey
13 months ago

Dev note ready for review.

Note: See TracTickets for help on using tickets.