Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete with NOT IN cause performance issue

Status
Not open for further replies.

fredong

IS-IT--Management
Sep 19, 2001
332
US
Hi,
I am trying to replace the Delete statement because it causedperformance issue and take up CPU. I try to replace the using 'NOT IN' I am not sure anybody has any advice for me. Thanks.


delete from @AA
where orderid not in
(
select max(orderid) as orderID
from @AA
group by BaseID
 
Look up JOIN in Books Online. It should give you details. You have all the information there on what you're joining everything too.

Hint, the columns you are joining on are in all the WHERE clauses of the 3rd to last post you made.




Catadmin - MCDBA, MCSA
"No, no. Yes. No, I tried that. Yes, both ways. No, I don't know. No again. Are there any more questions?"
-- Xena, "Been There, Done That"
 
I need example. Can somebody helps? Thanks.
 
Hi bborissov,
I tried your solution and the delete solution that you provided deleted more rows than my original statment. Do you know why?

--your solution

DECLARE @Aa TABLE (BaseID int, OrderId int)
INSERT INTO @Aa VALUES (1,1)
INSERT INTO @Aa VALUES (1,2)
SELECT * from @AA

Delete from @AA
FROM @Aa Aa
LEFT JOIN
(SELECT BaseID, MAX(OrderId) AS OrderId
FROM @Aa
GROUP BY BaseId) Tbl1
ON Aa.BaseId = Tbl1.BaseId AND
Aa.OrderId = Tbl1.OrderId
where Tbl1.BaseId IS NULL
SELECT * from @AA


--my original statement


delete from @AA
where orderid not in
(
select max(orderid) as orderID
from @AA
group by BaseID
 
For a bit the FROM FROM was freaking me out, then I remembered that I don't use the first FROM ever, so never mind!

Code:
DELETE FROM X WHERE <-- yuck
DELETE X WHERE <-- I like this

--makes sense
DELETE X
FROM
   MyTable X
   INNER JOIN Y ON Blah

--weird to me
DELETE FROM X FROM MyTable X
   INNER JOIN Y ON Blah
 
Esquared,

Either your "makes sense" code or my FROM FROM code will work. I just like being thorough so if another DBA has to look at my code he/she doesn't get confused by what I'm doing because I didn't show my work.

Good grief, the math classes I took in High School have completely messed with my head. "Show your work! Show your work!" @=)



Catadmin - MCDBA, MCSA
"No, no. Yes. No, I tried that. Yes, both ways. No, I don't know. No again. Are there any more questions?"
-- Xena, "Been There, Done That"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top