Skip to content

Improved member list query performance#26953

Open
jonatansberg wants to merge 2 commits intomainfrom
ber-3471-members-list-query-performance
Open

Improved member list query performance#26953
jonatansberg wants to merge 2 commits intomainfrom
ber-3471-members-list-query-performance

Conversation

@jonatansberg
Copy link
Copy Markdown
Member

@jonatansberg jonatansberg commented Mar 25, 2026

ref https://linear.app/ghost/issue/BER-3471/fix-default-members-list-query-performance-with-a-composite-index

This PR adds a composite index for the default members list query. The default members browse path orders by created_at DESC, id DESC. Without a matching composite index, large member tables fall back to a full scan plus filesort. This change keeps the schema aligned with that query shape by adding members(created_at, id) to the members schema definition and shipping a non-transactional migration that creates the index with online DDL on MySQL.

Performance impact:

With 2m members cold queries went from ~7s to ~0.1s and hot queries from ~0.8s to ~0.03s.

Verification:

  • Ran yarn knex-migrator migrate, yarn knex-migrator rollback --force, and yarn knex-migrator migrate against MySQL
  • Confirmed the members_created_at_id_index index exists after re-migrating and that the default members query uses it again
  • Verified the migration's up/down and idempotent paths against SQLite with an isolated direct migration check
  • Ran mocha test/unit/server/data/schema/schema.test.js

ref https://linear.app/ghost/issue/BER-3471/fix-default-members-list-query-performance-with-a-composite-index

The members browse index change needs to be recreated through the migration CLI so the version folder and RC version bumps match the current branch state while preserving the online-DDL migration behavior.
@github-actions github-actions bot added the migration [pull request] Includes migration for review label Mar 25, 2026
@github-actions
Copy link
Copy Markdown
Contributor

It looks like this PR contains a migration 👀
Here's the checklist for reviewing migrations:

General requirements

  • ⚠️ Tested performance on staging database servers, as performance on local machines is not comparable to a production environment
  • Satisfies idempotency requirement (both up() and down())
  • Does not reference models
  • Filename is in the correct format (and correctly ordered)
  • Targets the next minor version
  • All code paths have appropriate log messages
  • Uses the correct utils
  • Contains a minimal changeset
  • Does not mix DDL/DML operations
  • Tested in MySQL and SQLite

Schema changes

  • Both schema change and related migration have been implemented
  • For index changes: has been performance tested for large tables
  • For new tables/columns: fields use the appropriate predefined field lengths
  • For new tables/columns: field names follow the appropriate conventions
  • Does not drop a non-alpha table outside of a major version

Data changes

  • Mass updates/inserts are batched appropriately
  • Does not loop over large tables/datasets
  • Defends against missing or invalid data
  • For settings updates: follows the appropriate guidelines

ref https://linear.app/ghost/issue/BER-3471/fix-default-members-list-query-performance-with-a-composite-index
The members schema changed with the new composite index and the integrity test must track that hash explicitly.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8618e058-067a-45c1-aa38-c7db2da745f3

📥 Commits

Reviewing files that changed from the base of the PR and between 0bc372c and 7c09605.

📒 Files selected for processing (5)
  • ghost/admin/package.json
  • ghost/core/core/server/data/migrations/versions/6.23/2026-03-25-13-06-01-add-members-created-at-id-index.js
  • ghost/core/core/server/data/schema/schema.js
  • ghost/core/package.json
  • ghost/core/test/unit/server/data/schema/integrity.test.js

Walkthrough

This pull request updates the Ghost admin and core package versions from 6.22.1 to 6.23.0-rc.0. A new database migration is introduced that creates a composite index on the members table for columns (created_at, id). The migration includes conditional logic to detect if the index already exists and supports both SQLite and MySQL databases with engine-specific SQL syntax. The schema definition is updated to reflect this new index, and the test schema integrity hash is updated to match the modified schema.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Improved member list query performance' directly and clearly summarizes the main change—adding a composite index to optimize the default members list query.
Description check ✅ Passed The description is directly related to the changeset, providing context on the composite index addition, performance metrics, and verification steps performed.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ber-3471-members-list-query-performance

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.18%. Comparing base (05e0954) to head (7c09605).
⚠️ Report is 27 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #26953   +/-   ##
=======================================
  Coverage   73.17%   73.18%           
=======================================
  Files        1538     1539    +1     
  Lines      121650   121668   +18     
  Branches    14709    14718    +9     
=======================================
+ Hits        89015    89039   +24     
+ Misses      31605    31600    -5     
+ Partials     1030     1029    -1     
Flag Coverage Δ
admin-tests 54.37% <ø> (ø)
e2e-tests 73.18% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jonatansberg jonatansberg changed the title Improved members index migration versioning Improved member list query performance Mar 25, 2026
@jonatansberg jonatansberg marked this pull request as ready for review March 25, 2026 15:09
Comment on lines +19 to +38
if (await hasIndex(knex)) {
logging.info(`Skipping creation of index ${INDEX_NAME} on members for created_at, id - already exists`);
return;
}

logging.info(`Creating index ${INDEX_NAME} on members for created_at, id`);

if (DatabaseInfo.isMySQL(knex)) {
await knex.raw(`
ALTER TABLE members
ADD INDEX ${INDEX_NAME} (created_at, id),
ALGORITHM=INPLACE,
LOCK=NONE
`);
return;
}

await knex.schema.table('members', (table) => {
table.index(['created_at', 'id']);
});
Copy link
Copy Markdown
Member

@kevinansfield kevinansfield Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker but I wonder if we should improve the existing addIndex and dropIndex commands to support index existence checks and algorithm and lock options? We have precedent for that in the addColumn and dropColumn commands

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

Labels

migration [pull request] Includes migration for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants