Short-circuit storyId predicate to optimize query
This commit is contained in:
parent
bddd55d574
commit
35b5087dc0
4 changed files with 130 additions and 10 deletions
|
@ -2056,6 +2056,16 @@ async function getMessageBySender({
|
|||
return jsonToObject(rows[0].json);
|
||||
}
|
||||
|
||||
export function _storyIdPredicate(storyId: string | undefined): string {
|
||||
if (storyId === undefined) {
|
||||
// We could use 'TRUE' here, but it is better to require `$storyId`
|
||||
// parameter
|
||||
return '$storyId IS NULL';
|
||||
}
|
||||
|
||||
return 'storyId IS $storyId';
|
||||
}
|
||||
|
||||
async function getUnreadByConversationAndMarkRead({
|
||||
conversationId,
|
||||
newestUnreadAt,
|
||||
|
@ -2086,7 +2096,7 @@ async function getUnreadByConversationAndMarkRead({
|
|||
) AND
|
||||
expireTimer > 0 AND
|
||||
conversationId = $conversationId AND
|
||||
($storyId IS NULL OR storyId IS $storyId) AND
|
||||
(${_storyIdPredicate(storyId)}) AND
|
||||
received_at <= $newestUnreadAt;
|
||||
`
|
||||
).run({
|
||||
|
@ -2105,7 +2115,7 @@ async function getUnreadByConversationAndMarkRead({
|
|||
WHERE
|
||||
readStatus = ${ReadStatus.Unread} AND
|
||||
conversationId = $conversationId AND
|
||||
($storyId IS NULL OR storyId IS $storyId) AND
|
||||
(${_storyIdPredicate(storyId)}) AND
|
||||
received_at <= $newestUnreadAt
|
||||
ORDER BY received_at DESC, sent_at DESC;
|
||||
`
|
||||
|
@ -2125,7 +2135,7 @@ async function getUnreadByConversationAndMarkRead({
|
|||
WHERE
|
||||
readStatus = ${ReadStatus.Unread} AND
|
||||
conversationId = $conversationId AND
|
||||
($storyId IS NULL OR storyId IS $storyId) AND
|
||||
(${_storyIdPredicate(storyId)}) AND
|
||||
received_at <= $newestUnreadAt;
|
||||
`
|
||||
).run({
|
||||
|
@ -2360,7 +2370,7 @@ function getOlderMessagesByConversationSync(
|
|||
conversationId = $conversationId AND
|
||||
($messageId IS NULL OR id IS NOT $messageId) AND
|
||||
isStory IS 0 AND
|
||||
($storyId IS NULL OR storyId IS $storyId) AND
|
||||
(${_storyIdPredicate(storyId)}) AND
|
||||
(
|
||||
(received_at = $received_at AND sent_at < $sent_at) OR
|
||||
received_at < $received_at
|
||||
|
@ -2453,7 +2463,7 @@ function getNewerMessagesByConversationSync(
|
|||
SELECT json FROM messages WHERE
|
||||
conversationId = $conversationId AND
|
||||
isStory IS 0 AND
|
||||
($storyId IS NULL OR storyId IS $storyId) AND
|
||||
(${_storyIdPredicate(storyId)}) AND
|
||||
(
|
||||
(received_at = $received_at AND sent_at > $sent_at) OR
|
||||
received_at > $received_at
|
||||
|
@ -2483,7 +2493,7 @@ function getOldestMessageForConversation(
|
|||
SELECT * FROM messages WHERE
|
||||
conversationId = $conversationId AND
|
||||
isStory IS 0 AND
|
||||
($storyId IS NULL OR storyId IS $storyId)
|
||||
(${_storyIdPredicate(storyId)})
|
||||
ORDER BY received_at ASC, sent_at ASC
|
||||
LIMIT 1;
|
||||
`
|
||||
|
@ -2510,7 +2520,7 @@ function getNewestMessageForConversation(
|
|||
SELECT * FROM messages WHERE
|
||||
conversationId = $conversationId AND
|
||||
isStory IS 0 AND
|
||||
($storyId IS NULL OR storyId IS $storyId)
|
||||
(${_storyIdPredicate(storyId)})
|
||||
ORDER BY received_at DESC, sent_at DESC
|
||||
LIMIT 1;
|
||||
`
|
||||
|
@ -2651,7 +2661,7 @@ function getOldestUnreadMessageForConversation(
|
|||
conversationId = $conversationId AND
|
||||
readStatus = ${ReadStatus.Unread} AND
|
||||
isStory IS 0 AND
|
||||
($storyId IS NULL OR storyId IS $storyId)
|
||||
(${_storyIdPredicate(storyId)})
|
||||
ORDER BY received_at ASC, sent_at ASC
|
||||
LIMIT 1;
|
||||
`
|
||||
|
@ -2688,7 +2698,7 @@ function getTotalUnreadForConversationSync(
|
|||
conversationId = $conversationId AND
|
||||
readStatus = ${ReadStatus.Unread} AND
|
||||
isStory IS 0 AND
|
||||
($storyId IS NULL OR storyId IS $storyId);
|
||||
(${_storyIdPredicate(storyId)})
|
||||
`
|
||||
)
|
||||
.get({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue