Skip to content

Optimize Post_Link_Indexing_Action queries (replace LEFT JOIN + OR with EXISTS)#23000

Closed
adconecto wants to merge 1 commit intoYoast:trunkfrom
adconecto:fix/post-link-indexing-action-queries
Closed

Optimize Post_Link_Indexing_Action queries (replace LEFT JOIN + OR with EXISTS)#23000
adconecto wants to merge 1 commit intoYoast:trunkfrom
adconecto:fix/post-link-indexing-action-queries

Conversation

@adconecto
Copy link
Copy Markdown

@adconecto adconecto commented Feb 17, 2026

Replace LEFT JOIN with EXISTS in Post_Link_Indexing_Action queries

Context

The queries in Post_Link_Indexing_Action::get_count_query() and
Post_Link_Indexing_Action::get_select_query() use a LEFT JOIN + OR pattern:

LEFT JOIN wp_yoast_indexable ...
LEFT JOIN wp_yoast_seo_links ...
WHERE ( I.object_id IS NULL OR L.post_id IS NOT NULL )

On medium-sized datasets (~25k posts / ~33k seo_links rows), this structure results in very slow execution times (60+ seconds).

The issue is not dataset scale but query structure.
The LEFT JOIN + OR combination prevents efficient index usage and can lead to suboptimal execution plans and table scans.

This PR rewrites the queries using EXISTS / NOT EXISTS, which allows MySQL/MariaDB to:

  • Perform indexed lookups per post
  • Avoid large intermediate join result sets
  • Significantly reduce execution time

Summary

This PR can be summarized in the following changelog entry:

Improves performance of internal link indexing by replacing a LEFT JOIN + OR query pattern with EXISTS / NOT EXISTS in Post_Link_Indexing_Action.

Label: changelog: enhancement

Relevant technical choices:

  • Replaced LEFT JOIN usage with correlated EXISTS / NOT EXISTS subqueries
  • Maintained identical logical behavior
  • Ensured both get_count_query() and get_select_query() remain in sync
  • Did not change return structure or public API
  • No schema changes required

Original problematic pattern:

LEFT JOIN ...
WHERE ( I.object_id IS NULL OR L.post_id IS NOT NULL )

New pattern:

WHERE (
    NOT EXISTS ( ... )
    OR EXISTS ( ... )
)

This allows the database optimizer to:

  • Use indexes on object_id and post_id
  • Avoid join result explosion
  • Short-circuit evaluation per row

Test instructions

Test instructions for the acceptance test before the PR gets merged

This PR can be acceptance tested by following these steps:

  1. Install Yoast SEO on a site with a moderate amount of content (e.g. 10k+ posts)
  2. Ensure indexables and internal link data are generated
  3. Trigger internal link indexing
  4. Verify:
    • No errors occur
    • The indexing process completes successfully
    • No regression in functionality
  5. Optionally compare query execution time before and after this PR

Expected result:

  • Identical functional behavior
  • Noticeably improved execution time on larger datasets

Relevant test scenarios

  • Changes should be tested with the browser console open
  • Changes should be tested on different posts/pages/taxonomies/custom post types/custom taxonomies
  • Changes should be tested on different editors (Default Block/Gutenberg/Classic/Elementor/other)
  • Changes should be tested on different browsers
  • Changes should be tested on multisite

Test instructions for QA when the code is in the RC

  • QA should use the same steps as above.

Impact check

This PR affects:

  • Internal link indexing logic

  • Database query performance for:

    • get_count_query()
    • get_select_query()
  • No UI components or front-end rendering are affected.

Other environments

  • This PR also affects Shopify. I have added a changelog entry starting with [shopify-seo], added test instructions for Shopify and attached the Shopify label to this PR.
  • This PR also affects Yoast SEO for Google Docs. I have added a changelog entry starting with [yoast-doc-extension], added test instructions for Yoast SEO for Google Docs and attached the Google Docs Add-on label to this PR.

Documentation

  • I have written documentation for this change. For example, comments in the Relevant technical choices, comments in the code, documentation on Confluence / shared Google Drive / Yoast developer portal, or other.

Quality assurance

  • I have tested this code to the best of my abilities.
  • During testing, I had activated all plugins that Yoast SEO provides integrations for.
  • I have added unit tests to verify the code works as intended.
  • If any part of the code is behind a feature flag, my test instructions also cover cases where the feature flag is switched off.
  • I have written this PR in accordance with my team's definition of done.
  • I have checked that the base branch is correctly set.
  • I have run grunt build:images and commited the results, if my PR introduces new images or SVGs.

Innovation

  • No innovation project is applicable for this PR.
  • This PR falls under an innovation project. I have attached the innovation label.
  • I have added my hours to the WBSO document.

See also #22997

@thijsoo
Copy link
Copy Markdown
Contributor

thijsoo commented Feb 20, 2026

Hi @adconecto . I have create a new PR. With your suggestion and added some unit tests to the PR.

I'm closing this PR in favor of that one, so we can keep track of the task at hand. Thanks for your contribution!

@thijsoo thijsoo closed this Feb 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants