<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>beer planet &#187; lock</title>
	<atom:link href="http://beerpla.net/tag/lock/feed/" rel="self" type="application/rss+xml" />
	<link>http://beerpla.net</link>
	<description>where things have nothing to do with beer - tutorials, tips, how-tos, thoughts, hacks, and other techy nonsense</description>
	<lastBuildDate>Sun, 08 Aug 2010 23:59:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<atom:link rel='hub' href='http://beerpla.net/?pushpress=hub'/>
		<item>
		<title>[MySQL] Deleting/Updating Rows Common To 2 Tables &#8211; Speed And Slave Lag Considerations</title>
		<link>http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/</link>
		<comments>http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/#comments</comments>
		<pubDate>Mon, 11 May 2009 16:00:00 +0000</pubDate>
		<dc:creator>Artem Russakovskii</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[common]]></category>
		<category><![CDATA[deadlock]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[lag]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[replication]]></category>
		<category><![CDATA[row]]></category>
		<category><![CDATA[slave]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[update]]></category>
		<guid isPermaLink="false">http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/</guid>
		<description><![CDATA[<h2>Introduction</h2>
<p>A question I recently saw on Stack Overflow titled <a href="http://stackoverflow.com/questions/812512/faster-way-to-delete-matching-rows/" rel="nofollow">Faster way to delete matching [database] rows?</a> prompted me to organize my thoughts and observations on the subject and quickly jot them down here.</p>
<p>Here is the brief description of the task: say, you have 2 MySQL tables <em>a</em> and <em>b</em>. The tables contain the same type of data, for example log entries. Now you want to delete all or a subset of the entries in table <em>a</em> that exist in table <em>b</em>.</p>
<h2>Solutions Suggested By Others</h2>
<div class="wp_syntax"><div class="code"><pre>DELETE FROM a WHERE EXISTS (SELECT b.id FROM b WHERE</pre></div></div><p>...<div class=clear></div> <a href="http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/" class="read_more"><div class=excerpt-end>Read the rest of this article &#187;</div></a></p>]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>A question I recently saw on Stack Overflow titled <a href="http://stackoverflow.com/questions/812512/faster-way-to-delete-matching-rows/" rel="nofollow">Faster way to delete matching [database] rows?</a> prompted me to organize my thoughts and observations on the subject and quickly jot them down here.</p>
<p>Here is the brief description of the task: say, you have 2 MySQL tables <em>a</em> and <em>b</em>. The tables contain the same type of data, for example log entries. Now you want to delete all or a subset of the entries in table <em>a</em> that exist in table <em>b</em>.</p>
<h2>Solutions Suggested By Others</h2>
<div class="wp_syntax"><div class="code"><pre>DELETE FROM a WHERE EXISTS (SELECT b.id FROM b WHERE b.id = a.id);</pre></div></div>
<div class="wp_syntax"><div class="code"><pre>DELETE a FROM a INNER JOIN b on a.id=b.id;</pre></div></div>
<div class="wp_syntax"><div class="code"><pre>DELETE FROM a WHERE id IN (SELECT id FROM b)</pre></div></div>
<h2>The Problem With Suggested Solutions</h2>
<p>Solutions above are all fine if the tables are quite small and the SELECT/JOIN is fast. However, in large scale situations with replication, these queries could potentially lock up the tables from writes and severely aggravate slave lag because, as I mentioned in the <a href="http://beerpla.net/2008/09/05/mysql-slave-lag-delay-explained-and-7-ways-to-battle-it/">MySQL Slave Lag (Delay) Explained And 7 Ways To Battle It</a> post, replication is single-threaded.</p>
<p>Thus, if a single UPDATE/DELETE query takes a considerable amount of time, when it propagates to the slaves, they will be stuck executing it and doing nothing else, lagging behind more and more.</p>
<h2>My Thoughts And Solution</h2>
<p>I have personally dealt with having to delete many rows from one table that exist in another and in my experience it&#039;s best to do the following, especially if you expect lots of rows to be deleted. This technique most importantly will improve replication slave lag.</p>
<p>So, here it is: <strong>do a SELECT first, as a separate query</strong>, remembering the IDs returned in your script/application, then continue on deleting in batches (say, 50,000 rows at a time). This will achieve the following:</p>
<ul>
<li><em>each one of the delete statements will not lock the table for too long, thus not letting replication lag get out of control</em>. It is especially important if you rely on your replication to provide you relatively up-to-date data. The benefit of using batches is that if you find that each DELETE query still takes too long, you can adjust it to be smaller without touching any DB structures. </li>
<li>another benefit of using a separate SELECT is that <em>the SELECT itself might take a long time to run</em>, especially if it can&#039;t for whatever reason use the best DB indexes. If the SELECT is inner to a DELETE, when the whole statement migrates to the slaves, it will have to do the SELECT all over again, potentially lagging the slaves because of how long that SELECT will take. If you use a separate SELECT query, this problem goes away, as all you&#039;re passing to the DELETE query is a list of IDs. </li>
</ul>
<p>Do you have another opinion or see a fault with my logic? Feel free to share in the comments.</p>
<p>P.S. One thing to be careful about is, of course, potential edits to the table between the times the SELECT finishes and DELETEs start. I will let you handle such details by using transactions and/or logic pertinent to your application.</p>
<div class="shr-bookmarks shr-bookmarks-expand">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Consider%5B..%5D+-+http://bit.ly/atwEQy&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;t=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-hackernews">
			<a href="http://news.ycombinator.com/submitlink?u=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;t=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations" rel="nofollow" class="external" title="Submit this to Hacker News">Submit this to Hacker News</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;title=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;title=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;title=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;title=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22%5BMySQL%5D%20Deleting%2FUpdating%20Rows%20Common%20To%202%20Tables%20-%20Speed%20And%20Slave%20Lag%20Considerations%22&amp;body=Link: http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Introduction%20%20A%20question%20I%20recently%20saw%20on%20Stack%20Overflow%20titled%20Faster%20way%20to%20delete%20matching%20%5Bdatabase%5D%20rows%3F%20prompted%20me%20to%20organize%20my%20thoughts%20and%20observations%20on%20the%20subject%20and%20quickly%20jot%20them%20down%20here.%20%20Here%20is%20the%20brief%20description%20of%20the%20task%3A%20say%2C%20you%20have%202%20MySQL%20tables%20a%20and%20b.%20The%20ta" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
Similar Posts:<ul><li><a href="http://beerpla.net/2008/09/05/mysql-slave-lag-delay-explained-and-7-ways-to-battle-it/" rel="bookmark" title="September 5, 2008">MySQL Slave Lag (Delay) Explained And 7 Ways To Battle It</a></li>
<li><a href="http://beerpla.net/2008/04/15/mysql-conference-liveblogging-explain-demystified-tuesday-200p/" rel="bookmark" title="April 15, 2008">MySQL Conference Liveblogging: EXPLAIN Demystified (Tuesday 2:00PM)</a></li>
<li><a href="http://beerpla.net/2008/04/17/mysql-conference-liveblogging-mysql-hidden-treasures-thursday-1155pm/" rel="bookmark" title="April 17, 2008">MySQL Conference Liveblogging: MySQL Hidden Treasures (Thursday 11:55PM)</a></li>
<li><a href="http://beerpla.net/2009/03/18/mysql-indexing-considerations-of-implementing-a-priority-field-in-your-application/" rel="bookmark" title="March 18, 2009">MySQL Indexing Considerations Of Implementing A Priority Field In Your Application</a></li>
<li><a href="http://beerpla.net/2010/01/05/how-to-fix-intermittent-mysql-errcode-13-errors-on-windows/" rel="bookmark" title="January 5, 2010">How To Fix Intermittent MySQL Errcode 13 Errors On Windows</a></li>
</ul><!-- Similar Posts took 12.782 ms --><p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://beerpla.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
