Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#55372 closed enhancement (fixed)

Use tax_query in wp_get_nav_menu_items

Reported by: spacedmonkey's profile spacedmonkey Owned by: spacedmonkey's profile spacedmonkey
Milestone: 6.0 Priority: normal
Severity: normal Version: 3.0
Component: Menus Keywords: has-patch needs-unit-tests good-first-bug has-dev-note
Focuses: performance Cc:

Description

Remove usage of get_objects_in_term and replace with a tax_query. This adds a level of consistency and will improve caching.

Attachments (7)

Screenshot 2022-03-15 at 15.35.15.png (1016.2 KB) - added by spacedmonkey 3 years ago.
Before change
Screenshot 2022-03-15 at 15.33.56.png (982.6 KB) - added by spacedmonkey 3 years ago.
After change
Screenshot 2022-03-22 at 22.42.06.png (108.5 KB) - added by spacedmonkey 3 years ago.
Before - Object cache
Screenshot 2022-03-22 at 22.40.35.png (314.8 KB) - added by spacedmonkey 3 years ago.
Before post query
Screenshot 2022-03-22 at 22.41.10.png (241.9 KB) - added by spacedmonkey 3 years ago.
After - Single query
Screenshot 2022-03-22 at 22.38.49.png (1012.9 KB) - added by spacedmonkey 3 years ago.
Before - page performance
Screenshot 2022-03-22 at 22.37.55.png (1013.9 KB) - added by spacedmonkey 3 years ago.
After - page performance

Change History (19)

#1 @spacedmonkey
3 years ago

  • Keywords has-patch needs-unit-tests good-first-bug added

Created a PR #2399

#3 @spacedmonkey
3 years ago

I would love the thoughts on @boonebgorges on this one.

Basically, it makes more sense to use taxonomy query check. Tax queries are cached now and performance of them is pretty solid. Long term, I would like deprecate get_objects_in_term, as it is only used in two place ( one them is here ).

#4 @spacedmonkey
3 years ago

This change removes 3 queries on a single page load.

@spacedmonkey
3 years ago

Before - Object cache

@spacedmonkey
3 years ago

Before post query

@spacedmonkey
3 years ago

After - Single query

@spacedmonkey
3 years ago

Before - page performance

@spacedmonkey
3 years ago

After - page performance

#5 @spacedmonkey
3 years ago

After some more testing with a menu with 200 items in it, I found some interesting results.

Currently, there is one query to get all the object ids and another to get post objects ( menu items ). In my test, with a menu with 200 items, this resulted to two queries returning 200 rows. The post query, results in an extremely long query with 200 ids in an IN state in the query.

Replacing with a taxonomy query, means one single request. I will admit, this query is a little slower that the original WP_Query. But as you are replace two queries with one, it there is still a net performance win. Not to mention less stress on the database, not returning 200 rows in two queries.

#6 @peterwilsoncc
3 years ago

I logged some notes on the pull request using a testing plugin to loop through some various sized menus using the old and the new code.

I was seeing the longer query @spacedmonkey documents above but the cumulative result of the patch was a positive.

It would be good to get some feedback from hosts with separate web and MySQL servers to make sure that's not a problem.

Generally, I'm in favour of this going in.

This ticket was mentioned in Slack in #hosting-community by spacedmonkey. View the logs.


3 years ago

#8 @spacedmonkey
3 years ago

  • Milestone changed from Awaiting Review to 6.0
  • Owner set to spacedmonkey
  • Status changed from new to assigned

#9 @spacedmonkey
3 years ago

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

In 53010:

Menus: Convert wp_get_nav_menu_items function to use a taxonomy query.

Change the wp_get_nav_menu_items function to replace the call to get_objects_in_term with a simple taxonomy query. This change improves performance as it results in one fewer query to retrieve menu items from the
database.

Props Spacedmonkey, peterwilsoncc.
Fixes #55372.

spacedmonkey commented on PR #2399:


3 years ago
#10

Committed

#11 @spacedmonkey
3 years ago

Dev note ready for review.

Note: See TracTickets for help on using tickets.