Short-circuit storyId predicate to optimize query

This commit is contained in:
Fedor Indutny 2022-03-21 14:21:35 -07:00 committed by GitHub
parent bddd55d574
commit 35b5087dc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 130 additions and 10 deletions

View file

@ -0,0 +1,38 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from 'better-sqlite3';
import type { LoggerType } from '../../types/Logging';
export default function updateToSchemaVersion52(
currentVersion: number,
db: Database,
logger: LoggerType
): void {
if (currentVersion >= 52) {
return;
}
db.transaction(() => {
db.exec(
`
-- Create indices that don't have storyId in them so that
-- '_storyIdPredicate' could be optimized.
-- See migration 47
CREATE INDEX messages_conversation_no_story_id ON messages
(conversationId, isStory, received_at, sent_at);
-- See migration 50
CREATE INDEX messages_unread_no_story_id ON messages
(conversationId, readStatus, isStory, received_at, sent_at)
WHERE readStatus IS NOT NULL;
`
);
db.pragma('user_version = 52');
})();
logger.info('updateToSchemaVersion52: success!');
}

View file

@ -27,6 +27,7 @@ import updateToSchemaVersion48 from './48-fix-user-initiated-index';
import updateToSchemaVersion49 from './49-fix-preview-index';
import updateToSchemaVersion50 from './50-fix-messages-unread-index';
import updateToSchemaVersion51 from './51-centralize-conversation-jobs';
import updateToSchemaVersion52 from './52-optimize-stories';
function updateToSchemaVersion1(
currentVersion: number,
@ -1917,6 +1918,7 @@ export const SCHEMA_VERSIONS = [
updateToSchemaVersion49,
updateToSchemaVersion50,
updateToSchemaVersion51,
updateToSchemaVersion52,
];
export function updateSchema(db: Database, logger: LoggerType): void {