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 ««12

Delete Records Expand / Collapse
Author
Message
Posted Saturday, February 20, 2010 1:14 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:45 AM
Points: 40,456, Visits: 36,911
eddy-644184 (2/20/2010)
Didnt know that about the select * vs the select Key
But i try to minimize the use of select *, so its kinda a habit for just typing what i really need


I tend to write EXISTS (SELECT 1 FROM SomeTable ...), to make it clear that no columns are returned or necessary

Could you just confirm my statement on the use of select TOP 1 @PARAM = Value or am i wrong there .?


It'll work without the TOP 1, if the query returns more than 1 row, you'll get one of them, it isn't guaranteed which one. But then, if you use TOP 1 without an order by, it isn't guaranteed which one you'll get either.

SET @var = (SELECT value FROM ...) will throw an error if the query returns more than one row.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #869708
Posted Saturday, February 20, 2010 1:21 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:07 PM
Points: 7,161, Visits: 15,649
John Paul -

SQL Server allows you to use joins in your DELETE statements, so perhaps having EXISTS is not necessary.

Assuming the primary key in both tables is Pkey, you could use something like:

DELETE Table1
from
Table1 inner join
(Select * from Table1
INTERSECT
SELECT * from Table2
) DupeList on Table1.pkey=DupeList.Pkey

Note: you want to use EXPLICIT column lists instead of * in the INTERSECT statement, just to make sure that the columns are appropriately lined up with each other.


----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #869709
Posted Tuesday, February 23, 2010 2:39 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, November 25, 2014 1:49 AM
Points: 610, Visits: 291
Hi Gail,

GilaMonster (2/20/2010)
Hunterwood (2/16/2010)
It´s allways a good rule of thumb to use TOP 1 together with EXISTS, because it prevents the database engine from doing unneccesary work.


Got an example that proves that?



No. I don't. But in some situations when trying to optimize queries, I have seen better performace when changing from "exists (select * from..." to "exists (select TOP 1 1 from ...". Don't have any specific example, though.

My conclusion is also based on the article "Time Bomb Coding" by David Poole: http://www.sqlservercentral.com/articles/Performance+Tuning/69337/, wich addresses this topic (among others).


If the databases are small the performance difference might not be so huge, but it´s allways a good habbit.

A good habit is to test alternatives and see exactly what the performance difference really is, if there's one in the first place.


Your'e right. Never knows what works best in a specific case without testing alternatives.
I obviously was too fast to make my conclusion that it's allways better. Thanks for the good analyze and example!

What is your opinion on the article?
Have you ever seen a performance problem using the code bellow (copied from the article)?

IF ( SELECT COUNT(*) FROM .... WHERE ...) > 0
BEGIN
...etc
END

/Markus
Post #870996
Posted Tuesday, February 23, 2010 10:36 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:45 AM
Points: 40,456, Visits: 36,911
Hunterwood (2/23/2010)
Have you ever seen a performance problem using the code bellow (copied from the article)?

IF ( SELECT COUNT(*) FROM .... WHERE ...) > 0
BEGIN
...etc
END



Not on SQL 2005/2008. It may be that there's a difference in earlier versions that has since changed. In 2005/2008, the parser/optimiser is smart enough to realise that the above construct is an EXISTS, phrased differently.

If the count and the IF are separate statements, then there is an obvious and blatant performance difference



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #871316
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse