Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SP with multiple tables and databases (insert & delete)
Message
De
21/04/2003 08:30:21
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Stored procedures, Triggers, UDFs
Divers
Thread ID:
00758108
Message ID:
00779718
Vues:
16
This message has been marked as a message which has helped to the initial question of the thread.
>Sorry for the old thread. I was just re-reading it and I have a question:
>
>would it be better to change
>
IF EXISTS (SELECT * FROM cart WHERE cartid = @cartID)
>
>to
IF EXISTS (SELECT CartID FROM cart WHERE cartid = @cartID)
>
>Thanks a lot in advance.

For this example, it probably doesn't matter since you have to have cartid anyhow.

Now, if you're options were:

IF EXISTS(SELECT * FROM cart WHERE cartid = @cartID)

OR

IF (SELECT COUNT(*) FROM cart WHERE cartid = @cartID) > 0

OR

IF (SELECT COUNT(cartID) FROM cart WHERE cartid = @cartID) > 0

OR

IF (SELECT COUNT(anothercolumn) FROM cart WHERE cartid = @cartID) > 0

It's a different matter. The EXISTS() will always provide better performance since SQL Server can stop at the first row that qualifies and declare a TRUE condition.

The second and third examples should perform the same, as long as there are no NULLs in the cartID column.

I would expect the second and third examples to perform better then the fourth since both the second and third only reference one column and it is more likely that there is a non-clustered index that covers the queries.

-Mike
Michael Levy
MCSD, MCDBA
ma_levy@hotmail.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform