getAllStories: Remove nested queries
This commit is contained in:
parent
a795602e19
commit
95209689a8
5 changed files with 328 additions and 34 deletions
|
@ -2933,44 +2933,50 @@ function getAllStories(
|
|||
sourceServiceId?: ServiceIdString;
|
||||
}
|
||||
): GetAllStoriesResultType {
|
||||
const [storiesQuery, storiesParams] = sql`
|
||||
SELECT json, id
|
||||
FROM messages
|
||||
WHERE
|
||||
isStory = 1 AND
|
||||
(${conversationId} IS NULL OR conversationId IS ${conversationId}) AND
|
||||
(${sourceServiceId} IS NULL OR sourceServiceId IS ${sourceServiceId})
|
||||
ORDER BY received_at ASC, sent_at ASC;
|
||||
`;
|
||||
const rows: ReadonlyArray<{
|
||||
id: string;
|
||||
json: string;
|
||||
hasReplies: number;
|
||||
hasRepliesFromSelf: number;
|
||||
}> = db
|
||||
.prepare<Query>(
|
||||
`
|
||||
SELECT
|
||||
json,
|
||||
(SELECT EXISTS(
|
||||
SELECT 1
|
||||
FROM messages as replies
|
||||
WHERE replies.storyId IS messages.id
|
||||
)) as hasReplies,
|
||||
(SELECT EXISTS(
|
||||
SELECT 1
|
||||
FROM messages AS selfReplies
|
||||
WHERE
|
||||
selfReplies.storyId IS messages.id AND
|
||||
selfReplies.type IS 'outgoing'
|
||||
)) as hasRepliesFromSelf
|
||||
FROM messages
|
||||
WHERE
|
||||
type IS 'story' AND
|
||||
($conversationId IS NULL OR conversationId IS $conversationId) AND
|
||||
($sourceServiceId IS NULL OR sourceServiceId IS $sourceServiceId)
|
||||
ORDER BY received_at ASC, sent_at ASC;
|
||||
`
|
||||
}> = db.prepare(storiesQuery).all(storiesParams);
|
||||
|
||||
const [repliesQuery, repliesParams] = sql`
|
||||
SELECT DISTINCT storyId
|
||||
FROM messages
|
||||
WHERE storyId IS NOT NULL
|
||||
`;
|
||||
const replies: ReadonlyArray<{
|
||||
storyId: string;
|
||||
}> = db.prepare(repliesQuery).all(repliesParams);
|
||||
|
||||
const [repliesFromSelfQuery, repliesFromSelfParams] = sql`
|
||||
SELECT DISTINCT storyId
|
||||
FROM messages
|
||||
WHERE (
|
||||
storyId IS NOT NULL AND
|
||||
type IS 'outgoing'
|
||||
)
|
||||
.all({
|
||||
conversationId: conversationId || null,
|
||||
sourceServiceId: sourceServiceId || null,
|
||||
});
|
||||
`;
|
||||
const repliesFromSelf: ReadonlyArray<{
|
||||
storyId: string;
|
||||
}> = db.prepare(repliesFromSelfQuery).all(repliesFromSelfParams);
|
||||
|
||||
const repliesLookup = new Set(replies.map(row => row.storyId));
|
||||
const repliesFromSelfLookup = new Set(
|
||||
repliesFromSelf.map(row => row.storyId)
|
||||
);
|
||||
|
||||
return rows.map(row => ({
|
||||
...jsonToObject(row.json),
|
||||
hasReplies: row.hasReplies !== 0,
|
||||
hasRepliesFromSelf: row.hasRepliesFromSelf !== 0,
|
||||
hasReplies: Boolean(repliesLookup.has(row.id)),
|
||||
hasRepliesFromSelf: Boolean(repliesFromSelfLookup.has(row.id)),
|
||||
}));
|
||||
}
|
||||
|
||||
|
|
31
ts/sql/migrations/1130-isStory-index.ts
Normal file
31
ts/sql/migrations/1130-isStory-index.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { Database } from '@signalapp/better-sqlite3';
|
||||
|
||||
import type { LoggerType } from '../../types/Logging';
|
||||
|
||||
export const version = 1130;
|
||||
|
||||
export function updateToSchemaVersion1130(
|
||||
currentVersion: number,
|
||||
db: Database,
|
||||
logger: LoggerType
|
||||
): void {
|
||||
if (currentVersion >= 1130) {
|
||||
return;
|
||||
}
|
||||
|
||||
db.transaction(() => {
|
||||
// This is to improve the performance of getAllStories
|
||||
db.exec(`
|
||||
CREATE INDEX messages_isStory
|
||||
ON messages(received_at, sent_at)
|
||||
WHERE isStory = 1;
|
||||
`);
|
||||
})();
|
||||
|
||||
db.pragma('user_version = 1130');
|
||||
|
||||
logger.info('updateToSchemaVersion1130: success!');
|
||||
}
|
|
@ -88,10 +88,11 @@ import { updateToSchemaVersion1080 } from './1080-nondisappearing-addressable';
|
|||
import { updateToSchemaVersion1090 } from './1090-message-delete-indexes';
|
||||
import { updateToSchemaVersion1100 } from './1100-optimize-mark-call-history-read-in-conversation';
|
||||
import { updateToSchemaVersion1110 } from './1110-sticker-local-key';
|
||||
import { updateToSchemaVersion1120 } from './1120-messages-foreign-keys-indexes';
|
||||
import {
|
||||
updateToSchemaVersion1120,
|
||||
updateToSchemaVersion1130,
|
||||
version as MAX_VERSION,
|
||||
} from './1120-messages-foreign-keys-indexes';
|
||||
} from './1130-isStory-index';
|
||||
|
||||
function updateToSchemaVersion1(
|
||||
currentVersion: number,
|
||||
|
@ -2044,9 +2045,11 @@ export const SCHEMA_VERSIONS = [
|
|||
updateToSchemaVersion1070,
|
||||
updateToSchemaVersion1080,
|
||||
updateToSchemaVersion1090,
|
||||
|
||||
updateToSchemaVersion1100,
|
||||
updateToSchemaVersion1110,
|
||||
updateToSchemaVersion1120,
|
||||
updateToSchemaVersion1130,
|
||||
];
|
||||
|
||||
export class DBVersionFromFutureError extends Error {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue