Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 25th, 2003, 12:31 AM
Authorized User
 
Join Date: Jun 2003
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Default 'exists' dilemma

hello people,

someone told me that this statement:

if exists (select top 1 1 from customers) ...

is equivalent in performance to the next one

if exists (select * from customers)

this means that the rows from customers are not actually retrieved when using this last statement (or this is what i've heard).

i couldn't find anything on bol to convince me to use * instead of top 1 1, so i'm asking for your opinion here

thank you,

defiant
 
Old June 25th, 2003, 12:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 215
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If I run:
Code:
IF EXISTS (SELECT TOP 1 NULL
           FROM SomeTable)
PRINT 1

IF EXISTS (SELECT NULL
           FROM SomeTable)
PRINT 2

IF EXISTS (SELECT *
           FROM SomeTable)
PRINT 3
in SQL Server 7 with show execution plan I get that the top query takes only 6.95% of the time and the other 2 each take half of the remaining time.

regards
David Cameron
 
Old June 25th, 2003, 01:14 AM
Authorized User
 
Join Date: Jun 2003
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i did the same thing in sql server 2000 and for all 3 queries i see:

Query cost (relative to the batch): 33.33%

defiant.
 
Old June 25th, 2003, 01:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 215
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think there is your answer. In SQL Server the optimiser has converted all three to the same execution plan. In SQL Server 7 the optimiser is not as good and hasn't.

regards
David Cameron
 
Old June 27th, 2003, 07:26 AM
Authorized User
 
Join Date: Jun 2003
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes, i guess that's it.

thanks,

defiant.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Runat server dilemma ? rsearing ASP.NET 2.0 Basics 1 November 24th, 2006 04:10 AM
Web Service Enhancements dilemma sabansingh C# 3 July 11th, 2006 03:07 AM
OpenOffice List Dilemma AForgue XSLT 1 January 29th, 2004 11:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.