Skip to content

Improved performance of post previews#22554

Merged
jloh merged 1 commit intoTryGhost:mainfrom
jloh:add-posts-uuid-index
Mar 19, 2025
Merged

Improved performance of post previews#22554
jloh merged 1 commit intoTryGhost:mainfrom
jloh:add-posts-uuid-index

Conversation

@jloh
Copy link
Member

@jloh jloh commented Mar 19, 2025

ref https://linear.app/ghost/issue/ENG-2081

  • Added an index to the posts table on the uuid column to prevent a whole table scan when doing a posts preview which use a posts UUID to match against
  • On sites with a large number of posts this can currently cause lookups to take several seconds just to look up a single post

Cost before index:

| -> Limit: 1 row(s)  (cost=68964 rows=1) (actual time=333..333 rows=1 loops=1)
    -> Filter: (posts.uuid = '5e75e7da-bdcc-4a5c-ac7f-876041f4aa1d')  (cost=68964 rows=8127) (actual time=333..333 rows=1 loops=1)
        -> Table scan on posts  (cost=68964 rows=81271) (actual time=0.0641..325 rows=78701 loops=1)

Cost after index:

| -> Limit: 1 row(s)  (cost=0.63 rows=1) (actual time=0.0717..0.0718 rows=1 loops=1)
    -> Index lookup on posts using posts_uuid_test_index (uuid='5e75e7da-bdcc-4a5c-ac7f-876041f4aa1d')  (cost=0.63 rows=1) (actual time=0.0707..0.0707 rows=1 loops=1)

Adding the index on a site with 140k+ posts took ~1.4 seconds:

> ALTER TABLE `posts` ADD INDEX `posts_uuid_test_index`(`uuid`);
Query OK, 0 rows affected (1.44 sec)
Records: 0  Duplicates: 0  Warnings: 0

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 19, 2025

Walkthrough

A new migration file has been created to add an index to the uuid column of the posts table in the database. This file imports a utility function, createAddIndexMigration, and uses it to define the migration. The migration is exported as a module, indicating the target table (posts) and the column (uuid) for the index addition. Additionally, the uuid field's schema definition has been updated to include an index property, while retaining its original properties. Furthermore, the currentSchemaHash constant in the database integrity test has been updated to reflect the changes in the schema.

Suggested labels

browser-tests

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the migration [pull request] Includes migration for review label Mar 19, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Mar 19, 2025

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

General requirements

  • ⚠️ Tested on the staging database servers
  • 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

@jloh jloh marked this pull request as draft March 19, 2025 04:09
ref https://linear.app/ghost/issue/ENG-2081

- Added an index to the `posts` table on the `uuid` column to prevent a whole table scan when doing a posts preview which use a posts UUID to match against
- On sites with a large number of posts this can currently cause lookups to take several seconds just to look up a single post

Cost before index:
| -> Limit: 1 row(s)  (cost=68964 rows=1) (actual time=333..333 rows=1 loops=1)
    -> Filter: (posts.uuid = '5e75e7da-bdcc-4a5c-ac7f-876041f4aa1d')  (cost=68964 rows=8127) (actual time=333..333 rows=1 loops=1)
        -> Table scan on posts  (cost=68964 rows=81271) (actual time=0.0641..325 rows=78701 loops=1)

Cost after index:
| -> Limit: 1 row(s)  (cost=0.63 rows=1) (actual time=0.0717..0.0718 rows=1 loops=1)
    -> Index lookup on posts using posts_uuid_test_index (uuid='5e75e7da-bdcc-4a5c-ac7f-876041f4aa1d')  (cost=0.63 rows=1) (actual time=0.0707..0.0707 rows=1 loops=1)
@jloh jloh force-pushed the add-posts-uuid-index branch from 43f0d29 to 5d385ba Compare March 19, 2025 05:01
@jloh jloh marked this pull request as ready for review March 19, 2025 06:17
@jloh jloh requested a review from daniellockyer March 19, 2025 06:17
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
ghost/core/core/server/data/schema/schema.js (1)

53-53: Excellent performance optimization by adding index to posts.uuid

Adding an index to the UUID column is a well-considered optimization that will significantly improve query performance when looking up posts by UUID. According to the PR description, this change reduced lookup costs from 68964 rows to 0.63 rows and decreased lookup time from ~333ms to ~0.0718ms.

This change directly addresses the performance issues with post previews on sites with large post collections, where lookups were previously taking several seconds due to full table scans.

Consider reviewing other frequently queried columns in the system for potential indexing opportunities, especially for tables that grow large over time.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ce75740869af204000e5a55cd59a515e0026c81d and 5d385ba.

📒 Files selected for processing (3)
  • ghost/core/core/server/data/migrations/versions/5.114/2025-03-19-03-13-04-add-index-to-posts-uuid.js (1 hunks)
  • ghost/core/core/server/data/schema/schema.js (1 hunks)
  • ghost/core/test/unit/server/data/schema/integrity.test.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • ghost/core/core/server/data/migrations/versions/5.114/2025-03-19-03-13-04-add-index-to-posts-uuid.js
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Ghost-CLI tests
  • GitHub Check: Database tests (Node 20.11.1, sqlite3)
  • GitHub Check: Database tests (Node 22.13.1, mysql8)
  • GitHub Check: Regression tests (Node 20.11.1, mysql8)
  • GitHub Check: Database tests (Node 20.11.1, mysql8)
  • GitHub Check: Database tests (Node 18.12.1, mysql8)
  • GitHub Check: Unit tests (Node 20.11.1)
  • GitHub Check: Unit tests (Node 18.12.1)
🔇 Additional comments (1)
ghost/core/test/unit/server/data/schema/integrity.test.js (1)

38-38: Schema hash updated to reflect database changes

This update to the hash value is required due to the addition of an index to the 'uuid' column in the 'posts' table. The hash value ensures that any changes to the database schema are properly tracked and validated.

@jloh jloh merged commit a7fb152 into TryGhost:main Mar 19, 2025
52 checks passed
@jloh jloh deleted the add-posts-uuid-index branch March 19, 2025 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

affects:perf migration [pull request] Includes migration for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants