Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

EXISTS Subqueries: SELECT 1 vs. SELECT * (Database Weekly 11.02.2008) Expand / Collapse
Author
Message
Posted Tuesday, June 17, 2014 7:19 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, October 21, 2014 8:55 AM
Points: 2,873, Visits: 5,185
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1582322
Posted Tuesday, June 17, 2014 8:10 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 10:35 AM
Points: 3,783, Visits: 8,484
Would a post from Gail's blog help?
To TOP or not to TOP an EXISTS



Luis C.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1582368
Posted Tuesday, June 17, 2014 8:17 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, October 21, 2014 8:55 AM
Points: 2,873, Visits: 5,185
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1582370
Posted Tuesday, June 17, 2014 8:37 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 10:35 AM
Points: 3,783, Visits: 8,484
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1582383
Posted Tuesday, June 17, 2014 9:27 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, October 21, 2014 8:55 AM
Points: 2,873, Visits: 5,185
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1582408
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse