Skip to content

Add database index to fix slow post editing on large sites#22979

Open
ewimsatt wants to merge 1 commit intoYoast:trunkfrom
ewimsatt:fix/add-primary-focus-keyword-index
Open

Add database index to fix slow post editing on large sites#22979
ewimsatt wants to merge 1 commit intoYoast:trunkfrom
ewimsatt:fix/add-primary-focus-keyword-index

Conversation

@ewimsatt
Copy link

Description

Adds a composite database index to dramatically improve performance of duplicate focus keyword lookups on large sites.

Problem

On sites with 800K+ indexables, the query in WPSEO_Meta::keyword_usage() takes 1.5+ seconds, making post editing extremely slow. This query runs on every post edit to check if the primary focus keyword is already in use.

Solution

Add a composite index on:

  • primary_focus_keyword
  • object_type
  • post_status
  • object_id

This matches the exact WHERE clause pattern used in the query.

Performance Impact

Before index:

Query time: 1.557 seconds
Rows examined: 779,951

After index:

Query time: 0.001 seconds (1500x faster)
Rows examined: 3

Implementation

  • Follows existing migration pattern (see CreateIndexableSubpagesIndex)
  • Includes both up() and down() for safe rollback
  • Uses standard Yoast migration framework
  • No breaking changes

Testing

The migration can be tested by:

  1. Apply migration on test site with large indexable table
  2. Verify index exists: SHOW INDEX FROM wp_yoast_indexable
  3. Run EXPLAIN on the focus keyword query
  4. Measure post edit page load time

Fixes #22933

Adds composite index on (primary_focus_keyword, object_type, post_status, object_id)
to optimize duplicate focus keyword detection queries.

Performance impact on sites with 800K+ indexables:
- Before: 1.557 seconds
- After: 0.001 seconds (1500x improvement)

The query is executed on every post edit via WPSEO_Meta::keyword_usage()
to check if the focus keyword is already used elsewhere.

Fixes Yoast#22933
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.

[Bug]: Extremely slow post editing due to missing index in keyword_usage function

2 participants