Solved

slow delete query

Posted on 2009-03-31
11
400 Views
Last Modified: 2012-05-06
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
Comment
Question by:deckard666
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 24026510
is there an index on b.createdate?
Why do you need to use group by in your sub select?
0
 
LVL 29

Expert Comment

by:QPR
ID: 24026514
I meant A.createdate.
Also is there any delete triggers on A?
0
 
LVL 5

Expert Comment

by:allmer
ID: 24026517
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:deckard666
ID: 24026664
- 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
 
LVL 5

Expert Comment

by:allmer
ID: 24026698
Did you try:
delete from A
where A.createdate IN (select DISTINCT B.createdate from B);
Does that speed up your query?
0
 
LVL 5

Expert Comment

by:allmer
ID: 24026705
Did you use explain to find out whether your indexes are actually used in your query?
0
 
LVL 1

Expert Comment

by:hc2342uhxx3vw36x96hq
ID: 24026741
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
 

Author Comment

by:deckard666
ID: 24026992
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
 
LVL 14

Expert Comment

by:racek
ID: 24027069
DELETE FROM A
      WHERE EXISTS (SELECT NULL
                      FROM B
                     WHERE A.createdate = B.createdate );
0
 
LVL 5

Accepted Solution

by:
allmer earned 500 total points
ID: 24027204
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
 

Author Closing Comment

by:deckard666
ID: 31564712
for some reason switching to distinct worked wonders
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question