Solved

slow delete query

Posted on 2009-03-31
11
404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 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