Add indices to improve messages_on_delete trigger
This commit is contained in:
parent
189a8a0877
commit
7ef2a9155c
4 changed files with 198 additions and 110 deletions
32
ts/sql/migrations/1090-message-delete-indexes.ts
Normal file
32
ts/sql/migrations/1090-message-delete-indexes.ts
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
// 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 = 1090;
|
||||||
|
|
||||||
|
export function updateToSchemaVersion1090(
|
||||||
|
currentVersion: number,
|
||||||
|
db: Database,
|
||||||
|
logger: LoggerType
|
||||||
|
): void {
|
||||||
|
if (currentVersion >= 1090) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
db.transaction(() => {
|
||||||
|
db.exec(`
|
||||||
|
CREATE INDEX reactions_messageId
|
||||||
|
ON reactions (messageId);
|
||||||
|
|
||||||
|
CREATE INDEX storyReads_storyId
|
||||||
|
ON storyReads (storyId);
|
||||||
|
`);
|
||||||
|
})();
|
||||||
|
|
||||||
|
db.pragma('user_version = 1090');
|
||||||
|
|
||||||
|
logger.info('updateToSchemaVersion1090: success!');
|
||||||
|
}
|
|
@ -83,10 +83,11 @@ import { updateToSchemaVersion1040 } from './1040-undownloaded-backed-up-media';
|
||||||
import { updateToSchemaVersion1050 } from './1050-group-send-endorsements';
|
import { updateToSchemaVersion1050 } from './1050-group-send-endorsements';
|
||||||
import { updateToSchemaVersion1060 } from './1060-addressable-messages-and-sync-tasks';
|
import { updateToSchemaVersion1060 } from './1060-addressable-messages-and-sync-tasks';
|
||||||
import { updateToSchemaVersion1070 } from './1070-attachment-backup';
|
import { updateToSchemaVersion1070 } from './1070-attachment-backup';
|
||||||
|
import { updateToSchemaVersion1080 } from './1080-nondisappearing-addressable';
|
||||||
import {
|
import {
|
||||||
updateToSchemaVersion1080,
|
updateToSchemaVersion1090,
|
||||||
version as MAX_VERSION,
|
version as MAX_VERSION,
|
||||||
} from './1080-nondisappearing-addressable';
|
} from './1090-message-delete-indexes';
|
||||||
|
|
||||||
function updateToSchemaVersion1(
|
function updateToSchemaVersion1(
|
||||||
currentVersion: number,
|
currentVersion: number,
|
||||||
|
@ -2038,6 +2039,7 @@ export const SCHEMA_VERSIONS = [
|
||||||
updateToSchemaVersion1060,
|
updateToSchemaVersion1060,
|
||||||
updateToSchemaVersion1070,
|
updateToSchemaVersion1070,
|
||||||
updateToSchemaVersion1080,
|
updateToSchemaVersion1080,
|
||||||
|
updateToSchemaVersion1090,
|
||||||
];
|
];
|
||||||
|
|
||||||
export class DBVersionFromFutureError extends Error {
|
export class DBVersionFromFutureError extends Error {
|
||||||
|
|
|
@ -39,7 +39,6 @@ describe('SQL/updateToSchemaVersion1080', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Addressable Messages', () => {
|
describe('Addressable Messages', () => {
|
||||||
describe('Storing of new attachment jobs', () => {
|
|
||||||
it('returns only incoming/outgoing messages', () => {
|
it('returns only incoming/outgoing messages', () => {
|
||||||
const conversationId = generateGuid();
|
const conversationId = generateGuid();
|
||||||
const otherConversationId = generateGuid();
|
const otherConversationId = generateGuid();
|
||||||
|
@ -163,5 +162,4 @@ describe('SQL/updateToSchemaVersion1080', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
56
ts/test-node/sql/migration_1090_test.ts
Normal file
56
ts/test-node/sql/migration_1090_test.ts
Normal 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=?)'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue