SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete Records


Delete Records

Author
Message
GilaMonster
GilaMonster
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90117 Visits: 45284
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 :-D


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, MVP, M.Sc (Comp Sci)
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


Matt Miller (4)
Matt Miller (4)
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12701 Visits: 18584
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?
Hunterwood
Hunterwood
Right there with Babe
Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)

Group: General Forum Members
Points: 736 Visits: 297
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90117 Visits: 45284
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, MVP, M.Sc (Comp Sci)
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search