Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 December 6th, 2007, 07:34 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default if exists()--1 Question Many Solutions-Best One?

Hi, I have one query about how to check wether record exists or not with particular UserId
following are the options.I have used function for this.

Case-I

if exists(select productID from ProductShipped where customerId=10)
  begin
   //for customerID=10 at least one product is shipped
   return 1
  end
return 0

Case-II
if exists(select top 1 productID from ProductShipped where customerId=10)
  begin
   //for customerID=10 at least one product is shipped
   return 1
  end

return 0


Case-III
if exists(select top 1 1 from ProductShipped where customerId=10)
  begin
   //for customerID=10 at least one product is shipped
   return 1
  end

return 0

Which option should be the best option for query optimization.

assuming that ProductShipped table contains 100K of records.

One more thing ,when we use
 select top 1 ProductID from productShipped
does SQL Server internally create temporary table? and from there it shows top 1 record??

In above query where clause contains only one conditions while in real life situation it actually
contains more conditions

Any pointer that help me to select the best option.

Thanks in Advance



Cheers :)

vinod
__________________
Cheers :)

vinod
 
Old December 6th, 2007, 07:53 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

AFAIK, it doesn't need a temp temp table. All it needs to know is whether a record matching your criteria exists. So, as soon as it finds the first record, it can return true without bothering with the actual select statement.

I could be wrong, but I don't think you need the TOP 1 clause. After all, again all that you need to know is whether a record exists or not.....

You could even do this:

IF EXISTS (SELECT 1 FROM ProductShipped WHERE customerId = 10)

Since you only need to get True or False, there's no need to specify a column.

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old December 6th, 2007, 07:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Actually,

If EXISTS (SELECT * FROM ProductShipped WHERE customerId = 10)

is in general more efficient.

The EXISTS predicate returns TRUE when the subquery given as its argument has any rows in it at all. The SELECT * allows the optimizer to select which column to use to satisfy the query, rather than directing it to use a specific one.

If your ProductShipped table has an index on the customerID column, then the optimizer can determine that the subquery has rows in it from the index, without having to actually SELECT a column from the table itself, which you force it to do by specifying the column in the SELECT clause.

The TOP 1 clause is very much worse, as that requires that the subquery fully generate all its rows before the result is limited by the TOP expression.

(Note that SELECT * in the EXISTS predicate subquery is an exception to the "rule" that says you really shouldn't use SELECT * in your queries, but rather always explicitly specify the columns you want selected)

Jeff Mason
[email protected]
 
Old December 6th, 2007, 08:30 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii Imar and Jeff !!

Great Help from you.

Thank you very much.



Cheers :)

vinod





Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question: check if table exists in DB savoym Java Databases 4 March 9th, 2012 06:57 AM
Get here your complete solutions in C++! Thomos C++ Programming 0 August 2nd, 2007 08:23 AM
where to get the Exercise Solutions? ke3pup BOOK: Beginning Visual C++ 6 1 August 19th, 2006 05:33 PM
Exercise Solutions AlreadyDead BOOK: Beginning Access VBA 6 February 26th, 2006 04:23 PM





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