Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will "Where 0=1" parse full table or just return column names

Tags:

I came across this question:

SQL Server: Select Top 0?

I want to ask if I use the query

SELECT * FROM table WHERE 0=1 

or

SELECT TOP 0 * FROM table 

will it return just the column names instantly, or will it keep on parsing the whole table and in the end return zero results?

I have a production table with 10,000 rows - will it check the WHERE condition on each row?

like image 356
Friendy Avatar asked May 24 '16 06:05

Friendy


Video Answer


1 Answers

The SQL Server query optimizer is smart enough to figure out that this WHERE condition can never ever produce a true result on any row, so it doesn't bother actually scanning the table.

If you look at the actual execution plan for such a query, it's easy to see that nothing is being done and the query returns immediately:

enter image description here

like image 167
marc_s Avatar answered Sep 28 '22 07:09

marc_s