getUnreadByConversationAndMarkRead: Only query incoming messages

This commit is contained in:
Scott Nonnenberg 2022-06-02 18:09:13 -07:00 committed by GitHub
parent ecdc583f2a
commit d753fe8fcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 2 deletions

View File

@ -2101,13 +2101,15 @@ async function getUnreadByConversationAndMarkRead({
expirationStartTimestamp = $expirationStartTimestamp,
json = json_patch(json, $jsonPatch)
WHERE
conversationId = $conversationId AND
(${_storyIdPredicate(storyId, isGroup)}) AND
isStory IS 0 AND
type IS 'incoming' AND
(
expirationStartTimestamp IS NULL OR
expirationStartTimestamp > $expirationStartTimestamp
) AND
expireTimer > 0 AND
conversationId = $conversationId AND
(${_storyIdPredicate(storyId, isGroup)}) AND
received_at <= $newestUnreadAt;
`
).run({

View File

@ -0,0 +1,39 @@
// Copyright 2021-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 updateToSchemaVersion59(
currentVersion: number,
db: Database,
logger: LoggerType
): void {
if (currentVersion >= 59) {
return;
}
db.transaction(() => {
db.exec(
`
DROP INDEX expiring_message_by_conversation_and_received_at;
CREATE INDEX expiring_message_by_conversation_and_received_at
ON messages
(
conversationId,
storyId
expirationStartTimestamp,
expireTimer,
received_at,
)
WHERE isStory IS 0 AND type IS 'incoming';
`
);
db.pragma('user_version = 59');
})();
logger.info('updateToSchemaVersion59: success!');
}

View File

@ -2286,4 +2286,42 @@ describe('SQL migrations test', () => {
);
});
});
describe('updateToSchemaVersion59', () => {
it('updates index to make query efficient', () => {
updateToVersion(47);
const items = db
.prepare(
`
EXPLAIN QUERY PLAN
UPDATE messages
INDEXED BY expiring_message_by_conversation_and_received_at
SET
expirationStartTimestamp = 342342,
json = json_patch(json, '{ "something": true }')
WHERE
conversationId = 'conversationId' AND
storyId IS NULL AND
isStory IS 0 AND
type IS 'incoming' AND
(
expirationStartTimestamp IS NULL OR
expirationStartTimestamp > 23423423
) AND
expireTimer > 0 AND
received_at <= 234234;
`
)
.all();
const detail = items.map(item => item.detail).join('\n');
assert.notInclude(detail, 'B-TREE');
assert.notInclude(detail, 'SCAN');
assert.include(
detail,
'SEARCH messages USING INDEX expiring_message_by_conversation_and_received_at (expirationStartTimestamp=? AND expireTimer>?)'
);
});
});
});