xunit delete: Testcase message performance improvements

I found that deleting an xunit from the database was very, very slow.
This patch addresses the issue by adding additional indexes on the
testcases table to make it easier to find messages that are still in
use. I also rework the `cleanup_testcase_messages` trigger to only check
messageids that were used by the deleted testcase.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-07-25 10:41:58 -04:00
parent 2082b904a0
commit a168a7f84b

View File

@ -1,3 +1,30 @@
/* Copyright 2023 (c) Anna Schumaker */
PRAGMA user_version = 2;
/*
* The original `cleanup_testcase_messages` trigger was very slow. We can
* do a few things to improve upon it:
* 1. Add indexes on the testcases table to make it easier to check
* if specific messageids are still in use.
* 2. Rewrite the `cleanup_testcase_messages` trigger to only operate
* on the messageids associated with the deleted testcase.
*/
CREATE INDEX testcases_messageid_index ON testcases (messageid);
CREATE INDEX testcases_stdoutid_index ON testcases (stdoutid);
CREATE INDEX testcases_stderrid_index ON testcases (stderrid);
DROP TRIGGER cleanup_testcase_messages;
CREATE TRIGGER cleanup_testcase_messages
AFTER DELETE ON testcases
BEGIN
DELETE FROM messages
WHERE (messageid = OLD.messageid
OR messageid = OLD.stdoutid
OR messageid = OLD.stderrid)
AND NOT EXISTS
(SELECT 1 FROM testcases WHERE
messageid = messages.messageid
OR stdoutid = messages.messageid
OR stderrid = messages.messageid);
END;