0

当我尝试从具有外键约束的表中删除 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 行。为什么第一个查询需要这么长时间?

4

1 回答 1

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
于 2013-08-29T14:53:30.727 回答