Add indices to improve messages_on_delete trigger

This commit is contained in:
trevor-signal 2024-06-28 20:47:05 -04:00 committed by GitHub
parent 189a8a0877
commit 7ef2a9155c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 198 additions and 110 deletions

View file

@ -0,0 +1,56 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import type { Database } from '@signalapp/better-sqlite3';
import SQL from '@signalapp/better-sqlite3';
import { updateToVersion } from './helpers';
describe('SQL/updateToSchemaVersion1090', () => {
let db: Database;
beforeEach(() => {
db = new SQL(':memory:');
updateToVersion(db, 1090);
});
afterEach(() => {
db.close();
});
describe('Additional messages_on_delete indexes', () => {
it('uses index for selecting reactions by messageId', () => {
const details = db
.prepare(
`EXPLAIN QUERY PLAN
SELECT rowid FROM reactions
WHERE messageId = '123';
`
)
.all()
.map(step => step.detail)
.join(', ');
assert.strictEqual(
details,
'SEARCH reactions USING COVERING INDEX reactions_messageId (messageId=?)'
);
});
it('uses index for selecting storyReads by storyId', () => {
const details = db
.prepare(
`EXPLAIN QUERY PLAN
DELETE FROM storyReads WHERE storyId = '123';
`
)
.all()
.map(step => step.detail)
.join(', ');
assert.strictEqual(
details,
'SEARCH storyReads USING INDEX storyReads_storyId (storyId=?)'
);
});
});
});