<?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>Thu, 17 May 2012 22:50:53 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</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 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 </pre></div>...<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></div>]]></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://www.shareaholic.com/api/share/?title=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations&amp;link=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;notes=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&amp;short_link=http://bit.ly/atwEQy&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a></li><li
class="shr-facebook"> <a
href="http://www.shareaholic.com/api/share/?title=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations&amp;link=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;notes=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&amp;short_link=http://bit.ly/atwEQy&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-googlebuzz"> <a
href="http://www.shareaholic.com/api/share/?title=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations&amp;link=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;notes=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&amp;short_link=http://bit.ly/atwEQy&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a></li><li
class="shr-reddit"> <a
href="http://www.shareaholic.com/api/share/?title=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations&amp;link=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;notes=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&amp;short_link=http://bit.ly/atwEQy&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a></li><li
class="shr-hackernews"> <a
href="http://www.shareaholic.com/api/share/?title=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations&amp;link=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;notes=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&amp;short_link=http://bit.ly/atwEQy&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=202&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to Hacker News">Submit this to Hacker News</a></li><li
class="shr-delicious"> <a
href="http://www.shareaholic.com/api/share/?title=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations&amp;link=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;notes=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&amp;short_link=http://bit.ly/atwEQy&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" 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.shareaholic.com/api/share/?title=%5BMySQL%5D+Deleting%2FUpdating+Rows+Common+To+2+Tables+-+Speed+And+Slave+Lag+Considerations&amp;link=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;notes=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&amp;short_link=http://bit.ly/atwEQy&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" 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="http://www.shareaholic.com/api/share/?title=%5BMySQL%5D%20Deleting%2FUpdating%20Rows%20Common%20To%202%20Tables%20-%20Speed%20And%20Slave%20Lag%20Considerations&amp;link=http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/&amp;notes=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&amp;short_link=http://bit.ly/atwEQy&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=201&amp;tags=&amp;ctype=" 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/2008/04/16/mysql-conference-liveblogging-portable-scale-out-benchmarks-for-mysql-wednesday-1050am/" rel="bookmark" title="April 16, 2008">MySQL Conference Liveblogging: Portable Scale-out Benchmarks For MySQL (Wednesday 10:50AM)</a></li></ul><p><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbeerpla.net%2F2009%2F05%2F11%2Fmysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations%2F&amp;title=%5BMySQL%5D%20Deleting%2FUpdating%20Rows%20Common%20To%202%20Tables%20%26%238211%3B%20Speed%20And%20Slave%20Lag%20Considerations" id="wpa2a_2"><img
src="http://beerpla.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></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>
