SQL 服务器 "Executing Query" 没完没了

SQL Server is "Executing Query" endlessly

我正在尝试 运行 一个简单的查询,但它没有返回任何结果,并且不断地显示“正在执行查询”。

这是我的代码:

select *
from ALERTTRAN_01
where PARENT_ALERT_ID not in (select PARENT_ALERT_ID
                              from nonbulkclosealert)

但是当我将代码更改为

select *
from ALERTTRAN_01
where PARENT_ALERT_ID in (select PARENT_ALERT_ID
                          from nonbulkclosealert)

注意not inin,然后运行宁可以。

感谢任何帮助。

INNOT IN 的性能可能有点棘手,因为然后在匹配记录之前首先将子查询中的所有数据加载到内存中。所以如果有更多的行,它会耗尽性能。所以尝试使用 EXISTSNOT EXISTS 代替,像这样

select *
from ALERTTRAN_01
where not EXISTS (
    select PARENT_ALERT_ID
    from nonbulkclosealert WHERE PARENT_ALERT_ID = ALERTTRAN_01.PARENT_ALERT_ID 
)

这里有一些有用的链接,它们将解释 EXISTS 相对于 IN

的优势

https://www.sqlservercentral.com/blogs/not-exists-vs-not-in

https://www.red-gate.com/hub/product-learning/sql-prompt/consider-using-not-exists-instead-not-subquery