Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


EXISTS Subqueries: SELECT 1 vs. SELECT * (Database Weekly 11.02.2008)


EXISTS Subqueries: SELECT 1 vs. SELECT * (Database Weekly 11.02.2008)

Author
Message
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
I know it is a very old thread, however, quite often, this exactly discussion comes up in different work places...
Is anyone have any material evidence that one syntax is better/faster than another?
If the BoL description for COUNT(*) is not enough (where it is kind of stated that * - doesn't refer to columns in the COUNT function...), try execution plan:


SET SHOWPLAN_ALL ON
GO
SELECT COUNT(1) FROM sysobjects
SELECT COUNT(*) FROM sysobjects
GO



Any difference? I could see none! Check the underlying aggregate function - it is COUNT(*).
If not convinced, try to mesure time:


create table #results (tmms bigint)
go
declare @i bigint
declare @st datetime
set @st = getdate()
select @i = count(*) from dbo.YourLargestTable
insert #results select datediff(ms, @st, getdate())
go 100

select avg(tmms) from #results



Anything? Not even "very small difference"!
I've played quite few matches on very large partitioned and non-partitioned tables in 2005, 2008 and 2012 SQLServer...
My WorldCup's results:
COUNT(*) vs COUNT(1) = draw!

The same goes for EXISTS...
SELECT 'OK' WHERE EXISTS(SELECT 1 FROM sysobjects)
SELECT 'OK' WHERE EXISTS(SELECT NULL FROM sysobjects)
SELECT 'OK' WHERE EXISTS(SELECT * FROM sysobjects)


It doesn't matter what you use, SQL always performs Constant Scan, the rest (index or not index or which index) would be determined by the WHERE or JOIN conditions if any inside of EXISTS query.

So, looks like it is pure personal preference.
I would really appritiate if some one could teach me better (and has reproducible tests to prove its course)

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8506 Visits: 18114
Would a post from Gail's blog help?
To TOP or not to TOP an EXISTS


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
Luis Cazares (6/17/2014)
Would a post from Gail's blog help?
To TOP or not to TOP an EXISTS


Sorry Luis, help with what?
First of all it is a bit different topic. Second the conclusion there does look the same to me:
The Exists operator itself tries to retrieve jus the absolute minimum of information, so the addition of TOP 1 does nothing except add 5 characters to the query size.


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8506 Visits: 18114
Eugene Elutin (6/17/2014)
Luis Cazares (6/17/2014)
Would a post from Gail's blog help?
To TOP or not to TOP an EXISTS


Sorry Luis, help with what?
First of all it is a bit different topic. Second the conclusion there does look the same to me:
The Exists operator itself tries to retrieve jus the absolute minimum of information, so the addition of TOP 1 does nothing except add 5 characters to the query size.

I didn't remember exactly the content and made the mistake. But remembered the conclusion and seemed as an additional set of tests for the way that EXISTS work.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
Yep, Gail's article conclusion tells that it doesn't matter what you type between SELECT and FROM in EXISTS query, as long it represents valid T-SQL sytax, SQL server always does exactly the same thing.
I will not be surprised if SQL query engine (parser or compiler) strips all things typed there and replaces it with some litteral constant (most likely "SELECT *") before performing the query.
So far, all suggestions that one sytax performs better than another are just unproved speculations...

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search