简体   繁体   中英

before delete trigger low performance

I am writing a C++ application in QT Creator also using an SQLite database connection. What I want to achieve is that when I delete a row from a table, rows from other tables that refer to it are deleted too. So in my case if I delete a row from Basket, referring rows from Computer are also deleted, then from subsystemtable, subscriber, regulations and so on until the end.

Here is my database diagram:

数据库图

I have created triggers for each table that have foreign key references with another tables.

CREATE TRIGGER delete_fk BEFORE DELETE ON Basket
FOR EACH ROW BEGIN 
    DELETE FROM Computer WHERE computer.basket = old.id_basket;
END

CREATE TRIGGER delete_fk_comp BEFORE DELETE ON computer
FOR EACH ROW BEGIN
    DELETE FROM subsystemtable WHERE subsystemtable.computer = id.id_computer;
END

CREATE TRIGGER delete_fk_subscriber_map BEFORE DELETE ON subscriber
FOR EACH ROW BEGIN 
    DELETE FROM mappingtable WHERE mappingtable.subscriber = old.id_subscriber;
END

CREATE TRIGGER delete_fk_subscriber_indirect BEFORE DELETE ON subscriber
FOR EACH ROW BEGIN
    DELETE FROM indirectsubscriber WHERE indirectsubscriber.subscriber = old.id_subscriber;
END

CREATE TRIGGER delete_fk_subscriber BEFORE DELETE ON subscriber
FOR EACH ROW BEGIN
    DELETE FROM reserve WHERE reserve.subscriber = old.id_subscriber;
END

CREATE TRIGGER delete_fk_subscriber_res BEFORE DELETE ON subscriber
FOR EACH ROW BEGIN
    DELETE FROM reserve WHERE reserve.subscriber_res = old.id_subscriber;
END

CREATE TRIGGER delete_fk_subs BEFORE DELETE ON subsystem
FOR EACH ROW BEGIN
    DELETE FROM subsystemtable WHERE subsystemtable.subsystem = old.id_subsystem;
END

CREATE TRIGGER delete_fk_substable_reg BEFORE DELETE ON subsystemtable
FOR EACH ROW BEGIN
    DELETE FROM regulations WHERE regulations.subsystem = old.id;
END

CREATE TRIGGER delete_fk_substable BEFORE DELETE ON subsystemtable
FOR EACH ROW BEGIN 
    DELETE FROM subscriber WHERE subscriber.subsystem = old.id;
END

I got what I wanted. And it works exactly like I wanted. But my problem now is the low performance. So in my database I have about 20 computers for each basket, then about 5 subsystems for each computer, 100 subscribers for each subsystem and so on. When i delete 1 single basket the whole procedure takes about 400 ms. Should I delete 3 baskets at once, it will take more than 3 seconds already.

I need to find a way for this to work way faster. Limited by about 200-300 ms, I guess. And I need any suggestions about how can I do it.

You are implementing foreign keys by hand. This is not necessary; you can just use ON DELETE actions to let the DB do the appropriate changes.

Whether you are implementing foreign keys manually or with the help of the DB, you need to create indexes on certain key columns , or all the lookups needed to find related rows will be slow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM