当我尝试从具有外键约束的表中删除a时,需要很长时间。它在3分钟后没有完成。如果我这样做需要很长时间:
BEGIN TRANSACTION
DELETE FROM [T1]
WHERE
ROLEID = (SELECT ROLEID FROM T2
WHERE
NAME = 'GM')
ROLLBACK
但以下立即执行:
BEGIN TRANSACTION
DELETE FROM [T1]
WHERE
ROLEID = (5)
ROLLBACK
显然第二个查询没有子查询,但是我知道我的第一个查询中的子查询只返回2行。为什么第一个查询需要这么长时间?
答案 0 :(得分:1)
以下是我提出的建议:
SELECT ROLEID
INTO #RIDS
FROM APPROLES
WHERE
NAME IN ( 'GM', 'MINOR', 'USER')
DELETE T1 FROM [ROLEFUNCS] T1
INNER JOIN [#RIDS] T2 ON T2.ROLEID = T1.ROLEID
DELETE T3 FROM [USERROLES] T3
INNER JOIN [#RIDS] T2 ON T2.ROLEID = T3.ROLEID
DELETE T4 FROM [ROLES] T4
INNER JOIN [#RIDS] T2 ON T2.ROLEID = T4.ROLEID
DROP TABLE #RIDS