Skip to content

Listings: search several locations with one postmeta JOIN - #3189

Open
benitoalba wants to merge 1 commit into
Automattic:trunkfrom
benitoalba:fix/3178-multi-location-search-performance
Open

benitoalba wants to merge 1 commit into
Automattic:trunkfrom
benitoalba:fix/3178-multi-location-search-performance

Conversation

@benitoalba

Copy link
Copy Markdown
Contributor

Fixes #3178

Changes Proposed in this Pull Request

  • A location search like Chicago; Boston; Seattle built one nested OR group per location. WP_Meta_Query gives each nested group its own wp_postmeta JOIN, and those JOINs are on post_id only, so every extra location multiplies the rows examined by the number of meta rows on a listing. With about 25 meta rows per listing that is roughly ×25 per location, which matches the timings in the issue.
  • All clauses now sit in a single OR group. WP_Meta_Query shares one JOIN between sibling LIKE clauses in an OR group, so the query has one postmeta JOIN however many locations are searched.
  • Results don't change: (A1 OR A2 OR A3) OR (B1 OR B2 OR B3) is the same condition as the flat list.
  • The loop lived in two places, get_job_listings() and WP_Job_Manager_Post_Types::job_feed(). Both now call a new helper, job_manager_get_location_meta_query(). (The issue mentions the admin list filter; the second copy I found in trunk is the job feed.)

Timings on a local site with 170 listings, 25 meta rows each, MariaDB 12.3, result cache off:

Locations trunk this PR
1 2 ms 2 ms
2 62 ms 2 ms
3 1,537 ms 1 ms
4 41,210 ms 1 ms
8 not run 2 ms

The returned listing IDs were identical on trunk and this branch for 1, 2 and 3 locations. For 4 locations I compared the count only (86 on both), because trunk takes 41 s per run.

Note that #3039 and #3021 touch the same loop, so whichever lands second will need a small rebase.

Testing Instructions

  1. Create 25 or more job listings with different locations (or use a site that already has them).
  2. On the [jobs] page, search the location field for one city, e.g. Chicago, and note the results.
  3. Search for Chicago; Boston; Seattle; London. On trunk this takes many seconds or times out; on this branch it returns straight away with the listings from all four cities.
  4. Open the job feed with the same search, /?feed=job_feed&search_location=Chicago;Boston;Seattle;London, and check it returns the same listings quickly.
  5. Tick "Remote positions only" together with a multi-location search and check only remote listings in those locations show.
  6. Run the tests: make test (new tests: --filter 'multiple_location|location_meta_query').

Release Notes

  • Fix slow or timed-out job searches when several locations are entered separated by semicolons.

New or Updated Hooks and Templates

  • New function job_manager_get_location_meta_query( $search_location ): returns the meta query group used to match listings in any of the semicolon-separated locations. I tagged it @since 2.4.13; happy to change that to whatever the next version is.

A location search such as "Chicago; Boston; Seattle" built one nested OR
group per location. WP_Meta_Query gives each nested group its own
wp_postmeta JOIN, joined on post_id only, so every extra location
multiplied the rows examined by the number of meta rows on a listing.

Put all clauses in a single OR group, which WP_Meta_Query serves from
one JOIN. The condition is logically the same, so results don't change.
The shared helper job_manager_get_location_meta_query() replaces the
copies in get_job_listings() and the job feed.

Fixes Automattic#3178.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Job searches with multiple locations time out

1 participant