[Task]: for very large sites, XML sitemap index build results in `lastmod` query sort inside a temp table, every time the index is built

Open
#23,640 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
42/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
mariadb, mysql, php

Research direction

Start in inc/sitemaps/class-post-type-sitemap-provider.php around lines 722-738 and inspect the shown sitemap index query and its USE INDEX hint. Compare the available approaches for avoiding the repeated full sort, then confirm that the chosen behavior reduces large-site build cost while preserving the intended lastmod results or clearly documenting the trade-off.

Written by the indexing model from the issue text.

Description

component: xml sitemap
Context - Why was this issue created?

Every time the sitemap index is built, the post type sitemap provider runs one query per post type to find the lastmod date for each sitemap page.

That query numbers every published post of the type with ROW_NUMBER() OVER (ORDER BY post_modified_gmt), and a USE INDEX ( type_status_date ) hint stops MySQL from using an index ordered by post_modified_gmt.

So MySQL sorts the whole post set in an internal temporary table on every build. On a site with about 500k+ published posts, this one query causes about 500k+ temporary-table row updates each time the index is built.

The query doesn't modify any data, but its cost grows linearly with post count and is paid every time the index is built. We stumbled across this issue because the query ends up looking like a write storm in our database monitoring systems.

This is the query in question:

WITH `ordering` AS (
    SELECT ROW_NUMBER() OVER (ORDER BY `post_modified_gmt`) AS n, post_modified_gmt
    FROM `wp_posts` USE INDEX ( `type_status_date` )
    WHERE `post_status` IN ('publish') AND `post_type` = 'post'
    ORDER BY `post_modified_gmt`
  )
  SELECT `post_modified_gmt` FROM `ordering` WHERE MOD(n, 4000) = 0

The site this runs on modified sitemap links per sitemap to 4000.

It comes from the post type sitemap provider's index-link code, here. Older versions used the same approach with @rownum user variables.

Why it's expensive
  • Every published post is materialized and sorted just to pick out every 4,000th date. At 4,000 per page, 500k+ posts produces 125 boundary dates.
  • USE INDEX ( type_status_date ) blocks the fix a site owner would try. That index can't provide post_modified_gmt order. The hint also keeps the optimizer from using a site-added index such as (post_type, post_status, post_modified_gmt), which could otherwise give an ordered index scan.
  • The sitemap transient cache doesn't help here. wpseo_enable_xml_sitemap_transient_caching defaults to false, and even when enabled it doesn't make the index-building query itself cheaper.
What is the goal of this issue?

The goal of this issue is to ask for the team to consider making the changes detailed below, so that sitemap generation runs more efficiently on especially large sites.

What needs to be done to achieve the goal?

Any one of these would help, and they don't depend on each other:

  1. Reconsider the USE INDEX hint for this query. Without it, a site can add an index that serves the post_modified_gmt ordering, so the query doesn't have to sort in a temporary table.
  2. Add a filter to skip or simplify per-page lastmod in the index. For example, fall back to the post type's overall latest modified date for every page. Large sites may prefer a cheaper, less precise lastmod over a full sort on every build.
  3. Cache the page-boundary dates between index builds. They only change as posts are published or modified, which could invalidate the cache the same way the existing sitemap cache is invalidated.
Does the issue still need UX or research?

No

If available, what are the tips for fixing the problem or possible solutions?

See above.

What is the expected result/behavior?

Without USE INDEX hint, MySQL/MariaDB is able to get the boundary dates dramatically sooner (in our case, we've added a wp_posts index on post_modified_gmt, which is why this is the case).

Should documentation be added or updated for this change? And if so, where?

No response

Dominant language
JavaScript
Stars
2k
Forks
988
Avg merge
3d 13h
Merged PRs (30d)
66

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Yoast/wordpress-seo

All issues in Yoast/wordpress-seo

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.