Make WordPress Core

Opened 17 months ago

Closed 5 months ago

Last modified 2 months ago

#62123 closed enhancement (wontfix)

Improve performance of get_ancestors() with recursive common table expression.

Reported by: chouby's profile Chouby Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Taxonomy Keywords:
Focuses: performance Cc:

Description

get_ancestors() currently makes one DB query - through get_term() calls - per term in the hierarchy. This could be improved to have a unique query to get all ancestors using recursive common table expressions. The same for get_post_ancestors(). See https://dev.mysql.com/doc/refman/8.0/en/with.html

Here is a simple example of such a query to get all the parents of the term with term_id 6.

with recursive cte (term_id, parent) as (
  select     term_id,
             parent
  from       wp_term_taxonomy
  where      term_id = 6
  union all
  select     tt.term_id,
             tt.parent
  from       wp_term_taxonomy tt
  inner join cte
          on cte.parent = tt.term_id
)
select * from cte;

Caveat: this kind of queries requires MySQL 8 while the minimum version supported is still 5.7.

Change History (4)

This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.


16 months ago

#2 @pbearne
16 months ago

@Chouby do you want to create some to do this?
it would need to check MySQL version first.

#3 @Chouby
5 months ago

  • Resolution set to wontfix
  • Status changed from new to closed

After doing some tests, the proposal proves to be counterproductive in practical use cases.

For example, in the terms list table, terms are added to the cache prior to the call to get_ancestors(). So in this use case, probably the most frequent, the calls to get_term() in the function don't query the DB.

On the opposite, with this proposal, each call to get_ancestors() would result in one extra DB query.

Last edited 5 months ago by Chouby (previous) (diff)

#4 @swissspidy
2 months ago

  • Milestone Awaiting Review deleted

Removing milestone from closed ticket.

Note: See TracTickets for help on using tickets.