SQL EXISTS performance
I understand that SQL EXISTS
checks for the existence o开发者_运维知识库f rows, however does it evaluate the entire expression? So for example, would something like this:
IF EXISTS (SELECT TOP 1 1 FROM table WITH (NOLOCK))
BEGIN
...
END
Be faster than something like this:
IF EXISTS (SELECT 1 FROM table WITH (NOLOCK))
BEGIN
...
END
Exists will stop after the first hit because then the expression evaluates to true, so the top(1)-part is unnecessary.
Both those should run exactly the same. SQL Server takes into account that EXISTS
is a short-circuited operation and doesn't evaluate the return result, just checks to see if there IS a return result!
No, it won't.
SQL Server
uses TOP
in the plan to evaluate EXISTS
.
The statements generate identical query plans so there is no difference. The second example is easier to read in my opinion.
精彩评论