If I have a SQL query that is performing an existence check against three tables:
IF EXISTS(SELECT [KEY] FROM [Table1] WHERE [KEY]='Key1')
AND EXISTS(SELECT [KEY] FROM [Table2] WHERE [KEY]='Key2')
AND EXISTS(SELECT [KEY] FROM [Table3] WHERE [KEY]='Key3')
- Does SQL Server support 'early exit' for conditional statements, such that if the initial exists check against [Table1] returns false, the remaining two exists checks are not executed?
- Assuming Microsoft SQL Server as the backend, what locking behaviour would I expect to see on the three referenced tables, again assuming that the initial exists check against Table1 will return false?
Some basic testing using functions instead of actual queries would suggest that 'early exit' is supported, but lock analysis during query execution also suggests that a lock is acquired on all three tables which contradicts the 'early exit' findings.
Does SQL Server acquire a lock on all tables in a query, just in case it needs them later on?