The SELECT 1 vs. SELECT * in EXISTS conondrum

by Allan Svelmøe Hansen February 28, 2008 06:43

Recently I’ve been reading some various other blogs about this issue.
Is SELECT 1 faster (better) then SELECT * when used in an EXISTS (or similar) clause?
These are two blogs I often read which discuss the issue as well:
http://blog.sqlauthority.com/2008/02/26/sql-server-select-1-vs-select-an-interesting-observation/
and
http://www.sqlskills.com/blogs/conor/2008/02/07/EXISTSSubqueriesSELECT1VsSELECT.aspx

My own personal testing have not shown any difference between the two and my “guesstimate” is that the query optimizer in the database is clever enough to judge from the syntax that it doesn’t actually need to return anything when the SELECT * is in an EXISTS, and thus it doesn’t do much else then evaluate whether the statement is true, just as if it was SELECT 1.

I’ve looked at the query plans, just as P. Dave did (the first link), and my findings were exactly the same. No difference in execution at all.

Then I tried to use the SET STATISTICS TIME ON and SET STATISTICS IO ON combined with DBCC DROPCLEANBUFFERS and DBCC FREEPROCCACHE to clear the cache. This way I could see the time it took to handle the query and no result was conclusive.
The result varied from one case to the other case, so my own testing seems to conclude that IF there is a difference, it looks to be minor.
Granted, I used only a small number of columns to test with, ranging from 2 up to 20, with between 10.000 and 100.000 rows, so perhaps for very large sets, it would be different.

If anybody have any code which consistently shows that SELECT 1 is faster, it’d very much like to see it, because it is one of those SQL myths out there which are hard to prove/disprove.

Bookmark and Share DotnetKicks dotnetshoutout

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.1.0
Theme by Mads Kristensen | Modified by Mooglegiant

About:
Allan Svelmøe Hansen

My real name is Allan Svelmøe Hansen.
I live in Denmark, where I work as a developer for hedal:kruse:brohus using SQL Server and the .NET framework since 2004.
My primary fields of expertise is back-end data integration, database design and optimization.


       View Allan Svelmøe Hansen's profile on LinkedIn     

Disclaimer

The opinions expressed herein are my own personal opinions and thoughts and does not represent my employers view in any way, nor are my results guaranteed for all situations.
Content is presented "as is", with no warranty.