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

@ -2653,7 +2653,7 @@ function getOldestMessageForConversation(
const row = db
.prepare<Query>(
`
SELECT * FROM messages WHERE
SELECT received_at, sent_at, id FROM messages WHERE
conversationId = $conversationId AND
isStory IS 0 AND
(${_storyIdPredicate(storyId, includeStoryReplies)})
@ -2686,7 +2686,7 @@ function getNewestMessageForConversation(
const row = db
.prepare<Query>(
`
SELECT * FROM messages WHERE
SELECT received_at, sent_at, id FROM messages WHERE
conversationId = $conversationId AND
isStory IS 0 AND
(${_storyIdPredicate(storyId, includeStoryReplies)})
@ -2750,35 +2750,29 @@ function getLastConversationPreview({
}): MessageType | undefined {
type Row = Readonly<{
json: string;
received_at: number;
sent_at: number;
}>;
const db = getInstance();
const queryTemplate = (extraClause: string): string => {
return `
SELECT json, received_at, sent_at FROM messages
INDEXED BY messages_preview
WHERE
conversationId IS $conversationId AND
shouldAffectPreview IS 1 AND
isGroupLeaveEventFromOther IS 0 AND
${includeStoryReplies ? '' : 'storyId IS NULL AND'}
${extraClause}
ORDER BY received_at DESC, sent_at DESC
LIMIT 1
`;
};
const index = includeStoryReplies
? 'messages_preview'
: 'messages_preview_without_story';
const row: Row | undefined = prepare(
db,
`
SELECT * FROM (${queryTemplate('expiresAt IS NULL')})
UNION ALL
SELECT * FROM (${queryTemplate('expiresAt > $now')})
ORDER BY received_at DESC, sent_at DESC
LIMIT 1;
SELECT json FROM (
SELECT json, expiresAt FROM messages
INDEXED BY ${index}
WHERE
conversationId IS $conversationId AND
shouldAffectPreview IS 1 AND
isGroupLeaveEventFromOther IS 0
${includeStoryReplies ? '' : 'AND storyId IS NULL'}
ORDER BY received_at DESC, sent_at DESC
)
WHERE likely(expiresAt > $now)
LIMIT 1
`
).get({
conversationId,
@ -2824,7 +2818,7 @@ async function getLastConversationMessage({
const row = db
.prepare<Query>(
`
SELECT * FROM messages WHERE
SELECT json FROM messages WHERE
conversationId = $conversationId
ORDER BY received_at DESC, sent_at DESC
LIMIT 1;
@ -2855,7 +2849,7 @@ function getOldestUnseenMessageForConversation(
const row = db
.prepare<Query>(
`
SELECT * FROM messages WHERE
SELECT received_at, sent_at, id FROM messages WHERE
conversationId = $conversationId AND
seenStatus = ${SeenStatus.Unseen} AND
isStory IS 0 AND
@ -3130,7 +3124,6 @@ async function getExpiredMessages(): Promise<Array<MessageType>> {
.prepare<Query>(
`
SELECT json FROM messages WHERE
expiresAt IS NOT NULL AND
expiresAt <= $now
ORDER BY expiresAt ASC;
`
@ -3181,6 +3174,10 @@ async function getSoonestMessageExpiry(): Promise<undefined | number> {
.pluck(true)
.get();
if (result != null && result >= Number.MAX_SAFE_INTEGER) {
return undefined;
}
return result || undefined;
}