Fix messages_preview index
This commit is contained in:
parent
f49df88877
commit
fa8ff2ae4c
4 changed files with 68 additions and 7 deletions
|
@ -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),
|
||||
};
|
||||
})();
|
||||
|
|
33
ts/sql/migrations/49-fix-preview-index.ts
Normal file
33
ts/sql/migrations/49-fix-preview-index.ts
Normal 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!');
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue