<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Venu Anuganti Blog</title>
	
	<link>http://venublog.com</link>
	<description>Everything In Life Is Random! (Personal and Work)</description>
	<lastBuildDate>Mon, 08 Mar 2010 23:29:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/venublog/apQq" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="venublog/apqq" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>When indexes are created in internal temporary tables</title>
		<link>http://venublog.com/2010/03/08/when-indexes-are-created-in-internal-temporary-tables/</link>
		<comments>http://venublog.com/2010/03/08/when-indexes-are-created-in-internal-temporary-tables/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 11:40:26 +0000</pubDate>
		<dc:creator>Venu Anuganti</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[distinct key]]></category>
		<category><![CDATA[group key]]></category>
		<category><![CDATA[how to query internal temporary tables]]></category>
		<category><![CDATA[indexes on internal temporary tables]]></category>
		<category><![CDATA[mysql internal temporary tables]]></category>
		<category><![CDATA[optimizing mysql]]></category>
		<category><![CDATA[SHOW TEMPORARY TABLES]]></category>

		<guid isPermaLink="false">http://venublog.com/2010/03/08/when-indexes-are-created-in-internal-temporary-tables/</guid>
		<description><![CDATA[During my previous post on how to improve derived tables performance, I patched the code to add indexes forcefully on internal derived table results, which made a huge difference in the performance. It was just an experiment and a thought to see if it really works without re-writing the queries, so that the logic can [...]]]></description>
			<content:encoded><![CDATA[<p>During my previous post on <a href="http://venublog.com/2010/03/06/how-to-improve-subqueries-derived-tables-performance/">how to improve derived tables performance</a>, I patched the code to add indexes forcefully on internal derived table results, which made a huge difference in the performance. It was just an experiment and a thought to see if it really works without re-writing the queries, so that the logic can be pushed towards the engine rather than query re-write. \</p>
<p>But I got few emails in my inbox today asking whether MySQL really create any keys on internal temporary tables.</p>
<p>The answer is YES; and MySQL does create two keys on internal temporary tables namely &#8216;<strong>group_key</strong>&#8216; and &#8216;<strong>distinct_key</strong>&#8216; on the following conditions:</p>
<ul>
<li>If there is any aggregate function and/or group-by (<strong>group_key</strong>) </li>
<li>Distinct column name(<strong>group_key</strong>) </li>
<li>Distinct in combination with group-by/aggregation functions (<strong>distinct_key</strong>) </li>
</ul>
<p>Provided the query results are yielded in temporary table (<strong>Using temporary</strong> from the explain), else they get optimized away by the existing indexes from the regular table itself. These keys are added to both memory and disk based (MyISAM) internal temporary tables; so it does not matter if the internal temporary table is in memory or disk.</p>
<p>Here is a simple dump of internal temporary table index stats for some of the basic queries related to Information schema [<strong>Warning:</strong> <em>these queries are really bad, and can't be used for any production use as they are meant for demonstration of different internal keys</em> ]. This is a patch that I might be using for <a href="http://venublog.com/2010/02/03/show-temporary-tables/">SHOW TEMPORARY TABLES</a> when internal tables are included in the second version. The <a href="https://code.launchpad.net/~mydb08/maria/maria-5.1/+merge/20003">first version of the patch</a> is already pushed to <a href="http://askmonty.org/wiki/index.php/MariaDB">Maria branch</a>, hoping that it gets pushed to 5.1.</p>
</p>

<div class="wp_codebox"><table><tr id="p5112"><td class="code" id="p511code2"><pre class="mysql" style="font-family:monospace;"><span style="color: #CC0099;">-----------------------------</span>
 TMP <span style="color: #990099; font-weight: bold;">TABLE</span> STATS<span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">SESSION</span>: <span style="color: #008080;">1</span>
   temp file  : <span style="color: #CC0099;">/</span>tmp<span style="color: #CC0099;">/</span><span style="color: #808080; font-style: italic;">#sqlf90_1_1f</span>
   temp <span style="color: #990099; font-weight: bold;">type</span>  : MEMORY
   <span style="color: #990099; font-weight: bold;">index</span> <span style="color: #000099;">count</span>: <span style="color: #008080;">1</span>
    <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span><span style="color: #CC0099;">-</span><span style="color: #008080;">1</span>   : distinct_key
    <span style="color: #000099;">field</span>     : <span style="color: #FF00FF;">&#40;</span><span style="color: #9900FF; font-weight: bold;">null</span><span style="color: #FF00FF;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span><span style="color: #CC0099;">-</span><span style="color: #008080;">2</span>   : distinct_key
    <span style="color: #000099;">field</span>     : <span style="color: #990099; font-weight: bold;">ENGINE</span>
 query: <span style="color: #990099; font-weight: bold;">select</span> <span style="color: #000099;">count</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">distinct</span> <span style="color: #990099; font-weight: bold;">engine</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">from</span> information_schema.<span style="color: #990099; font-weight: bold;">tables</span>
<span style="color: #CC0099;">-----------------------------</span>
&nbsp;
<span style="color: #CC0099;">-----------------------------</span>
 TMP <span style="color: #990099; font-weight: bold;">TABLE</span> STATS<span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">SESSION</span>: <span style="color: #008080;">1</span>
   temp file  : <span style="color: #CC0099;">/</span>tmp<span style="color: #CC0099;">/</span><span style="color: #808080; font-style: italic;">#sqlf90_1_21</span>
   temp <span style="color: #990099; font-weight: bold;">type</span>  : MEMORY
   <span style="color: #990099; font-weight: bold;">index</span> <span style="color: #000099;">count</span>: <span style="color: #008080;">1</span>
    <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span><span style="color: #CC0099;">-</span><span style="color: #008080;">1</span>   : group_key
    <span style="color: #000099;">field</span>     : TABLE_NAME
 query: <span style="color: #990099; font-weight: bold;">select</span> table_name<span style="color: #000033;">,</span> <span style="color: #000099;">sum</span><span style="color: #FF00FF;">&#40;</span>data_length<span style="color: #CC0099;">+</span>index_length<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">from</span> information_schema.<span style="color: #990099; font-weight: bold;">tables</span> 
        <span style="color: #990099; font-weight: bold;">where</span> table_schema<span style="color: #CC0099;">=</span><span style="color: #008000;">'mysql'</span> <span style="color: #990099; font-weight: bold;">group by</span> <span style="color: #008080;">1</span>
<span style="color: #CC0099;">-----------------------------</span>
&nbsp;
<span style="color: #CC0099;">-----------------------------</span>
 TMP <span style="color: #990099; font-weight: bold;">TABLE</span> STATS<span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">SESSION</span>: <span style="color: #008080;">1</span>
   temp file  : <span style="color: #CC0099;">/</span>tmp<span style="color: #CC0099;">/</span><span style="color: #808080; font-style: italic;">#sqlf90_1_24</span>
   temp <span style="color: #990099; font-weight: bold;">type</span>  : MEMORY
   <span style="color: #990099; font-weight: bold;">index</span> <span style="color: #000099;">count</span>: <span style="color: #008080;">1</span>
    <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span><span style="color: #CC0099;">-</span><span style="color: #008080;">1</span>   : group_key
    <span style="color: #000099;">field</span>     : TABLE_SCHEMA
    <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span><span style="color: #CC0099;">-</span><span style="color: #008080;">2</span>   : group_key
    <span style="color: #000099;">field</span>     : TABLE_NAME
    <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span><span style="color: #CC0099;">-</span><span style="color: #008080;">3</span>   : group_key
    <span style="color: #000099;">field</span>     : COLUMN_NAME
 query: <span style="color: #990099; font-weight: bold;">select</span> tab.table_schema<span style="color: #000033;">,</span> tab.table_name<span style="color: #000033;">,</span> column_name<span style="color: #000033;">,</span> index_name<span style="color: #000033;">,</span> seq_in_index 
        <span style="color: #990099; font-weight: bold;">from</span> Information_schema.<span style="color: #990099; font-weight: bold;">tables</span> tab <span style="color: #990099; font-weight: bold;">join</span> information_schema.statistics stast 
        <span style="color: #990099; font-weight: bold;">using</span><span style="color: #FF00FF;">&#40;</span>table_schema<span style="color: #000033;">,</span>table_name<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">group by</span>  <span style="color: #008080;">1</span><span style="color: #000033;">,</span><span style="color: #008080;">2</span><span style="color: #000033;">,</span><span style="color: #008080;">3</span>
<span style="color: #CC0099;">-----------------------------</span>
&nbsp;
<span style="color: #CC0099;">-----------------------------</span>
 TMP <span style="color: #990099; font-weight: bold;">TABLE</span> STATS<span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">SESSION</span>: <span style="color: #008080;">1</span>
   temp file  : <span style="color: #CC0099;">/</span>tmp<span style="color: #CC0099;">/</span><span style="color: #808080; font-style: italic;">#sqlf90_1_bd</span>
   temp <span style="color: #990099; font-weight: bold;">type</span>  : MEMORY
   <span style="color: #990099; font-weight: bold;">index</span> <span style="color: #000099;">count</span>: <span style="color: #008080;">1</span>
    <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span><span style="color: #CC0099;">-</span><span style="color: #008080;">1</span>   : group_key
    <span style="color: #000099;">field</span>     : TABLE_NAME
    <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span><span style="color: #CC0099;">-</span><span style="color: #008080;">2</span>   : group_key
    <span style="color: #000099;">field</span>     : TABLE_TYPE
    <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span><span style="color: #CC0099;">-</span><span style="color: #008080;">3</span>   : group_key
    <span style="color: #000099;">field</span>     : <span style="color: #990099; font-weight: bold;">ENGINE</span>
    <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span><span style="color: #CC0099;">-</span><span style="color: #008080;">4</span>   : group_key
    <span style="color: #000099;">field</span>     : INDEX_SCHEMA
    <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span><span style="color: #CC0099;">-</span><span style="color: #008080;">5</span>   : group_key
    <span style="color: #000099;">field</span>     : INDEX_NAME
 query: <span style="color: #990099; font-weight: bold;">select</span> tab.table_schema<span style="color: #000033;">,</span>  tab.table_name<span style="color: #000033;">,</span> table_type<span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">engine</span><span style="color: #000033;">,</span> index_schema<span style="color: #000033;">,</span> 
        index_name <span style="color: #990099; font-weight: bold;">from</span> information_schema.<span style="color: #990099; font-weight: bold;">tables</span> tab <span style="color: #990099; font-weight: bold;">join</span> information_schema.statistics
        stats <span style="color: #990099; font-weight: bold;">using</span><span style="color: #FF00FF;">&#40;</span>table_schema<span style="color: #000033;">,</span> table_name<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">where</span> table_schema<span style="color: #CC0099;">=</span><span style="color: #008000;">'mysql'</span> <span style="color: #990099; font-weight: bold;">group by</span> 
        <span style="color: #008080;">1</span><span style="color: #000033;">,</span><span style="color: #008080;">2</span><span style="color: #000033;">,</span><span style="color: #008080;">3</span><span style="color: #000033;">,</span><span style="color: #008080;">4</span><span style="color: #000033;">,</span><span style="color: #008080;">5</span><span style="color: #000033;">,</span> <span style="color: #008080;">6</span> <span style="color: #990099; font-weight: bold;">order by</span> <span style="color: #008080;">4</span><span style="color: #000033;">,</span><span style="color: #008080;">3</span><span style="color: #000033;">,</span><span style="color: #008080;">2</span><span style="color: #000033;">,</span><span style="color: #008080;">1</span>
<span style="color: #CC0099;">-----------------------------</span>
&nbsp;
<span style="color: #CC0099;">-----------------------------</span>
 TMP <span style="color: #990099; font-weight: bold;">TABLE</span> STATS<span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">SESSION</span>: <span style="color: #008080;">1</span>
   temp file  : <span style="color: #CC0099;">/</span>tmp<span style="color: #CC0099;">/</span><span style="color: #808080; font-style: italic;">#sqlf90_1_e0</span>
   temp <span style="color: #990099; font-weight: bold;">type</span>  : MEMORY
   <span style="color: #990099; font-weight: bold;">index</span> <span style="color: #000099;">count</span>: <span style="color: #008080;">1</span>
    <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span><span style="color: #CC0099;">-</span><span style="color: #008080;">1</span>   : group_key
    <span style="color: #000099;">field</span>     : TABLE_NAME
 query: <span style="color: #990099; font-weight: bold;">select</span>  table_name<span style="color: #000033;">,</span> <span style="color: #000099;">sum</span><span style="color: #FF00FF;">&#40;</span>data_length<span style="color: #CC0099;">+</span>index_length<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">from</span> information_schema.<span style="color: #990099; font-weight: bold;">tables</span>
        <span style="color: #990099; font-weight: bold;">where</span> table_schema<span style="color: #CC0099;">=</span><span style="color: #008000;">'mysql'</span> <span style="color: #990099; font-weight: bold;">group by</span> <span style="color: #008080;">1</span>
<span style="color: #CC0099;">-----------------------------</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://venublog.com/2010/03/08/when-indexes-are-created-in-internal-temporary-tables/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to improve subqueries derived tables performance</title>
		<link>http://venublog.com/2010/03/06/how-to-improve-subqueries-derived-tables-performance/</link>
		<comments>http://venublog.com/2010/03/06/how-to-improve-subqueries-derived-tables-performance/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 21:38:03 +0000</pubDate>
		<dc:creator>Venu Anuganti</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Derived tables]]></category>
		<category><![CDATA[mysql internal temporary tables]]></category>
		<category><![CDATA[mysql performance]]></category>
		<category><![CDATA[Optimize mysql derived tables]]></category>
		<category><![CDATA[sub queries performance]]></category>

		<guid isPermaLink="false">http://venublog.com/?p=497</guid>
		<description><![CDATA[Last week I was working on one of the issue where the sub-query related to OLAP staging was running for about 2+ hours in the production server and finally nailed down to get the query to run in &#60; 7 secs. It was bit interesting and kind of known issue in MySQL sub-queries world and [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I was working on one of the issue where the sub-query related to OLAP staging was running for about 2+ hours in the production server and finally nailed down to get the query to run in &lt; 7 secs. It was bit interesting and kind of known issue in MySQL sub-queries world and one of the limitation from MySQL on giving more control over derived table results.</p>
<p>Sometimes we can re-write the sub-queries so that there is no derived tables complexity involved; but the bad part is; this particular sub-query is part of an UPDATE statement; so not all sub-queries can be re-written especially when they are part of UPDATE or DELETE statements due to its own limitations.</p>
<p><strong>PROBLEM:</strong></p>
<p>Here is the subset of the problem query and as you can see it runs for about 6 minutes in this small subset of data that I used for testing on Mac. All tables are InnoDB based.</p>

<div class="wp_codebox"><table><tr id="p4976"><td class="code" id="p497code6"><pre class="mysql" style="font-family:monospace;"><span style="color: #CC0099;">--------------</span>
<span style="color: #990099; font-weight: bold;">SELECT</span>
    <span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>aggrpt.imps<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> imps<span style="color: #000033;">,</span>
    <span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>aggrpt.clicks<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> clicks<span style="color: #000033;">,</span>
    <span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>aggrpt.pos<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> pos
&nbsp;
<span style="color: #990099; font-weight: bold;">FROM</span> aggrpt
<span style="color: #000099;">LEFT</span> <span style="color: #990099; font-weight: bold;">JOIN</span>
<span style="color: #FF00FF;">&#40;</span>
    <span style="color: #990099; font-weight: bold;">SELECT</span>
    <span style="color: #990099; font-weight: bold;">DISTINCT</span> ext_group_id<span style="color: #000033;">,</span> group_id
    <span style="color: #990099; font-weight: bold;">FROM</span> sub
<span style="color: #FF00FF;">&#41;</span> sub2  <span style="color: #990099; font-weight: bold;">ON</span><span style="color: #FF00FF;">&#40;</span>sub2.ext_group_id<span style="color: #CC0099;">=</span>aggrpt.adgroupid<span style="color: #FF00FF;">&#41;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">GROUP BY</span>
aggrpt.report_date<span style="color: #000033;">,</span>
aggrpt.campaignid<span style="color: #000033;">,</span>
aggrpt.adgroupid<span style="color: #000033;">,</span>
aggrpt.keywordid
<span style="color: #990099; font-weight: bold;">ORDER BY</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #990099; font-weight: bold;">OUTFILE</span> <span style="color: #008000;">'/tmp/test-sub.txt'</span>
<span style="color: #CC0099;">------------</span><span style="color: #808080; font-style: italic;">-- </span>
&nbsp;
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">47827</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">6</span> <span style="color: #000099;">min</span> <span style="color: #008080;">47.48</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<p><strong>HOW TO GET AROUND &#8211; SOLUTIONS:</strong></p>
<p>Moving the derived table (in the above case sub2) to a view did not help; and timings are more or less the same. Here is the two alternative ways, which made the query run in &lt; 10 secs.</p>
<ol>
<li>By creating external table and adding an index instead of using derived table</li>
<li>Added an index within the mysql code on the derived table temporary results table by adding FORCE INDEX syntax (changed the syntax to support this, so that engine will create an index on temporary derived table results, which in this case happens to fit within heap engine instead of disk based)</li>
</ol>
<p><strong>CASE 1:</strong></p>
<p>This is a known alternative and lot of people use this in production by avoiding the derived tables and/or sub-queries completely by creating tables for derived tables. The only thing that made the big difference is adding an index on this; without index it takes more or less the same 6 minutes times.</p>

<div class="wp_codebox"><table><tr id="p4977"><td class="code" id="p497code7"><pre class="mysql" style="font-family:monospace;"><span style="color: #CC0099;">--------------</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> sub_temp <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #990099; font-weight: bold;">DISTINCT</span> ext_group_id<span style="color: #000033;">,</span> group_id <span style="color: #990099; font-weight: bold;">FROM</span> sub
<span style="color: #CC0099;">--------------</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">72385</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.81</span> sec<span style="color: #FF00FF;">&#41;</span>
Records: <span style="color: #008080;">72385</span>  Duplicates: <span style="color: #008080;">0</span>  <span style="color: #990099; font-weight: bold;">Warnings</span>: <span style="color: #008080;">0</span>
<span style="color: #CC0099;">--------------</span>
<span style="color: #990099; font-weight: bold;">alter</span> <span style="color: #990099; font-weight: bold;">table</span> sub_temp <span style="color: #990099; font-weight: bold;">add</span> <span style="color: #990099; font-weight: bold;">index</span> i_ext_group_id<span style="color: #FF00FF;">&#40;</span>ext_group_id<span style="color: #FF00FF;">&#41;</span>
<span style="color: #CC0099;">--------------</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">72385</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.10</span> sec<span style="color: #FF00FF;">&#41;</span>
Records: <span style="color: #008080;">72385</span>  Duplicates: <span style="color: #008080;">0</span>  <span style="color: #990099; font-weight: bold;">Warnings</span>: <span style="color: #008080;">0</span>
<span style="color: #CC0099;">--------------</span>
<span style="color: #990099; font-weight: bold;">SELECT</span>
    <span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>aggrpt.imps<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> imps<span style="color: #000033;">,</span>
    <span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>aggrpt.clicks<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> clicks<span style="color: #000033;">,</span>
    <span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>aggrpt.pos<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> pos
&nbsp;
<span style="color: #990099; font-weight: bold;">FROM</span> aggrpt
<span style="color: #000099;">LEFT</span> <span style="color: #990099; font-weight: bold;">JOIN</span> sub_temp sub <span style="color: #990099; font-weight: bold;">ON</span><span style="color: #FF00FF;">&#40;</span>sub.ext_group_id<span style="color: #CC0099;">=</span>aggrpt.adgroupid<span style="color: #FF00FF;">&#41;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">GROUP BY</span>
    aggrpt.report_date<span style="color: #000033;">,</span>
    aggrpt.campaignid<span style="color: #000033;">,</span>
    aggrpt.adgroupid<span style="color: #000033;">,</span>
    aggrpt.keywordid
<span style="color: #990099; font-weight: bold;">ORDER BY</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #990099; font-weight: bold;">OUTFILE</span> <span style="color: #008000;">'/tmp/test-sub-temp.txt'</span>
<span style="color: #CC0099;">------------</span><span style="color: #808080; font-style: italic;">-- </span>
&nbsp;
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">47827</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">10.41</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<p>As you can see its a great improvement from 6minutes to 10secs; but without index <em>i_ext_group_id</em> on sub-temp table; then things will be as usual.</p>
<p><strong>CASE 2:</strong></p>
<p>The engine should be smart enough to add the index on intermediate results temporary table (in this case sub2, index on column i_ext_group_id); but MySQL does not support this or at least should allow people to specify one using FORCE/USE INDEX, so that engine can add one.</p>
<p>For example; I patched MySQL to support this syntax, so that it adds the index on the derived table sub2 results automatically and the query immediately returns in 7 secs.</p>

<div class="wp_codebox"><table><tr id="p4978"><td class="code" id="p497code8"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span>
    <span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>aggrpt.imps<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> imps<span style="color: #000033;">,</span>
    <span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>aggrpt.clicks<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> clicks<span style="color: #000033;">,</span>
    <span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>aggrpt.pos<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> pos
&nbsp;
<span style="color: #990099; font-weight: bold;">FROM</span> aggrpt
<span style="color: #000099;">LEFT</span> <span style="color: #990099; font-weight: bold;">JOIN</span>
<span style="color: #FF00FF;">&#40;</span>
    <span style="color: #990099; font-weight: bold;">SELECT</span>
    <span style="color: #990099; font-weight: bold;">DISTINCT</span> ext_group_id<span style="color: #000033;">,</span> group_id
    <span style="color: #990099; font-weight: bold;">FROM</span> sub
<span style="color: #FF00FF;">&#41;</span> sub2 <span style="color: #CC0099;">&lt;</span>strong<span style="color: #CC0099;">&gt;</span>USE <span style="color: #990099; font-weight: bold;">INDEX</span><span style="color: #FF00FF;">&#40;</span>ext_group_id<span style="color: #FF00FF;">&#41;</span><span style="color: #CC0099;">&lt;/</span>strong<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">ON</span><span style="color: #FF00FF;">&#40;</span>sub2.ext_group_id<span style="color: #CC0099;">=</span>aggrpt.adgroupid<span style="color: #FF00FF;">&#41;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">GROUP BY</span>
aggrpt.report_date<span style="color: #000033;">,</span>
aggrpt.campaignid<span style="color: #000033;">,</span>
aggrpt.adgroupid<span style="color: #000033;">,</span>
aggrpt.keywordid
<span style="color: #990099; font-weight: bold;">ORDER BY</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #990099; font-weight: bold;">OUTFILE</span> <span style="color: #008000;">'/tmp/test-sub-force.txt'</span>
<span style="color: #CC0099;">------------</span><span style="color: #808080; font-style: italic;">-- </span>
&nbsp;
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">47827</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">7.18</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<p>This is again a great improvement; but again using USE/FORCE INDEX is a hack here (this can also be implemented as hint) with column name; which acts as a hint to optimizer to create an index on that column (if its hint, then on JOIN column).</p>
<p><strong>CHANGES TO MYSQL OPTIMIZER</strong></p>
<p>But the idea for this post is to show <strong> how important the index in the intermediate temporary table results </strong>is for the query performance and optimizer should be smart enough to identify and add one automatically. The logic is same as how currently optimizer opts for index vs non-index scan by estimating the cost.</p>
]]></content:encoded>
			<wfw:commentRss>http://venublog.com/2010/03/06/how-to-improve-subqueries-derived-tables-performance/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Changing MySQL parser code on Windows – Build breaks due to Bison</title>
		<link>http://venublog.com/2010/02/07/changing-mysql-parser-code-on-windows-build-breaks-due-to-bison/</link>
		<comments>http://venublog.com/2010/02/07/changing-mysql-parser-code-on-windows-build-breaks-due-to-bison/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 22:00:40 +0000</pubDate>
		<dc:creator>Venu Anuganti</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Bison errors on windows]]></category>
		<category><![CDATA[bison: m4: Invalid argument]]></category>
		<category><![CDATA[generating yacc files windows]]></category>
		<category><![CDATA[how to build mysql on windows]]></category>
		<category><![CDATA[Windows mysql build]]></category>

		<guid isPermaLink="false">http://venublog.com/2010/02/07/changing-mysql-parser-code-on-windows-build-breaks-due-to-bison/</guid>
		<description><![CDATA[In case if you working on Windows environment for MySQL development (sometimes I use visual studio for easy debugging); and in case if you change the parser code (sql_yacc.yy) or if you are working directly from development branch (bzr launchpad), then the build breaks to generate the yacc files (sql_yacc.h and sql_yacc.cc) with an error [...]]]></description>
			<content:encoded><![CDATA[<p>In case if you working on Windows environment for MySQL development (sometimes I use visual studio for easy debugging); and in case if you change the parser code (<i>sql_yacc.yy</i>) or if you are working directly from development branch (<a href="https://code.launchpad.net/mysql-server">bzr launchpad</a>), then the build breaks to generate the yacc files (<i>sql_yacc.h</i> and <i>sql_yacc.cc</i>) with an error <b>bison: M4: Invalid argument</b> as shown below:</p>

<div class="wp_codebox"><table><tr id="p48213"><td class="code" id="p482code13"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;</span>------ Build started: Project: sql, Configuration: Debug Win32 <span style="color: #660033;">------</span>
<span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;</span>Generating sql_yacc.h, sql_yacc.cc
<span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;</span>------ Build started: Project: GenServerSource, Configuration: Debug Win32 <span style="color: #660033;">------</span>
<span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;</span>Generating sql_yacc.h, sql_yacc.cc
<span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">bison</span>: <span style="color: #c20cb9; font-weight: bold;">m4</span>: Invalid argument
<span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;</span>Project : error PRJ0019: A tool returned an error code from <span style="color: #ff0000;">&quot;Generating sql_yacc.h, sql_yacc.cc&quot;</span>
<span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;</span>sql - <span style="color: #000000;">1</span> error<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span>, <span style="color: #000000;">0</span> warning<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">bison</span>: <span style="color: #c20cb9; font-weight: bold;">m4</span>: Invalid argument
<span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;</span>Project : error PRJ0019: A tool returned an error code from <span style="color: #ff0000;">&quot;Generating sql_yacc.h, sql_yacc.cc&quot;</span>
<span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;</span>GenServerSource - <span style="color: #000000;">1</span> error<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span>, <span style="color: #000000;">0</span> warning<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></td></tr></table></div>

<p>But if use source zip file for any particular release, then it won&#8217;t fail as the files (sql_yacc.cc and sql_yacc.h) are pre-built and copied to the distribution zip file.</p>
<p>
But, again if you wanted to change the code or happen to save sql_yacc.yy, then it starts generating the files and build will break. It looks like lot of people are experincing the same problem to build parser code on Windows using any recent version of bison (not just MySQL code base).
</p>
<p>
Both bison.exe and m4.exe are in the path and they are the latest version; but still it fails.. </p>

<div class="wp_codebox"><table><tr id="p48214"><td class="code" id="p482code14"><pre class="bash" style="font-family:monospace;">c:\mysql-<span style="color: #000000;">5.1</span>\sql<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #c20cb9; font-weight: bold;">bison</span>
C:\Gnu\GetGnuWin32\gnuwin32\bin\bison.EXE
&nbsp;
c:\mysql-<span style="color: #000000;">5.1</span>\sql<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #c20cb9; font-weight: bold;">m4</span>
C:\Gnu\GetGnuWin32\gnuwin32\bin\m4.EXE
&nbsp;
c:\mysql-<span style="color: #000000;">5.1</span>\sql<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">bison</span> <span style="color: #660033;">--version</span>
<span style="color: #c20cb9; font-weight: bold;">bison</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>GNU Bison<span style="color: #7a0874; font-weight: bold;">&#41;</span> 2.4.1
&nbsp;
c:\mysql-<span style="color: #000000;">5.1</span>\sql<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">m4</span> <span style="color: #660033;">--version</span>
<span style="color: #c20cb9; font-weight: bold;">m4</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>GNU M4<span style="color: #7a0874; font-weight: bold;">&#41;</span> 1.4.13</pre></td></tr></table></div>

<p>It looks like the problem is with Windows version of bison to pick m4 executable even though m4 is in the path. For example, you can directy try to generate the files from sql directory using bison as&#8230; </p>

<div class="wp_codebox"><table><tr id="p48215"><td class="code" id="p482code15"><pre class="bash" style="font-family:monospace;">c:\mysql-<span style="color: #000000;">5.1</span>\sql<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">bison</span> <span style="color: #660033;">-y</span> <span style="color: #660033;">-p</span> MYSQL <span style="color: #660033;">--defines</span>=sql_yacc.h <span style="color: #660033;">--output</span>=sql_yacc.cc sql_yacc.yy
<span style="color: #c20cb9; font-weight: bold;">bison</span>: <span style="color: #c20cb9; font-weight: bold;">m4</span>: Invalid argument</pre></td></tr></table></div>

<p>The <b>work around</b> what I found is to copy m4.exe to sql directory directly, so that bison can pick from local working directory, then everything starts working as expected.</p>

<div class="wp_codebox"><table><tr id="p48216"><td class="code" id="p482code16"><pre class="bash" style="font-family:monospace;">c:\mysql-<span style="color: #000000;">5.1</span>\sql<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">bison</span> <span style="color: #660033;">-y</span> <span style="color: #660033;">-p</span> MYSQL <span style="color: #660033;">--defines</span>=sql_yacc.h <span style="color: #660033;">--output</span>=sql_yacc.cc sql_yacc.yy
<span style="color: #c20cb9; font-weight: bold;">bison</span>: <span style="color: #c20cb9; font-weight: bold;">m4</span>: Invalid argument
&nbsp;
c:\mysql-<span style="color: #000000;">5.1</span>\sql<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-al</span> sql_yacc.<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #660033;">-rw-rw-rw-</span>  <span style="color: #000000;">1</span> venu <span style="color: #000000;">0</span> <span style="color: #000000;">413012</span> <span style="color: #000000;">2010</span>-02-07 <span style="color: #000000;">11</span>:<span style="color: #000000;">58</span> sql_yacc.yy
&nbsp;
c:\mysql-<span style="color: #000000;">5.1</span>\sql<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #c20cb9; font-weight: bold;">m4</span>
C:\Gnu\GetGnuWin32\gnuwin32\bin\m4.EXE
&nbsp;
c:\mysql-<span style="color: #000000;">5.1</span>\sql<span style="color: #000000; font-weight: bold;">&gt;</span>copy C:\Gnu\GetGnuWin32\gnuwin32\bin\m4.EXE .
        <span style="color: #000000;">1</span> <span style="color: #c20cb9; font-weight: bold;">file</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> copied.
&nbsp;
c:\mysql-<span style="color: #000000;">5.1</span>\sql<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #c20cb9; font-weight: bold;">m4</span>
c:\mysql-<span style="color: #000000;">5.1</span>\sql\m4.EXE
&nbsp;
c:\mysql-<span style="color: #000000;">5.1</span>\sql<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">bison</span> <span style="color: #660033;">-y</span> <span style="color: #660033;">-p</span> MYSQL <span style="color: #660033;">--defines</span>=sql_yacc.h <span style="color: #660033;">--output</span>=sql_yacc.cc sql_yacc.yy
&nbsp;
c:\mysql-<span style="color: #000000;">5.1</span>\sql<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-al</span> sql_yacc.<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #660033;">-rw-rw-rw-</span>  <span style="color: #000000;">1</span> venu <span style="color: #000000;">0</span> <span style="color: #000000;">1510389</span> <span style="color: #000000;">2010</span>-02-07 <span style="color: #000000;">14</span>:<span style="color: #000000;">33</span> sql_yacc.cc
<span style="color: #660033;">-rw-rw-rw-</span>  <span style="color: #000000;">1</span> venu <span style="color: #000000;">0</span>   <span style="color: #000000;">30532</span> <span style="color: #000000;">2010</span>-02-07 <span style="color: #000000;">14</span>:<span style="color: #000000;">33</span> sql_yacc.h
<span style="color: #660033;">-rw-rw-rw-</span>  <span style="color: #000000;">1</span> venu <span style="color: #000000;">0</span>  <span style="color: #000000;">413012</span> <span style="color: #000000;">2010</span>-02-07 <span style="color: #000000;">11</span>:<span style="color: #000000;">58</span> sql_yacc.yy</pre></td></tr></table></div>

<p>Kind of weired, but atleast there is a work around to change the parser code on Windows now; and it works great including Visual studio also starts building without any errors. But if you remove m4.exe from sql directory, then things starts to break immediately.</p>
]]></content:encoded>
			<wfw:commentRss>http://venublog.com/2010/02/07/changing-mysql-parser-code-on-windows-build-breaks-due-to-bison/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>SHOW TEMPORARY TABLES</title>
		<link>http://venublog.com/2010/02/03/show-temporary-tables/</link>
		<comments>http://venublog.com/2010/02/03/show-temporary-tables/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 09:30:10 +0000</pubDate>
		<dc:creator>Venu Anuganti</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Find temporary tables in MySQL]]></category>
		<category><![CDATA[mysql patches]]></category>
		<category><![CDATA[MySQL Temp Tables]]></category>
		<category><![CDATA[SHOW TEMPORARY TABLES]]></category>

		<guid isPermaLink="false">http://venublog.com/2010/02/03/show-temporary-tables/</guid>
		<description><![CDATA[I had this patch for a while where one can get listing of both session and global temporary tables across all sessions. It really helped lot of times to understand the bottlenecks of some of the temporary table issues as MySQL never exposed them in the form of SHOW TABLES.
I also added a new status [...]]]></description>
			<content:encoded><![CDATA[<p align="left">I had this patch for a while where one can get listing of both session and global temporary tables across all sessions. It really helped lot of times to understand the bottlenecks of some of the temporary table issues as MySQL never exposed them in the form of SHOW TABLES.</p>
<p align="left">I also added a new status variable called &#8216;<em>Created_tmp_heap_to_disk_tables</em>&#8216;, which keeps track of how many memory based temp tables are re-created back to disk based.</p>
<p align="left">The patch is now ported to newer MySQL versions, both 5.0 and 5.1; and it works great on most of the platforms that I tested (Mac, Linux and Windows)</p>
<p align="left">It introduces two new <a href="http://dev.mysql.com/doc/refman/5.0/en/information-schema.html">INFORMATION_SCHEMA</a> tables, TEMPORARY_TABLES and GLOBAL_TEMPORARY_TABLES along with supporting regular SHOW syntax</p>

<div class="wp_codebox"><table><tr id="p46820"><td class="code" id="p468code20"><pre class="mysql" style="font-family:monospace;">&nbsp;
<span style="color: #990099; font-weight: bold;">SHOW</span> <span style="color: #FF00FF;">&#91;</span><span style="color: #990099; font-weight: bold;">SESSION</span><span style="color: #CC0099;">/</span><span style="color: #990099; font-weight: bold;">GLOBAL</span><span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLES</span> <span style="color: #FF00FF;">&#91;</span><span style="color: #990099; font-weight: bold;">FROM</span> db<span style="color: #FF00FF;">&#93;</span></pre></td></tr></table></div>

<p align="left">Some examples of how it works at present</p>
<h4>Session Temp Tables</h4>

<div class="wp_codebox"><table><tr id="p46821"><td class="code" id="p468code21"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">show</span> <span style="color: #990099; font-weight: bold;">session</span> <span style="color: #990099; font-weight: bold;">temporary</span> <span style="color: #990099; font-weight: bold;">tables</span><span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+------+-------+--------+--------------+</span>
<span style="color: #CC0099;">|</span> Id <span style="color: #CC0099;">|</span> Db   <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Table</span> <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Engine</span> <span style="color: #CC0099;">|</span> Name         <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+------+-------+--------+--------------+</span>
<span style="color: #CC0099;">|</span>  <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span> test <span style="color: #CC0099;">|</span> t2    <span style="color: #CC0099;">|</span> MEMORY <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_1_3 |</span>
<span style="color: #CC0099;">|</span>  <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span> test <span style="color: #CC0099;">|</span> t1    <span style="color: #CC0099;">|</span> MyISAM <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_1_2 |</span>
<span style="color: #CC0099;">+----+------+-------+--------+--------------+</span>
<span style="color: #008080;">2</span> rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">show</span> <span style="color: #990099; font-weight: bold;">temporary</span> <span style="color: #990099; font-weight: bold;">tables</span><span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+------+-------+--------+--------------+</span>
<span style="color: #CC0099;">|</span> Id <span style="color: #CC0099;">|</span> Db   <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Table</span> <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Engine</span> <span style="color: #CC0099;">|</span> Name         <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+------+-------+--------+--------------+</span>
<span style="color: #CC0099;">|</span>  <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span> test <span style="color: #CC0099;">|</span> t2    <span style="color: #CC0099;">|</span> MEMORY <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_1_3 |</span>
<span style="color: #CC0099;">|</span>  <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span> test <span style="color: #CC0099;">|</span> t1    <span style="color: #CC0099;">|</span> MyISAM <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_1_2 |</span>
<span style="color: #CC0099;">+----+------+-------+--------+--------------+</span>
<span style="color: #008080;">2</span> rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">select</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">from</span> information_schema.temporary_tables<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+------------+----------+------------+--------+--------------+</span>
<span style="color: #CC0099;">|</span> SESSION_ID <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">DATABASE</span> <span style="color: #CC0099;">|</span> TABLE_NAME <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">ENGINE</span> <span style="color: #CC0099;">|</span> NAME         <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+------------+----------+------------+--------+--------------+</span>
<span style="color: #CC0099;">|</span>          <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span> test     <span style="color: #CC0099;">|</span> t2         <span style="color: #CC0099;">|</span> MEMORY <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_1_3 |</span>
<span style="color: #CC0099;">|</span>          <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span> test     <span style="color: #CC0099;">|</span> t1         <span style="color: #CC0099;">|</span> MyISAM <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_1_2 |</span>
<span style="color: #CC0099;">+------------+----------+------------+--------+--------------+</span>
<span style="color: #008080;">2</span> rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<p><h4>Global Temp Tables Across All Sessions:</h4>

<div class="wp_codebox"><table><tr id="p46822"><td class="code" id="p468code22"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">select</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">from</span> information_schema.global_temporary_tables<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+------------+----------+------------+--------+--------------+</span>
<span style="color: #CC0099;">|</span> SESSION_ID <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">DATABASE</span> <span style="color: #CC0099;">|</span> TABLE_NAME <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">ENGINE</span> <span style="color: #CC0099;">|</span> NAME         <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+------------+----------+------------+--------+--------------+</span>
<span style="color: #CC0099;">|</span>          <span style="color: #008080;">6</span> <span style="color: #CC0099;">|</span> test     <span style="color: #CC0099;">|</span> t3         <span style="color: #CC0099;">|</span> MyISAM <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_6_0 |</span>
<span style="color: #CC0099;">|</span>          <span style="color: #008080;">5</span> <span style="color: #CC0099;">|</span> test     <span style="color: #CC0099;">|</span> t2         <span style="color: #CC0099;">|</span> MEMORY <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_5_3 |</span>
<span style="color: #CC0099;">|</span>          <span style="color: #008080;">5</span> <span style="color: #CC0099;">|</span> test     <span style="color: #CC0099;">|</span> t1         <span style="color: #CC0099;">|</span> MyISAM <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_5_2 |</span>
<span style="color: #CC0099;">|</span>          <span style="color: #008080;">4</span> <span style="color: #CC0099;">|</span> venu     <span style="color: #CC0099;">|</span> v1         <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">InnoDB</span> <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_4_0 |</span>
<span style="color: #CC0099;">+------------+----------+------------+--------+--------------+</span>
<span style="color: #008080;">4</span> rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">show</span> <span style="color: #990099; font-weight: bold;">global</span> <span style="color: #990099; font-weight: bold;">temporary</span> <span style="color: #990099; font-weight: bold;">tables</span><span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+------+-------+--------+--------------+</span>
<span style="color: #CC0099;">|</span> Id <span style="color: #CC0099;">|</span> Db   <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Table</span> <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Engine</span> <span style="color: #CC0099;">|</span> Name         <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+------+-------+--------+--------------+</span>
<span style="color: #CC0099;">|</span>  <span style="color: #008080;">6</span> <span style="color: #CC0099;">|</span> test <span style="color: #CC0099;">|</span> t3    <span style="color: #CC0099;">|</span> MyISAM <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_6_0 |</span>
<span style="color: #CC0099;">|</span>  <span style="color: #008080;">5</span> <span style="color: #CC0099;">|</span> test <span style="color: #CC0099;">|</span> t2    <span style="color: #CC0099;">|</span> MEMORY <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_5_3 |</span>
<span style="color: #CC0099;">|</span>  <span style="color: #008080;">5</span> <span style="color: #CC0099;">|</span> test <span style="color: #CC0099;">|</span> t1    <span style="color: #CC0099;">|</span> MyISAM <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_5_2 |</span>
<span style="color: #CC0099;">|</span>  <span style="color: #008080;">4</span> <span style="color: #CC0099;">|</span> venu <span style="color: #CC0099;">|</span> v1    <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">InnoDB</span> <span style="color: #CC0099;">|</span> <span style="color: #808080; font-style: italic;">#sql29da_4_0 |</span>
<span style="color: #CC0099;">+----+------+-------+--------+--------------+</span>
<span style="color: #008080;">4</span> rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<ul>
<li> Id: Session ID </li>
<li> Db: Database Name </li>
<li> Engine: Engine Type </li>
<li> Name: Internal Name, how its stored in the file system </li>
</ul>
<p align="left">Will publish the patch once am done with porting internal temp tables listing by introducing two new columns Table_Type and Info where Table_Type will indicate if its internal or external and Info will have initial 1024 bytes of the query that caused the internal temp table.</p>
]]></content:encoded>
			<wfw:commentRss>http://venublog.com/2010/02/03/show-temporary-tables/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Create Table Like Scalability Issues</title>
		<link>http://venublog.com/2010/01/10/create-table-like-scalability-issues/</link>
		<comments>http://venublog.com/2010/01/10/create-table-like-scalability-issues/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 07:44:22 +0000</pubDate>
		<dc:creator>Venu Anuganti</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[CREATE TABLE LIKE]]></category>
		<category><![CDATA[MySQL Open Close Tables]]></category>
		<category><![CDATA[MySQL Scalability]]></category>
		<category><![CDATA[MySQL Table Locks]]></category>
		<category><![CDATA[WAITING for table]]></category>

		<guid isPermaLink="false">http://venublog.com/2010/01/10/create-table-like-scalability-issues/</guid>
		<description><![CDATA[When we wanted to quickly process intermediate data by using temporary or heap tables; then its normal tendency to create a table like its source table; so people will simply opt for CREATE TEMPORARY TABLE temp_table LIKE source_table.
This is an easy and convenient way. But the problem is if you have a simple stored procedure [...]]]></description>
			<content:encoded><![CDATA[<p align="left">When we wanted to quickly process intermediate data by using temporary or heap tables; then its normal tendency to create a table like its source table; so people will simply opt for <a href="http://dev.mysql.com/doc/refman/5.0/en/create-table.html">CREATE TEMPORARY TABLE temp_table LIKE source_table</a>.</p>
<p align="left">This is an easy and convenient way. But the problem is if you have a simple stored procedure or code module that gets executed frequently, then you might indirectly experience a slow-down in the performance if your source_table is large enough and highly contended. Things will be really worst if concurrent threads starts using the source_table or in combination of source_table and CREATE LIKE.. statements and can lead to disaster scalability issues. The main reason for the post is; I noticed from SHOW PROCESSLIST from random servers where X threads getting into <strong>WAITING for TABLE</strong> lock state for simple CREATE TABLE LIKE statements and lot of threads associated with source table is opening and closing the tables.</p>
<p align="left">The main source of issue is, In MySQL 5.0 or lower versions; CREATE LIKE uses <strong>exclusive named lock</strong> (<em>lock_and_wait_for_table_name</em>) on the source_table; which forces all threads to close the table which are currently in use and gets the named lock and no other thread can read/write from source_table until destination table is created. If source_table is large enough and few long running selects are running; then CREATE TABLE will wait until it gets the lock. To make it simple; you can even simulate this with a simple read lock on a table in one session and try to create the table like the locked table in another session; and notice that create table will wait until session one unlocks the tables.</p>
<p> <strong>Session 1:</strong></p>

<div class="wp_codebox"><table><tr id="p44330"><td class="code" id="p443code30"><pre class="mysql" style="font-family:monospace;">localhost:test<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">create</span> <span style="color: #990099; font-weight: bold;">table</span> source_table <span style="color: #FF00FF;">&#40;</span>id <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#41;</span><span style="color: #990099; font-weight: bold;">Engine</span><span style="color: #CC0099;">=</span><span style="color: #990099; font-weight: bold;">InnoDB</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
localhost:test<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">lock</span> <span style="color: #990099; font-weight: bold;">table</span> source_table <span style="color: #990099; font-weight: bold;">read</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

</p>
<p>
<strong>Session 2:</strong></p>

<div class="wp_codebox"><table><tr id="p44331"><td class="code" id="p443code31"><pre class="mysql" style="font-family:monospace;">localhost:test<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">create</span> <span style="color: #990099; font-weight: bold;">temporary</span> <span style="color: #990099; font-weight: bold;">table</span> temp_table <span style="color: #CC0099; font-weight: bold;">like</span> source_table<span style="color: #000033;">;</span></pre></td></tr></table></div>

</p>
<p align="left">
Thats a simple case, but lets simulate a real example with stored procedure and <a href="http://dev.mysql.com/doc/refman/5.1/en/mysqlslap.html">mysqlslap</a> with multiple threads where few threads will be using the source_table and few threads starts creating the temporary tables.
</p>
<p>First, create the the source_table and populate the table with 10K rows..</p>
<p>
<div class="wp_codebox"><table><tr id="p44332"><td class="code" id="p443code32"><pre class="mysql" style="font-family:monospace;">localhost:test<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">use</span> test<span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.02</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
localhost:test<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> source_table<span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected<span style="color: #000033;">,</span> <span style="color: #008080;">1</span> warning <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
localhost:test<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> source_table<span style="color: #FF00FF;">&#40;</span>id <span style="color: #999900; font-weight: bold;">int</span><span style="color: #000033;">,</span> name <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">20</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #990099; font-weight: bold;">Engine</span><span style="color: #CC0099;">=</span><span style="color: #990099; font-weight: bold;">InnoDB</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
localhost:test<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> load_table_proc<span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
localhost:test<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> temp_scale_proc<span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
localhost:test<span style="color: #CC0099;">&gt;</span> DELIMITER GO
localhost:test<span style="color: #CC0099;">&gt;</span> 
localhost:test<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> load_table_proc<span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">count</span> <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #FF00FF;">&#41;</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">BEGIN</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">DECLARE</span> _i <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #000033;">;</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">SET</span> _i <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span><span style="color: #000033;">;</span>
    <span style="color: #CC0099;">-&gt;</span> WHILE _i <span style="color: #CC0099;">&lt;</span> <span style="color: #CC0099;">=</span> <span style="color: #000099;">count</span> <span style="color: #990099; font-weight: bold;">DO</span>
    <span style="color: #CC0099;">-&gt;</span>   <span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> source_table <span style="color: #990099; font-weight: bold;">SELECT</span> _i<span style="color: #000033;">,</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'testing'</span><span style="color: #000033;">,</span> _i<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
    <span style="color: #CC0099;">-&gt;</span>   <span style="color: #990099; font-weight: bold;">SET</span> _i <span style="color: #CC0099;">=</span> _i <span style="color: #CC0099;">+</span> <span style="color: #008080;">1</span><span style="color: #000033;">;</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #009900;">END</span> WHILE<span style="color: #000033;">;</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #009900;">END</span>
    <span style="color: #CC0099;">-&gt;</span> GO
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
localhost:test<span style="color: #CC0099;">&gt;</span> 
localhost:test<span style="color: #CC0099;">&gt;</span> DELIMITER <span style="color: #000033;">;</span>
localhost:test<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">CALL</span> load_table_proc<span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">10000</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">1</span> row affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">1.54</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

</p>
<p>
CREATE another stored procedure which can use the source_table to create the temp table and searches for a given number
</p>
<p>
<div class="wp_codebox"><table><tr id="p44333"><td class="code" id="p443code33"><pre class="mysql" style="font-family:monospace;">localhost:test<span style="color: #CC0099;">&gt;</span> DELIMITER GO
localhost:test<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> temp_scale_proc<span style="color: #FF00FF;">&#40;</span>cnt <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #FF00FF;">&#41;</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">BEGIN</span>
    <span style="color: #CC0099;">-&gt;</span>     <span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> temp_tab<span style="color: #000033;">;</span>
    <span style="color: #CC0099;">-&gt;</span>     <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table<span style="color: #000033;">;</span>
    <span style="color: #CC0099;">-&gt;</span>     <span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> temp_tab <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> source_table<span style="color: #000033;">;</span>
    <span style="color: #CC0099;">-&gt;</span>     <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> temp_tab <span style="color: #990099; font-weight: bold;">WHERE</span> id<span style="color: #CC0099;">=</span>cnt<span style="color: #000033;">;</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #009900;">END</span>
    <span style="color: #CC0099;">-&gt;</span> GO
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
localhost:test<span style="color: #CC0099;">&gt;</span> DELIMITER <span style="color: #000033;">;</span>
localhost:test<span style="color: #CC0099;">&gt;</span></pre></td></tr></table></div>

</p>
<p align="left">
Now execute the stored procedure temp_scale_proc in parallel using 10 threads through <a href="http://dev.mysql.com/doc/refman/5.1/en/mysqlslap.html">mysqlslap</a>; and start watching the processlist and observe the thread states.
</p>
<p>
<div class="wp_codebox"><table><tr id="p44334"><td class="code" id="p443code34"><pre class="bash" style="font-family:monospace;">mysqlslap <span style="color: #660033;">--create-schema</span>=<span style="color: #ff0000;">&quot;test&quot;</span> <span style="color: #660033;">--concurrency</span>=<span style="color: #000000;">10</span> <span style="color: #660033;">--iteration</span>=<span style="color: #000000;">100</span> <span style="color: #660033;">--delimiter</span>=<span style="color: #ff0000;">&quot;;&quot;</span>  <span style="color: #660033;">--query</span>=<span style="color: #ff0000;">&quot;call test.temp_scale_proc(1+(RAND() * 10000))&quot;</span></pre></td></tr></table></div>

</p>
<p>
The above calls the stored procedure by passing a random number between 1 to 10000.
</p>
<p>
Some thread states from SHOW PROCESSLIST showing locking and scalability issues&#8230;
</p>
<p>
<div class="wp_codebox"><table><tr id="p44335"><td class="code" id="p443code35"><pre class="mysql" style="font-family:monospace;"><span style="color: #008080;">2117</span>    root    localhost       <span style="color: #9900FF; font-weight: bold;">NULL</span>    <span style="color: #000099;">Sleep</span>   <span style="color: #008080;">0</span>               <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #008080;">2118</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Waiting for <span style="color: #990099; font-weight: bold;">table</span>       <span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> temp_tab <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> source_table
<span style="color: #008080;">2119</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Sending <span style="color: #990099; font-weight: bold;">data</span>    <span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> temp_tab <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> source_table
<span style="color: #008080;">2120</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Waiting for <span style="color: #990099; font-weight: bold;">table</span>       <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #008080;">2121</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Waiting for <span style="color: #990099; font-weight: bold;">table</span>       <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #008080;">2122</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Waiting for <span style="color: #990099; font-weight: bold;">table</span>       <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #008080;">2123</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Waiting for <span style="color: #990099; font-weight: bold;">table</span>       <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #008080;">2124</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Waiting for <span style="color: #990099; font-weight: bold;">table</span>       <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #008080;">2125</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Waiting for <span style="color: #990099; font-weight: bold;">table</span>       <span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> temp_tab <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> source_table
<span style="color: #008080;">2126</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Waiting for <span style="color: #990099; font-weight: bold;">table</span>       <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #008080;">2127</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Waiting for <span style="color: #990099; font-weight: bold;">table</span>       <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #808080; font-style: italic;">--
Total open tables: 8 and Name Locks: 7 (output of SHOW OPEN TABLES)</span></pre></td></tr></table></div>

</p>
<p>
Not just the locking, the statement also indirectly causes table to be closed and opened again and again by other sessions&#8230;
</p>
<p>
<div class="wp_codebox"><table><tr id="p44336"><td class="code" id="p443code36"><pre class="mysql" style="font-family:monospace;"><span style="color: #008080;">2117</span>    root    localhost       <span style="color: #9900FF; font-weight: bold;">NULL</span>    <span style="color: #000099;">Sleep</span>   <span style="color: #008080;">15</span>              <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #008080;">3032</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       closing <span style="color: #990099; font-weight: bold;">tables</span>  <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> temp_tab <span style="color: #990099; font-weight: bold;">WHERE</span> id<span style="color: #CC0099;">=</span> <span style="color: #000099;">NAME_CONST</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'cnt'</span><span style="color: #000033;">,</span><span style="color: #008080;">9319</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #008080;">3033</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Opening <span style="color: #990099; font-weight: bold;">tables</span>  <span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> temp_tab <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> source_table
<span style="color: #008080;">3034</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Waiting for <span style="color: #990099; font-weight: bold;">table</span>       <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #008080;">3035</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Waiting for <span style="color: #990099; font-weight: bold;">table</span>       <span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> temp_tab <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> source_table
<span style="color: #008080;">3036</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       checking permissions    <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #008080;">3037</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       closing <span style="color: #990099; font-weight: bold;">tables</span>  <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #008080;">3038</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Opening <span style="color: #990099; font-weight: bold;">tables</span>  <span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> temp_tab <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> source_table
<span style="color: #008080;">3039</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       checking permissions    <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #008080;">3040</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       checking permissions    <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #008080;">3041</span>    root    localhost       test    Query   <span style="color: #008080;">0</span>       Waiting for <span style="color: #990099; font-weight: bold;">table</span>       <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> temp_tab <span style="color: #CC0099; font-weight: bold;">LIKE</span> source_table
<span style="color: #808080; font-style: italic;">--
Total open tables: 1 and Name Locks: 1 (output of SHOW OPEN TABLES)</span></pre></td></tr></table></div>

</p>
<p align="left">
So, the best bet is not to use CREATE TABLE LIKE on a highly contended source table or use direct table schema to create the table or if you upgrade to MySQL 5.1 and above; where locking issue is addressed as it opens the source_table instead of getting the named lock that prevents any updates to FRM file.</p>
]]></content:encoded>
			<wfw:commentRss>http://venublog.com/2010/01/10/create-table-like-scalability-issues/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Performance comparison of Repair by Sorting or by Keycache</title>
		<link>http://venublog.com/2010/01/04/performance-comparison-of-repair-by-sorting-or-by-keycache/</link>
		<comments>http://venublog.com/2010/01/04/performance-comparison-of-repair-by-sorting-or-by-keycache/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 11:55:52 +0000</pubDate>
		<dc:creator>Venu Anuganti</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MyISAM repair performanance]]></category>
		<category><![CDATA[myisam_max_sort_file_size]]></category>
		<category><![CDATA[MySQL parallel sort]]></category>
		<category><![CDATA[Parallel repair]]></category>
		<category><![CDATA[Repair by keycache]]></category>
		<category><![CDATA[repair by sort]]></category>
		<category><![CDATA[slow alter table enable keys]]></category>
		<category><![CDATA[sort_buffer_size]]></category>

		<guid isPermaLink="false">http://venublog.com/2010/01/04/performance-comparison-of-repair-by-sorting-or-by-keycache/</guid>
		<description><![CDATA[Other day I noticed a case where loading the same set of data to InnoDB took only 10 minutes where as loading it to MyISAM took ~2 hours. 
Digging it further found that it is all because of well known Repair with keycache issue. But for some reason, it took me a while to get [...]]]></description>
			<content:encoded><![CDATA[<p>Other day I noticed a case where loading the same set of data to InnoDB took only 10 minutes where as loading it to MyISAM took ~2 hours. </p>
<p>Digging it further found that it is all because of well known <strong>Repair with keycache</strong> issue. But for some reason, it took me a while to get to the root cause of the issue as it was working fine until few days. When MyISAM needs to repair the table (REPAIR, ALTER or LOAD or ENABLE KEYS); it uses two modes for repair:</p>
<ul>
<li>repair by sorting </li>
<li>repair using keycache (falls to this mode by default if repair by sort fails for any reason) </li>
</ul>
<p>first it tests if the table can be repaired by <strong>sorting</strong> provided it meets the following requirements: </p>
<ul>
<li>table at least has one key </li>
<li>total size needed for individual key is less than <strong>myisam_max_sort_file_size </strong></li>
</ul>
<p>If it meets the above requirements, then it uses either <strong>regular sorting </strong>if <strong>myisam_repair_threads&#160; = 1</strong> (default) by building each key at a time or in <strong>parallel</strong> if <strong>myisam_repair_threads &gt; 1 </strong>by using ‘n’ threads in parallel (n = total keys in the table). If you have a table with more than one key and table needs a frequent key rebuild, then setting <strong>myisam_repair_threads</strong> = 2 can speedup the repair/alter process.</p>
<p>If it fails to satisfy the above conditions, then it falls to expensive <strong>keycache repair mode</strong>. </p>
<p>Lets consider the following simple example (lengthy index) where one uses &#8216;Repair by sort&#8217; and another uses &#8216;Repair by keycache&#8217; when rebuilding 2M rows; the only difference between the two cases is difference in myisam_max_sort_file_size </p>
</p>

<div class="wp_codebox"><table><tr id="p42342"><td class="code" id="p423code42"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">create</span> <span style="color: #990099; font-weight: bold;">table</span> test_repair_cache <span style="color: #FF00FF;">&#40;</span>s_id <span style="color: #999900; font-weight: bold;">bigint</span> <span style="color: #FF9900; font-weight: bold;">unsigned</span> <span style="color: #CC0099; font-weight: bold;">not</span> <span style="color: #9900FF; font-weight: bold;">null</span><span style="color: #000033;">,</span> 
                                       s_key <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span> 
                                       s_value <span style="color: #999900; font-weight: bold;">text</span><span style="color: #000033;">,</span> 
                                       <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span><span style="color: #FF00FF;">&#40;</span>s_id<span style="color: #000033;">,</span> s_key<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">INDEX</span><span style="color: #FF00FF;">&#40;</span>s_key<span style="color: #000033;">,</span> s_id<span style="color: #FF00FF;">&#41;</span>
        <span style="color: #FF00FF;">&#41;</span><span style="color: #990099; font-weight: bold;">Engine</span><span style="color: #CC0099;">=</span>MyISAM <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #FF9900; font-weight: bold;">CHARSET</span><span style="color: #CC0099;">=</span>utf8 <span style="color: #FF9900; font-weight: bold;">PACK_KEYS</span><span style="color: #CC0099;">=</span><span style="color: #008080;">1</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

</p>
<table>
<tbody>
<tr>
<th>with myisam_max_sort_file_size=1.5G</th>
<th>with myisam_max_sort_file_size=512M</th>
</tr>
<tr>
<td>

<div class="wp_codebox"><table><tr id="p42343"><td class="code" id="p423code43"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">show</span> <span style="color: #990099; font-weight: bold;">global</span> variables <span style="color: #CC0099; font-weight: bold;">like</span> <span style="color: #008000;">'myisam<span style="color: #008080; font-weight: bold;">_</span>max<span style="color: #008080; font-weight: bold;">_</span>sort<span style="color: #008080; font-weight: bold;">_</span>file<span style="color: #008080; font-weight: bold;">_</span>size'</span><span style="color: #000033;">;</span>
<span style="color: #CC0099;">+---------------------------+------------+</span>
<span style="color: #CC0099;">|</span> Variable_name             <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Value</span>      <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+---------------------------+------------+</span>
<span style="color: #CC0099;">|</span> myisam_max_sort_file_size <span style="color: #CC0099;">|</span> <span style="color: #008080;">1610612736</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+---------------------------+------------+</span>
<span style="color: #008080;">1</span> row <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">alter</span> <span style="color: #990099; font-weight: bold;">table</span> test_repair_cache <span style="color: #990099; font-weight: bold;">disable</span> <span style="color: #990099; font-weight: bold;">keys</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">load</span> <span style="color: #990099; font-weight: bold;">data</span> <span style="color: #990099; font-weight: bold;">local</span> <span style="color: #990099; font-weight: bold;">infile</span> <span style="color: #008000;">'x'</span> <span style="color: #990099; font-weight: bold;">into</span> <span style="color: #990099; font-weight: bold;">table</span> test_repair_cache  <span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">2000000</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2</span> <span style="color: #000099;">min</span> <span style="color: #008080;">28.67</span> sec<span style="color: #FF00FF;">&#41;</span>
Records: <span style="color: #008080;">2000000</span>  Deleted: <span style="color: #008080;">0</span>  Skipped: <span style="color: #008080;">0</span>  <span style="color: #990099; font-weight: bold;">Warnings</span>: <span style="color: #008080;">0</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">alter</span> <span style="color: #990099; font-weight: bold;">table</span> test_repair_cache <span style="color: #990099; font-weight: bold;">enable</span> <span style="color: #990099; font-weight: bold;">keys</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">13.96</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span></pre></td></tr></table></div>

</td>
<td>

<div class="wp_codebox"><table><tr id="p42344"><td class="code" id="p423code44"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">show</span> <span style="color: #990099; font-weight: bold;">global</span> variables <span style="color: #CC0099; font-weight: bold;">like</span> <span style="color: #008000;">'myisam<span style="color: #008080; font-weight: bold;">_</span>max<span style="color: #008080; font-weight: bold;">_</span>sort<span style="color: #008080; font-weight: bold;">_</span>file<span style="color: #008080; font-weight: bold;">_</span>size'</span><span style="color: #000033;">;</span>
<span style="color: #CC0099;">+---------------------------+-----------+</span>
<span style="color: #CC0099;">|</span> Variable_name             <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Value</span>     <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+---------------------------+-----------+</span>
<span style="color: #CC0099;">|</span> myisam_max_sort_file_size <span style="color: #CC0099;">|</span> <span style="color: #008080;">536870912</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+---------------------------+-----------+</span>
<span style="color: #008080;">1</span> row <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">alter</span> <span style="color: #990099; font-weight: bold;">table</span> test_repair_cache <span style="color: #990099; font-weight: bold;">disable</span> <span style="color: #990099; font-weight: bold;">keys</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">load</span> <span style="color: #990099; font-weight: bold;">data</span> <span style="color: #990099; font-weight: bold;">local</span> <span style="color: #990099; font-weight: bold;">infile</span> <span style="color: #008000;">'x'</span> <span style="color: #990099; font-weight: bold;">into</span> <span style="color: #990099; font-weight: bold;">table</span> test_repair_cache  <span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">2000000</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2</span> <span style="color: #000099;">min</span> <span style="color: #008080;">28.95</span> sec<span style="color: #FF00FF;">&#41;</span>
Records: <span style="color: #008080;">2000000</span>  Deleted: <span style="color: #008080;">0</span>  Skipped: <span style="color: #008080;">0</span>  <span style="color: #990099; font-weight: bold;">Warnings</span>: <span style="color: #008080;">0</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">alter</span> <span style="color: #990099; font-weight: bold;">table</span> test_repair_cache <span style="color: #990099; font-weight: bold;">enable</span> <span style="color: #990099; font-weight: bold;">keys</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">6</span> <span style="color: #000099;">min</span> <span style="color: #008080;">47.92</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span></pre></td></tr></table></div>

</td>
</tr>
</tbody>
</table>
<p>As you can see, it took only 13 secs when there is a sufficient myisam_max_sort_file_size to sort 2M rows and ~6m&#160; in case of keycache mode. For larger data sets, this can take hours and sometimes even a day or two.</p>
<p>Here is the formula to find the right sort size for the above case (~1.5G) </p>

<div class="wp_codebox"><table><tr id="p42345"><td class="code" id="p423code45"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">select</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2000000</span> <span style="color: #CC0099;">*</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span> <span style="color: #CC0099;">*</span> <span style="color: #008080;">3</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">+</span> <span style="color: #008080;">8</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">/</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">1024</span> <span style="color: #CC0099;">*</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">*</span> <span style="color: #008080;">1024</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> SizeInGig<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+-----------+</span>
<span style="color: #CC0099;">|</span> SizeInGig <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+-----------+</span>
<span style="color: #CC0099;">|</span>    <span style="color: #008080;">1.4249</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+-----------+</span>
<span style="color: #008080;">1</span> row <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<p>SHOW PROCESSLIST thread state will indicate if its using sort or keycache for repairing the keys, for example, here is the show processlist state for all three repair modes</p>

<div class="wp_codebox"><table><tr id="p42346"><td class="code" id="p423code46"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">show</span> processlist<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+-------+---------+-----------+------+---------+------+----------------------+-------------------------------------------+</span>
<span style="color: #CC0099;">|</span> Id    <span style="color: #CC0099;">|</span> <span style="color: #000099;">User</span>    <span style="color: #CC0099;">|</span> Host      <span style="color: #CC0099;">|</span> db   <span style="color: #CC0099;">|</span> Command <span style="color: #CC0099;">|</span> <span style="color: #999900; font-weight: bold;">Time</span> <span style="color: #CC0099;">|</span> State                <span style="color: #CC0099;">|</span> Info                                      <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+-------+---------+-----------+------+---------+------+----------------------+-------------------------------------------+</span>
<span style="color: #CC0099;">|</span> <span style="color: #008080;">23735</span> <span style="color: #CC0099;">|</span> testing <span style="color: #CC0099;">|</span> localhost <span style="color: #CC0099;">|</span> test <span style="color: #CC0099;">|</span> Query   <span style="color: #CC0099;">|</span>  <span style="color: #008080;">248</span> <span style="color: #CC0099;">|</span> Repair <span style="color: #990099; font-weight: bold;">with</span> keycache <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">alter</span> <span style="color: #990099; font-weight: bold;">table</span> test_repair_cache <span style="color: #990099; font-weight: bold;">enable</span> <span style="color: #990099; font-weight: bold;">keys</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+-------+---------+-----------+------+---------+------+----------------------+-------------------------------------------+</span>
&nbsp;
<span style="color: #CC0099;">+-------+---------+-----------+------+---------+------+-----------------------+-------------------------------------------+</span>
<span style="color: #CC0099;">|</span> Id    <span style="color: #CC0099;">|</span> <span style="color: #000099;">User</span>    <span style="color: #CC0099;">|</span> Host      <span style="color: #CC0099;">|</span> db   <span style="color: #CC0099;">|</span> Command <span style="color: #CC0099;">|</span> <span style="color: #999900; font-weight: bold;">Time</span> <span style="color: #CC0099;">|</span> State                 <span style="color: #CC0099;">|</span> Info                                      <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+-------+---------+-----------+------+---------+------+-----------------------+-------------------------------------------+</span>
<span style="color: #CC0099;">|</span> <span style="color: #008080;">23740</span> <span style="color: #CC0099;">|</span> testing <span style="color: #CC0099;">|</span> localhost <span style="color: #CC0099;">|</span> test <span style="color: #CC0099;">|</span> Query   <span style="color: #CC0099;">|</span>    <span style="color: #008080;">4</span> <span style="color: #CC0099;">|</span> Repair <span style="color: #990099; font-weight: bold;">with</span> <span style="color: #008080;">2</span> threads <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">alter</span> <span style="color: #990099; font-weight: bold;">table</span> test_repair_cache <span style="color: #990099; font-weight: bold;">enable</span> <span style="color: #990099; font-weight: bold;">keys</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+-------+---------+-----------+------+---------+------+-----------------------+-------------------------------------------+</span>
&nbsp;
<span style="color: #CC0099;">+-------+---------+-----------+------+---------+------+-------------------+-------------------------------------------+</span>
<span style="color: #CC0099;">|</span> Id    <span style="color: #CC0099;">|</span> <span style="color: #000099;">User</span>    <span style="color: #CC0099;">|</span> Host      <span style="color: #CC0099;">|</span> db   <span style="color: #CC0099;">|</span> Command <span style="color: #CC0099;">|</span> <span style="color: #999900; font-weight: bold;">Time</span> <span style="color: #CC0099;">|</span> State             <span style="color: #CC0099;">|</span> Info                                      <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+-------+---------+-----------+------+---------+------+-------------------+-------------------------------------------+</span>
<span style="color: #CC0099;">|</span> <span style="color: #008080;">23743</span> <span style="color: #CC0099;">|</span> testing <span style="color: #CC0099;">|</span> localhost <span style="color: #CC0099;">|</span> test <span style="color: #CC0099;">|</span> Query   <span style="color: #CC0099;">|</span>    <span style="color: #008080;">5</span> <span style="color: #CC0099;">|</span> Repair by sorting <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">alter</span> <span style="color: #990099; font-weight: bold;">table</span> test_repair_cache <span style="color: #990099; font-weight: bold;">enable</span> <span style="color: #990099; font-weight: bold;">keys</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+-------+---------+-----------+------+---------+------+-------------------+-------------------------------------------+</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://venublog.com/2010/01/04/performance-comparison-of-repair-by-sorting-or-by-keycache/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>ON DUPLICATE KEY With NULL Validation</title>
		<link>http://venublog.com/2009/11/20/on-duplicate-key-with-null-validation/</link>
		<comments>http://venublog.com/2009/11/20/on-duplicate-key-with-null-validation/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 03:01:26 +0000</pubDate>
		<dc:creator>Venu Anuganti</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[innodb]]></category>
		<category><![CDATA[MyISAM ON DUPLICATE]]></category>
		<category><![CDATA[NULL VALUES]]></category>
		<category><![CDATA[On DUPLICATE KEY]]></category>

		<guid isPermaLink="false">http://venublog.com/2009/11/20/on-duplicate-key-with-null-validation/</guid>
		<description><![CDATA[I am not sure if this is a bug or how MySQL works on validating constraints in association with ON DUPLICATE KEY (late or early checking).  For example, consider the following use case (this is irrepective of storage engine and MySQL version):

mysql&#62; create table t1&#40;id int not null primary key, val int not null&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>I am not sure if this is a bug or how MySQL works on validating constraints in association with ON DUPLICATE KEY (late or early checking).  For example, consider the following use case (this is irrepective of storage engine and MySQL version):</p>

<div class="wp_codebox"><table><tr id="p40349"><td class="code" id="p403code49"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">create</span> <span style="color: #990099; font-weight: bold;">table</span> t1<span style="color: #FF00FF;">&#40;</span>id <span style="color: #999900; font-weight: bold;">int</span> <span style="color: #CC0099; font-weight: bold;">not</span> <span style="color: #9900FF; font-weight: bold;">null</span> <span style="color: #990099; font-weight: bold;">primary key</span><span style="color: #000033;">,</span> val <span style="color: #999900; font-weight: bold;">int</span> <span style="color: #CC0099; font-weight: bold;">not</span> <span style="color: #9900FF; font-weight: bold;">null</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">Engine</span><span style="color: #CC0099;">=</span>MyISAM<span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.07</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">insert</span> <span style="color: #990099; font-weight: bold;">into</span> t1 <span style="color: #990099; font-weight: bold;">values</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">10</span><span style="color: #000033;">,</span><span style="color: #008080;">20</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">1</span> row affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.01</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">insert</span> <span style="color: #990099; font-weight: bold;">into</span> t1 <span style="color: #990099; font-weight: bold;">values</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">20</span><span style="color: #000033;">,</span><span style="color: #008080;">10</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">1</span> row affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">create</span> <span style="color: #990099; font-weight: bold;">table</span> t2<span style="color: #FF00FF;">&#40;</span>id1 <span style="color: #999900; font-weight: bold;">int</span> <span style="color: #CC0099; font-weight: bold;">not</span> <span style="color: #9900FF; font-weight: bold;">null</span> <span style="color: #990099; font-weight: bold;">primary key</span><span style="color: #000033;">,</span> val1 <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">Engine</span><span style="color: #CC0099;">=</span>MyISAM<span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.14</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">insert</span> <span style="color: #990099; font-weight: bold;">into</span> t2 <span style="color: #990099; font-weight: bold;">values</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">10</span><span style="color: #000033;">,</span><span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">1</span> row affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">insert</span> <span style="color: #990099; font-weight: bold;">into</span> t1<span style="color: #FF00FF;">&#40;</span>id<span style="color: #000033;">,</span> val<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">select</span> id1<span style="color: #000033;">,</span> val1 <span style="color: #990099; font-weight: bold;">from</span> t2 <span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #990099; font-weight: bold;">DUPLICATE KEY</span> <span style="color: #990099; font-weight: bold;">UPDATE</span> val<span style="color: #CC0099;">=</span><span style="color: #009900;">IF</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">VALUES</span><span style="color: #FF00FF;">&#40;</span>val<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">IS</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span> val<span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">VALUES</span><span style="color: #FF00FF;">&#40;</span>val<span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">2</span> rows affected<span style="color: #000033;">,</span> <span style="color: #008080;">1</span> warning <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
Records: <span style="color: #008080;">1</span>  Duplicates: <span style="color: #008080;">1</span>  <span style="color: #990099; font-weight: bold;">Warnings</span>: <span style="color: #008080;">1</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">show</span> <span style="color: #990099; font-weight: bold;">warnings</span><span style="color: #000033;">;</span>
<span style="color: #CC0099;">+---------+------+-----------------------------+</span>
<span style="color: #CC0099;">|</span> Level   <span style="color: #CC0099;">|</span> Code <span style="color: #CC0099;">|</span> Message                     <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+---------+------+-----------------------------+</span>
<span style="color: #CC0099;">|</span> Warning <span style="color: #CC0099;">|</span> <span style="color: #008080;">1048</span> <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Column</span> <span style="color: #008000;">'val'</span> cannot be <span style="color: #9900FF; font-weight: bold;">null</span> <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+---------+------+-----------------------------+</span>
<span style="color: #008080;">1</span> row <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">select</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">from</span> t1<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+------+</span>
<span style="color: #CC0099;">|</span> id <span style="color: #CC0099;">|</span> val  <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+------+</span>
<span style="color: #CC0099;">|</span> <span style="color: #008080;">10</span> <span style="color: #CC0099;">|</span>    <span style="color: #008080;">0</span> <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">|</span> <span style="color: #008080;">20</span> <span style="color: #CC0099;">|</span>   <span style="color: #008080;">10</span> <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+------+</span>
<span style="color: #008080;">2</span> rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<p>In the above case, you have table t1 with &#8216;val&#8217; column being NOT NULL; and t2 is trying to insert to t1 with NULL for val column as part of the INSERT, and ON DUPLICATE KEY; it actually keeps the old value and will not populate the NULL to t1 at all (common use case when you have a business logic to replace old with new or keep the old as is when new value is not appropriate or not modified, which in general is represented by NULL).</p>
<p>So; but as you can see; the validation of NULL happens before the ON DUPLICATE constraint checking; and in the above case, it should not as already primary key satisfies the duplicate constraint (before trigger of fill_records) and server should proceed with changes from what was specified from the ON DUPLICATE section (after trigger).</p>
<p>That is apart; and still you can see that it got a new value 0 from no where by replacing the old one 20 for column val, which is odd. The correct behavior is to just check only primary/unique constraint values when ON DUPLICATE KEY is specified in early binding and validate the rest in late binding by taking the final output from ON DUPLICATE clause&#8230; then the above should have been&#8230;</p>

<div class="wp_codebox"><table><tr id="p40350"><td class="code" id="p403code50"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">insert</span> <span style="color: #990099; font-weight: bold;">into</span> t1<span style="color: #FF00FF;">&#40;</span>id<span style="color: #000033;">,</span> val<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">select</span> id1<span style="color: #000033;">,</span> val1 <span style="color: #990099; font-weight: bold;">from</span> t2 <span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #990099; font-weight: bold;">DUPLICATE KEY</span> <span style="color: #990099; font-weight: bold;">UPDATE</span> <span style="color: #000099;">conv</span><span style="color: #CC0099;">=</span><span style="color: #009900;">IF</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">VALUES</span><span style="color: #FF00FF;">&#40;</span>val<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">IS</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span> val<span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">VALUES</span><span style="color: #FF00FF;">&#40;</span>val<span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">2</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
Records: <span style="color: #008080;">1</span>  Duplicates: <span style="color: #008080;">1</span>  <span style="color: #990099; font-weight: bold;">Warnings</span>: <span style="color: #008080;">0</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">select</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">from</span> t1<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+------+</span>
<span style="color: #CC0099;">|</span> id <span style="color: #CC0099;">|</span> val  <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+------+</span>
<span style="color: #CC0099;">|</span> <span style="color: #008080;">10</span> <span style="color: #CC0099;">|</span>   <span style="color: #008080;">20</span> <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">|</span> <span style="color: #008080;">20</span> <span style="color: #CC0099;">|</span>   <span style="color: #008080;">10</span> <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+------+</span>
<span style="color: #008080;">2</span> rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<p>As no other server supports ON DUPLICATE KEY, its hard to derive what is the right logic that needs to be applied here. But I still think early validation is wrong here as the value can change in the late binding due to other constraints that can happen from ON DUPLICATE KEY derivation.</p>
<p>The above is only one use case with NULL; this can be simulated to other constraints as well&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://venublog.com/2009/11/20/on-duplicate-key-with-null-validation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
