all groups > sql server (microsoft) > january 2004 >
You're in the

sql server (microsoft)

group:

SQL 2000 performance diabolical when using 'delete from ... where X in (select Y from...)'


SQL 2000 performance diabolical when using 'delete from ... where X in (select Y from...)' nojunk_fixitdik NO[at]SPAM hotmail.com
1/23/2004 2:17:53 AM
sql server (microsoft):
I get a daily feed of customer records and 3 related tables. The
customer feed is of the order 60,000 records of which only 5,000 or so
are actually of any interest (in that they have records in the related
tables). Therefore we purge all the unused customers with the
following query:

DELETE FROM customers
WHERE c_no NOT IN (SELECT DISTINCT t.c_no FROM transactions t)
AND c_no NOT IN (SELECT DISTINCT q.c_no FROM queries q)
AND c_no NOT IN (SELECT DISTINCT c.c_no FROM contacts c)

This query used to take less than 3 minutes on SQL7 but on SQL2000 is
taking 42 minutes or more.

There appear to be a few chains on this topic, but most are around
reindexing or literals. As c_no is indexed in all the tables and the
query does not involve literals, I was hoping someone could suggest a
way to speed this up?

Thanks

Re: SQL 2000 performance diabolical when using 'delete from ... where X in (select Y from...)' mountain man
1/23/2004 1:51:09 PM
[quoted text, click to view]


The performance could be environmental, however
there's a few subselects, and also a few distincts in your
code which will have some overhead.

If you've the time to test that the following code identifies
the equivalent rows out of the client table then I'd be
interested to see if it's faster.



-- delete
-- select count(*)
-- select c.c_no
from customers c
left join transactions t on (c.c_no = t.c_no)
left join queries q on (c.c_no = q.c_no)
left join contacts c2 on (c.c_no = c2.c_no)
where t.c_no is null
and q.c_no is null
and c2.c_no is null





Pete Brown
Falls Creek
Oz

Re: SQL 2000 performance diabolical when using 'delete from ... where X in (select Y from...)' nojunk_fixitdik NO[at]SPAM hotmail.com
1/27/2004 7:25:32 AM
Hi Pete,

thanks for the suggestion, I am soooo embarassed as I left out a
critical part of the story (I was trying to simplify the problem and
didn't actually test what I wrote as an example): I am also
'where'ing on a datetime field (as I get this download everynight and
have to keep a month's worth of data) so I passed the datetime as a
parameter and then used it in the queries so:

exec sp_purge_unused_customers '27 Jan 04'

and the queries became:

delete from customers c where c.c_no= .. AND download_date=@paramdate

by replacing paramdate with a local var @passeddate so:

declare @passeddate datetime
set @passeddate=@paramdate

and using @passeddate in the quereies, the stored proc ran in under 5
secs.

It may not sound like it, but I am really greatful to you for making
me revisit this with some gusto and thanks, mate, appreciate it

AddThis Social Bookmark Button