February 14, 2008 at 1:18 am
No idea, sorry.
If you just do a straight Select 1 FROM a, do you get the same error?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 14, 2008 at 3:31 am
Yes, the same error is raised.
I posted a comment on the blog, maybe Conor will be able to explain this.
Thanks!
Best Regards,
Chris Büttner
February 14, 2008 at 2:47 pm
Probably a fairly minor point but even a very very small difference can be worth it when you have a piece of code being run thousands of times a second.
Also another good place to use a 1 instead of a * is when doing a count.
For example
SELECT COUNT(1) FROM sysobjects
vs
SELECT COUNT(*) FROM sysobjects
Again a fairly minor difference but one that can have a large impact if the code is run often enough.
Kenneth
Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]
June 13, 2014 at 4:24 am
Hi All
I have a question like if we use
(SELECT top 1 1 from) instead of (SELECT 1 from) will it contribute in some sort of improvement.
I am talking about EXISTS here.
Thanks
Satvir
June 13, 2014 at 4:26 am
It will not.
Please in future post new questions in a new thread. Thanks
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 17, 2014 at 7:19 am
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)
June 17, 2014 at 8:10 am
Would a post from Gail's blog help?
June 17, 2014 at 8:17 am
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.
June 17, 2014 at 8:37 am
Eugene Elutin (6/17/2014)
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.
June 17, 2014 at 9:27 am
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...
Viewing 10 posts - 16 through 24 (of 24 total)
You must be logged in to reply to this topic. Login to reply