SQL VORHANDEN Leistung
-
12-10-2019 - |
Frage
Ich verstehe, dass SQL EXISTS
überprüft das Vorhandensein von Zeilen, jedoch ist es den gesamten Ausdruck zu bewerten? So würde zum Beispiel so etwas wie folgt aus:
IF EXISTS (SELECT TOP 1 1 FROM table WITH (NOLOCK))
BEGIN
...
END
Seien Sie schneller als so etwas wie folgt aus:
IF EXISTS (SELECT 1 FROM table WITH (NOLOCK))
BEGIN
...
END
Lösung
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!
Andere Tipps
Exists will stop after the first hit because then the expression evaluates to true, so the top(1)-part is unnecessary.
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.