Optimize conversation open performance

This commit is contained in:
Fedor Indutny 2023-01-27 09:47:24 -08:00 committed by GitHub
parent 6a80b4b837
commit 67b108c718
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 26 deletions

View file

@ -0,0 +1,59 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { LoggerType } from '../../types/Logging';
export default function updateToSchemaVersion76(
currentVersion: number,
db: Database,
logger: LoggerType
): void {
if (currentVersion >= 76) {
return;
}
db.transaction(() => {
db.exec(
`
-- Re-created below
DROP INDEX IF EXISTS message_expires_at;
DROP INDEX IF EXISTS messages_preview;
-- Create non-null expiresAt column
ALTER TABLE messages
DROP COLUMN expiresAt;
ALTER TABLE messages
ADD COLUMN
expiresAt INT
GENERATED ALWAYS
AS (ifnull(
expirationStartTimestamp + (expireTimer * 1000),
${Number.MAX_SAFE_INTEGER}
));
-- Re-create indexes
-- Note the "s" at the end of "messages"
CREATE INDEX messages_expires_at ON messages (
expiresAt
);
-- Note that expiresAt is intentionally dropped from the index since
-- expiresAt > $now is likely to be true so we just try selecting it
-- *after* ordering by received_at/sent_at.
CREATE INDEX messages_preview ON messages
(conversationId, shouldAffectPreview, isGroupLeaveEventFromOther,
received_at, sent_at);
CREATE INDEX messages_preview_without_story ON messages
(conversationId, shouldAffectPreview, isGroupLeaveEventFromOther,
received_at, sent_at) WHERE storyId IS NULL;
`
);
db.pragma('user_version = 76');
})();
logger.info('updateToSchemaVersion76: success!');
}