0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Added index on posts table for published_at column

refs https://github.com/TryGhost/Arch/issues/18

- The prev/next helpers are slow and are causing major performance issues. The helpers are using `posts.published_at` for comparisons extensively, which causes a full table scan - bad for query performance.
- We use published_at in other queries too (like default order for queries fetching all posts), so there  might be a slight performance boost across the system with this new index.
This commit is contained in:
Naz 2023-08-07 20:20:09 +08:00 committed by naz
parent 5ab81554fc
commit d068409fac
3 changed files with 52 additions and 2 deletions

View file

@ -0,0 +1,50 @@
const logging = require('@tryghost/logging');
const {createNonTransactionalMigration} = require('../../utils');
const INDEX_NAME = 'posts_published_at_index';
module.exports = createNonTransactionalMigration(
async function up(knex) {
let hasIndex = false;
if (knex.client.config.client === 'sqlite3') {
const result = await knex.raw(`select * from sqlite_master where type = 'index' and tbl_name = 'posts' and name = '${INDEX_NAME}'`);
hasIndex = (result.length !== 0);
} else {
const result = await knex.raw(`show index from posts where Key_name = '${INDEX_NAME}'`);
hasIndex = (result[0].length !== 0);
}
if (hasIndex) {
logging.info(`Skipping creation of index ${INDEX_NAME} on posts for published_at - already exists`);
return;
}
logging.info(`Creating index ${INDEX_NAME} on posts for published_at`);
await knex.schema.table('posts', (table) => {
table.index(['published_at']);
});
},
async function down(knex) {
let missingIndex = false;
if (knex.client.config.client === 'sqlite3') {
const result = await knex.raw(`select * from sqlite_master where type = 'index' and tbl_name = 'posts' and name = '${INDEX_NAME}'`);
missingIndex = (result.length === 0);
} else {
const result = await knex.raw(`show index from posts where Key_name = '${INDEX_NAME}'`);
missingIndex = (result[0].length === 0);
}
if (missingIndex) {
logging.info(`Skipping drop of index ${INDEX_NAME} on posts for published_at - does not exist`);
return;
}
logging.info(`Dropping index ${INDEX_NAME} on posts for published_at`);
await knex.schema.table('posts', (table) => {
table.dropIndex(['published_at']);
});
}
);

View file

@ -83,7 +83,7 @@ module.exports = {
created_by: {type: 'string', maxlength: 24, nullable: false},
updated_at: {type: 'dateTime', nullable: true},
updated_by: {type: 'string', maxlength: 24, nullable: true},
published_at: {type: 'dateTime', nullable: true},
published_at: {type: 'dateTime', nullable: true, index: true},
published_by: {type: 'string', maxlength: 24, nullable: true},
custom_excerpt: {type: 'string', maxlength: 2000, nullable: true, validations: {isLength: {max: 300}}},
codeinjection_head: {type: 'text', maxlength: 65535, nullable: true},

View file

@ -35,7 +35,7 @@ const validateRouteSettings = require('../../../../../core/server/services/route
*/
describe('DB version integrity', function () {
// Only these variables should need updating
const currentSchemaHash = '1c8f47d8f6c93e80a08185f8d36da158';
const currentSchemaHash = 'ad44bf95fee71a878704bff2a313a583';
const currentFixturesHash = '1803057343a6afa7b50f1dabbc21424d';
const currentSettingsHash = 'dd0e318627ded65e41f188fb5bdf5b74';
const currentRoutesHash = '3d180d52c663d173a6be791ef411ed01';