<?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>MySQL Performance Blog &#187; problems</title>
	<atom:link href="http://www.mysqlperformanceblog.com/category/bugs/problems/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlperformanceblog.com</link>
	<description>Everything about MySQL Performance</description>
	<lastBuildDate>Sat, 21 Nov 2009 03:11:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Slow DROP TABLE</title>
		<link>http://www.mysqlperformanceblog.com/2009/06/16/slow-drop-table/</link>
		<comments>http://www.mysqlperformanceblog.com/2009/06/16/slow-drop-table/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 17:55:42 +0000</pubDate>
		<dc:creator>Maciej Dobrzanski</dc:creator>
				<category><![CDATA[problems]]></category>

		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/?p=655</guid>
		<description><![CDATA[It is a known fact that ext3 is not the most efficient file system out there and for example file removals can be painfully slow and cause a lot of random I/O. However, as it turns out, it can sometimes have a much more severe impact on the MySQL performance that it would seem. When [...]]]></description>
			<content:encoded><![CDATA[<p>It is a known fact that <em>ext3</em> is not the most efficient file system out there and for example file removals can be painfully slow and cause a lot of random I/O. However, as it turns out, it can sometimes have a much more severe impact on the MySQL performance that it would seem. When or why?</p>
<p>When you run <code>DROP TABLE</code> there are several things that need to happen – write lock on a table so it cannot be used by any other thread, the data file(s) removal by the storage engine and of course in the end MySQL has to destroy the definition file (.frm). That's not all that happens, there is one other thing:</p>
<div class="igBar"><span id="lcode-6"><a href="#" onclick="javascript:showPlainTxt('code-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-6">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">VOID<span style="color:#006600; font-weight:bold;">&#40;</span>pthread_mutex_lock<span style="color:#006600; font-weight:bold;">&#40;</span>&amp;LOCK_open<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">error= mysql_rm_table_part2<span style="color:#006600; font-weight:bold;">&#40;</span>thd, tables, if_exists, drop_temporary, <span style="color:#800000;color:#800000;">0</span>, <span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">pthread_mutex_unlock<span style="color:#006600; font-weight:bold;">&#40;</span>&amp;LOCK_open<span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The entire code which removes a table is surrounded by <code>LOCK_open</code> mutex. The mutex is used in various places inside MySQL, but primarily when tables are being opened or closed. It means that when <code>LOCK_open</code> is locked, no query can be executed because they are stopped from accessing any table.</p>
<p>Here is when the slow file removal on the ext3 file system starts to be a pain. Deleting a 10GB file can take a few seconds and if that is a MySQL table, the mutex remains locked for all that time stalling all queries:</p>
<div class="igBar"><span id="lcode-7"><a href="#" onclick="javascript:showPlainTxt('code-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-7">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">+-----+------+-----------+------+---------+------+----------------+------------------------------------------------+</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">| Id | User | Host | db | Command | Time | State | Info |</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">+-----+------+-----------+------+---------+------+----------------+------------------------------------------------+</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">| <span style="color:#800000;color:#800000;">1</span> | root | localhost | test | Query | <span style="color:#800000;color:#800000;">7</span> | NULL | drop table large_table | </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">| <span style="color:#800000;color:#800000;">329</span> | root | localhost | test | Query | <span style="color:#800000;color:#800000;">7</span> | Opening tables | select sql_no_cache * from other_table limit <span style="color:#800000;color:#800000;">1</span> | </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">+-----+------+-----------+------+---------+------+----------------+------------------------------------------------+ </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>I tried some alternative approaches to trick MySQL into removing smaller files with <code>DROP TABLE</code> to minimize the effect, such as:</p>
<ul>
<li>TRUNCATE TABLE large_table; ALTER TABLE large_table ENGINE=…; DROP TABLE large_table;</li>
<li>TRUNCATE TABLE large_table; OPTIMIZE TABLE large_table; DROP TABLE large_table;</li>
</ul>
<p>Unfortunately as it turned out each of the administrative commands like <code>ALTER TABLE</code> or <code>OPTIMIZE TABLE</code> one way or another uses <code>LOCK_open</code> mutex when the old table files are deleted:</p>
<div class="igBar"><span id="lcode-8"><a href="#" onclick="javascript:showPlainTxt('code-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-8">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">| <span style="color:#800000;color:#800000;">3</span> | root | localhost | test | Query | <span style="color:#800000;color:#800000;">7</span> | rename result table | ALTER TABLE large_table ENGINE=MyISAM |</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">| <span style="color:#800000;color:#800000;">679</span> | root | localhost | test | Query | <span style="color:#800000;color:#800000;">6</span> | Opening tables | select * from other_table limit <span style="color:#800000;color:#800000;">1</span> | </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The only alternative seems to be changing the file system. To <em>XFS</em> for example, which handles file removals much more efficiently:</p>
<p><strong><font color="blue">EXT3</font></strong></p>
<div class="igBar"><span id="lcode-9"><a href="#" onclick="javascript:showPlainTxt('code-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-9">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mysql&gt; drop table large_table; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Query OK, <span style="color:#800000;color:#800000;">0</span> rows affected <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">7</span>.<span style="color:#800000;color:#800000;">44</span> sec<span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong><font color="blue">XFS</font></strong></p>
<div class="igBar"><span id="lcode-10"><a href="#" onclick="javascript:showPlainTxt('code-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-10">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mysql&gt; drop table large_table;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Query OK, <span style="color:#800000;color:#800000;">0</span> rows affected <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">29</span> sec<span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>A better solution through MySQL internals could be to simulate the table drop by renaming the corresponding data file or files and physically removing them outside of the mutex lock. However it may not be that simple, because the actual removal is performed by the storage engine, so it's not something MySQL code can control.</p>
<p>This is certainly not a common situation, but may become a problem to anyone when it's the least expected (e.g. dropping old unused tables). </p>
    <hr noshade style="margin:0;height:1px" />
    <p>Entry posted by Maciej Dobrzanski |
      <a href="http://www.mysqlperformanceblog.com/2009/06/16/slow-drop-table/#comments">19 comments</a></p>
    <p>Add to: <a href="http://del.icio.us/post?url=http://www.mysqlperformanceblog.com/2009/06/16/slow-drop-table/&amp;title=Slow DROP TABLE" title="Bookmark this post on del.icio.us"><img src="http://www.mysqlperformanceblog.com/wp-content/themes/boxy-but-gold/images/delicious.png" alt="delicious" /></a> | <a href="http://digg.com/submit?phase=2&amp;url=http://www.mysqlperformanceblog.com/2009/06/16/slow-drop-table/&amp;title=Slow DROP TABLE" title="Digg this post on Digg.com"><img src="http://www.mysqlperformanceblog.com/wp-content/themes/boxy-but-gold/images/digg.png" alt="digg" /></a> | <a href="http://reddit.com/submit?url=http://www.mysqlperformanceblog.com/2009/06/16/slow-drop-table/&amp;title=Slow DROP TABLE" title="Submit this post on reddit.com"><img src="http://www.mysqlperformanceblog.com/wp-content/themes/boxy-but-gold/images/reddit.png" alt="reddit" /></a> | <a href="http://www.netscape.com/submit/?U=http://www.mysqlperformanceblog.com/2009/06/16/slow-drop-table/&amp;T=Slow DROP TABLE" title="Vote for this article on Netscape"><img src="http://www.mysqlperformanceblog.com/wp-content/themes/boxy-but-gold/images/netscape.gif" alt="netscape" /></a> | <a href="http://www.google.com/bookmarks/mark?op=add&amp;bkmk=http://www.mysqlperformanceblog.com/2009/06/16/slow-drop-table/&amp;title=Slow DROP TABLE" title="Add to Google Bookmarks"><img src="http://www.mysqlperformanceblog.com/wp-content/themes/boxy-but-gold/images/google.png" alt="Google Bookmarks" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mysqlperformanceblog.com/2009/06/16/slow-drop-table/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
