getAllStories: Remove nested queries
This commit is contained in:
parent
a795602e19
commit
95209689a8
5 changed files with 328 additions and 34 deletions
145
ts/test-node/sql/migration_1130_test.ts
Normal file
145
ts/test-node/sql/migration_1130_test.ts
Normal file
|
@ -0,0 +1,145 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { assert } from 'chai';
|
||||
import type { WritableDB } from '../../sql/Interface';
|
||||
import { createDB, updateToVersion } from './helpers';
|
||||
|
||||
describe('SQL/updateToSchemaVersion1130', () => {
|
||||
let db: WritableDB;
|
||||
beforeEach(() => {
|
||||
db = createDB();
|
||||
updateToVersion(db, 1130);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
db.close();
|
||||
});
|
||||
|
||||
it('uses new index for getAllStories query and no params', () => {
|
||||
const details = db
|
||||
.prepare(
|
||||
`
|
||||
EXPLAIN QUERY PLAN
|
||||
SELECT json, id
|
||||
FROM messages
|
||||
WHERE
|
||||
isStory = 1 AND
|
||||
(NULL IS NULL OR conversationId IS NULL) AND
|
||||
(NULL IS NULL OR sourceServiceId IS NULL)
|
||||
ORDER BY received_at ASC, sent_at ASC;
|
||||
`
|
||||
)
|
||||
.all()
|
||||
.map(step => step.detail)
|
||||
.join(', ');
|
||||
|
||||
assert.strictEqual(details, 'SCAN messages USING INDEX messages_isStory');
|
||||
});
|
||||
|
||||
it('uses new index for getAllStories query and with conversationId', () => {
|
||||
const details = db
|
||||
.prepare(
|
||||
`
|
||||
EXPLAIN QUERY PLAN
|
||||
SELECT json, id
|
||||
FROM messages
|
||||
WHERE
|
||||
isStory = 1 AND
|
||||
('something' IS NULL OR conversationId IS 'something') AND
|
||||
(NULL IS NULL OR sourceServiceId IS NULL)
|
||||
ORDER BY received_at ASC, sent_at ASC;
|
||||
`
|
||||
)
|
||||
.all()
|
||||
.map(step => step.detail)
|
||||
.join(', ');
|
||||
|
||||
assert.strictEqual(details, 'SCAN messages USING INDEX messages_isStory');
|
||||
});
|
||||
|
||||
it('uses new index for getAllStories query and with sourceServiceId', () => {
|
||||
const details = db
|
||||
.prepare(
|
||||
`
|
||||
EXPLAIN QUERY PLAN
|
||||
SELECT json, id
|
||||
FROM messages
|
||||
WHERE
|
||||
isStory = 1 AND
|
||||
(NULL IS NULL OR conversationId IS NULL) AND
|
||||
('something' IS NULL OR sourceServiceId IS 'something')
|
||||
ORDER BY received_at ASC, sent_at ASC;
|
||||
`
|
||||
)
|
||||
.all()
|
||||
.map(step => step.detail)
|
||||
.join(', ');
|
||||
|
||||
assert.strictEqual(details, 'SCAN messages USING INDEX messages_isStory');
|
||||
});
|
||||
|
||||
it('uses new index for getAllStories query and both params', () => {
|
||||
const details = db
|
||||
.prepare(
|
||||
`
|
||||
EXPLAIN QUERY PLAN
|
||||
SELECT json, id
|
||||
FROM messages
|
||||
WHERE
|
||||
isStory = 1 AND
|
||||
('something' IS NULL OR conversationId IS 'something') AND
|
||||
('something' IS NULL OR sourceServiceId IS 'something')
|
||||
ORDER BY received_at ASC, sent_at ASC;
|
||||
`
|
||||
)
|
||||
.all()
|
||||
.map(step => step.detail)
|
||||
.join(', ');
|
||||
|
||||
assert.strictEqual(details, 'SCAN messages USING INDEX messages_isStory');
|
||||
});
|
||||
|
||||
it('uses previous index for getAllStories get replies query', () => {
|
||||
const details = db
|
||||
.prepare(
|
||||
`
|
||||
EXPLAIN QUERY PLAN
|
||||
SELECT DISTINCT storyId
|
||||
FROM messages
|
||||
WHERE storyId IS NOT NULL
|
||||
`
|
||||
)
|
||||
.all()
|
||||
.map(step => step.detail)
|
||||
.join(', ');
|
||||
|
||||
assert.strictEqual(
|
||||
details,
|
||||
'SEARCH messages USING COVERING INDEX messages_by_storyId (storyId>?)'
|
||||
);
|
||||
});
|
||||
|
||||
it('uses previous index for getAllStories get replies from self query', () => {
|
||||
const details = db
|
||||
.prepare(
|
||||
`
|
||||
EXPLAIN QUERY PLAN
|
||||
SELECT DISTINCT storyId
|
||||
FROM messages
|
||||
WHERE (
|
||||
storyId IS NOT NULL AND
|
||||
type IS 'outgoing'
|
||||
)
|
||||
`
|
||||
)
|
||||
.all()
|
||||
.map(step => step.detail)
|
||||
.join(', ');
|
||||
|
||||
assert.strictEqual(
|
||||
details,
|
||||
'SEARCH messages USING INDEX messages_by_storyId (storyId>?)'
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue