Fix messages_preview index

Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com>
This commit is contained in:
automated-signal 2022-01-07 12:27:40 -08:00 committed by GitHub
parent c53df5a1d5
commit 9d17f40c1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 7 deletions

View File

@ -2541,10 +2541,8 @@ function getLastConversationActivity({
}
function getLastConversationPreview({
conversationId,
ourUuid,
}: {
conversationId: string;
ourUuid: UUIDStringType;
}): MessageType | undefined {
const db = getInstance();
const row = prepare(
@ -2565,7 +2563,6 @@ function getLastConversationPreview({
`
).get({
conversationId,
ourUuid,
now: Date.now(),
});
@ -2591,10 +2588,7 @@ async function getLastConversationMessages({
conversationId,
ourUuid,
}),
preview: getLastConversationPreview({
conversationId,
ourUuid,
}),
preview: getLastConversationPreview({ conversationId }),
hasUserInitiatedMessages: hasUserInitiatedMessages(conversationId),
};
})();

View File

@ -0,0 +1,33 @@
// Copyright 2021 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 updateToSchemaVersion49(
currentVersion: number,
db: Database,
logger: LoggerType
): void {
if (currentVersion >= 49) {
return;
}
db.transaction(() => {
db.exec(
`
DROP INDEX messages_preview;
-- Note the omitted 'expiresAt' column in the index. If it is present
-- sqlite can't ORDER BY received_at, sent_at using this index.
CREATE INDEX messages_preview ON messages
(conversationId, shouldAffectPreview, isGroupLeaveEventFromOther, received_at, sent_at);
`
);
db.pragma('user_version = 49');
})();
logger.info('updateToSchemaVersion49: success!');
}

View File

@ -24,6 +24,7 @@ import updateToSchemaVersion45 from './45-stories';
import updateToSchemaVersion46 from './46-optimize-stories';
import updateToSchemaVersion47 from './47-further-optimize';
import updateToSchemaVersion48 from './48-fix-user-initiated-index';
import updateToSchemaVersion49 from './49-fix-preview-index';
function updateToSchemaVersion1(
currentVersion: number,
@ -1911,6 +1912,7 @@ export const SCHEMA_VERSIONS = [
updateToSchemaVersion46,
updateToSchemaVersion47,
updateToSchemaVersion48,
updateToSchemaVersion49,
];
export function updateSchema(db: Database, logger: LoggerType): void {

View File

@ -1292,4 +1292,36 @@ describe('SQL migrations test', () => {
);
});
});
describe('updateToSchemaVersion49', () => {
it('creates usable index for messages preview', () => {
updateToVersion(49);
const details = db
.prepare(
`
EXPLAIN QUERY PLAN
SELECT json FROM messages
WHERE
conversationId = 'convo' AND
shouldAffectPreview IS 1 AND
isGroupLeaveEventFromOther IS 0 AND
(
expiresAt IS NULL
OR
expiresAt > 123
)
ORDER BY received_at DESC, sent_at DESC
LIMIT 1;
`
)
.all()
.map(({ detail }) => detail)
.join('\n');
assert.include(details, 'USING INDEX messages_preview');
assert.notInclude(details, 'TEMP B-TREE');
assert.notInclude(details, 'SCAN');
});
});
});