• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 410
  • Last Modified:

slow delete query

Hey,

I'm finding this query takes about 50 minutes to run. Is there a way to optimize?

delete from A
where A.createdate IN (select B.createdate from B group by B.createdate);

I need to run this query daily. Basically, B is an daily set of rows that update Table A. So I remove any rows where the date already exists in B, then add everything in B to A. Table A has 7 million rows, but is growing at 400K rows or so a day. Table B has 2 million rows.

In both cases, "createdate" is a DATE field.
0
deckard666
Asked:
deckard666
  • 4
  • 3
  • 2
  • +2
1 Solution
 
QPRCommented:
is there an index on b.createdate?
Why do you need to use group by in your sub select?
0
 
QPRCommented:
I meant A.createdate.
Also is there any delete triggers on A?
0
 
allmerCommented:
Try:

EXPLAIN
delete from A
where A.createdate IN (select B.createdate from B group by B.createdate);
AND or
EXPLAIN
select B.createdate from B group by B.createdate;

You may want to add indexes such that your SELECT query and or your where .. in
gets faster.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
deckard666Author Commented:
- I have indexes on both tables for createdate.
- No triggers
- Group by is to get distinct createdates (there are multiple rows with the same date).

Note, when I run this query without the select subquery, it runs really fast.

delete from A
where A.createdate = cast("2009-03-31 AS Date)
or A.createdate = cast("2009-03-30 AS Date) or ....
0
 
allmerCommented:
Did you try:
delete from A
where A.createdate IN (select DISTINCT B.createdate from B);
Does that speed up your query?
0
 
allmerCommented:
Did you use explain to find out whether your indexes are actually used in your query?
0
 
hc2342uhxx3vw36x96hqCommented:
Try the attached code.
DELETE FROM a
      WHERE EXISTS (SELECT 'X'
                      FROM b
                     WHERE a.createdate = b.createdate AND ROWNUM = 1);

Open in new window

0
 
deckard666Author Commented:
ROWNUM is not a feature available in MySQL 5.0. I get an error on it.
Running the distinct statement now and i'll post the explain when its done.
0
 
racekCommented:
DELETE FROM A
      WHERE EXISTS (SELECT NULL
                      FROM B
                     WHERE A.createdate = B.createdate );
0
 
allmerCommented:
Both of the below should give you only distinct rows from B and delete them from a are there any differences in performance?

I still wonder if you checked EXPLAIN (don't worry you don't have to wait 50min for that).

Also you could try to tweak MySQL (give it more memory for certain operations)


delete from A 
where A.createdate IN (
select B.createdate from B 
UNION
select B.createdate from B LIMIT 1
);
 
delete from A 
where A.createdate IN (select DISTINCT B.createdate from B);

Open in new window

0
 
deckard666Author Commented:
for some reason switching to distinct worked wonders
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now