-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved performance fetching posts (#20460)
ref https://linear.app/tryghost/issue/ONC-111 - added composite index to posts_tags for post_id,tag_id for faster lookup - added composite index to posts for updated_at; this is commonly used by get helpers on the front end to display data like the latest posts In testing, this provided a very dramatic improvement for simple get helper requests like 'filter="id:-{{post.id}}+tag:sampleTag" limit="3"' which are by default sorted by updated_at desc. I'm not entirely clear why when sorting by published_at we do not need a composite index - so far it doesn't seem to be necessary. This should cover the primary cases for get helpers - the latest posts with a given tag or set of tags.
- Loading branch information
Showing
4 changed files
with
33 additions
and
4 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...server/data/migrations/versions/5.87/2024-06-25-12-08-20-add-posts-tags-post-tag-index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const logging = require('@tryghost/logging'); | ||
const {createNonTransactionalMigration} = require('../../utils'); | ||
const {addIndex, dropIndex} = require('../../../schema/commands'); | ||
|
||
module.exports = createNonTransactionalMigration( | ||
async function up(knex) { | ||
await addIndex('posts_tags', ['post_id', 'tag_id'], knex); | ||
}, | ||
async function down(knex) { | ||
try { | ||
await dropIndex('posts_tags', ['post_id', 'tag_id'], knex); | ||
} catch (err) { | ||
if (err.code === 'ER_DROP_INDEX_FK') { | ||
logging.error({ | ||
message: 'Error dropping index over posts_tags(post_id, tag_id), re-adding index for post_id' | ||
}); | ||
|
||
await addIndex('posts_tags', ['post_id'], knex); | ||
await dropIndex('posts_tags', ['post_id', 'tag_id'], knex); | ||
return; | ||
} | ||
|
||
throw err; | ||
} | ||
} | ||
); |
2 changes: 1 addition & 1 deletion
2
...0-17-02-15-add-posts-type-status-index.js → ...add-posts-type-status-updated-at-index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// For information on writing migrations, see https://www.notion.so/ghost/Database-migrations-eb5b78c435d741d2b34a582d57c24253 | ||
const {createAddIndexMigration} = require('../../utils'); | ||
|
||
module.exports = createAddIndexMigration('posts',['type','status']); | ||
module.exports = createAddIndexMigration('posts',['type','status','updated_at']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters