gpt4 book ai didi

subquery - 如何在 Firebird 中优化 DELETE .. NOT IN .. SUBQUERY

转载 作者:行者123 更新时间:2023-12-04 15:13:55 27 4
gpt4 key购买 nike

我有这种删除查询:

DELETE 
FROM SLAVE_TABLE
WHERE ITEM_ID NOT IN (SELECT ITEM_ID FROM MASTER_TABLE)

有没有办法优化这个?

最佳答案

您可以使用 EXECUTE BLOCK 顺序扫描明细表并删除没有主记录匹配的记录。

EXECUTE BLOCK
AS
DECLARE VARIABLE C CURSOR FOR
(SELECT d.id
FROM detail d LEFT JOIN master m
ON d.master_id = m.id
WHERE m.id IS NULL);
DECLARE VARIABLE I INTEGER;
BEGIN
OPEN C;
WHILE (1 = 1) DO
BEGIN
FETCH C INTO :I;
IF(ROW_COUNT = 0)THEN
LEAVE;
DELETE FROM detail WHERE id = :I;
END
CLOSE C;
END

关于subquery - 如何在 Firebird 中优化 DELETE .. NOT IN .. SUBQUERY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12442659/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com