

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ShareDigest blog &#187; software</title>
	<atom:link href="http://blog.sharedigest.com/category/software/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sharedigest.com</link>
	<description>Trading, software, web and random stuff</description>
	<lastBuildDate>Tue, 06 Jul 2010 11:30:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Optimizing MySQL 1: Using Indexes</title>
		<link>http://blog.sharedigest.com/optimizing-mysql-1-using-indexes/</link>
		<comments>http://blog.sharedigest.com/optimizing-mysql-1-using-indexes/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 04:45:35 +0000</pubDate>
		<dc:creator>SD</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[improve mysql performance]]></category>
		<category><![CDATA[mysql indexes]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[query optimization]]></category>

		<guid isPermaLink="false">http://blog.sharedigest.com/?p=20</guid>
		<description><![CDATA[Using Indexing To Speed Up MySQL



So recently I&#8217;ve come up against some issues with our website, webCV. The problem? Mysql tables were not correctly optimized, making the site run extremely slow since it was a heavily database intensive site. So after doing some reading in a great MySQL book, I decided to test some concepts, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Using Indexing To Speed Up MySQL</strong></p>
<table>
<tr>
<td width="50%">
So recently I&#8217;ve come up against some issues with our website, <a title="webCV" href="http://www.webcv.com.au" target="_blank">webCV</a>. The problem? Mysql tables were not correctly optimized, making the site run extremely slow since it was a heavily database intensive site. So after doing some reading in a great MySQL book, I decided to test some concepts, such as indexing, and that will be covered here: How to optimize your MySQL tables using indexes.
</td>
<td width="50%">
<div align="right">
<script type="text/javascript"><!--
google_ad_client = "pub-1739519678603459";
/* SDblog_300_250, created 9/24/09 */
google_ad_slot = "6287581496";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
</td>
</tr>
</table>
<p>The exact workings of indexes will not be discussed here (for a more complete discussion of how they work, see <a title="http://dev.mysql.com/doc/refman/5.1/en/mysql-indexes.html" href="http://dev.mysql.com/doc/refman/5.1/en/mysql-indexes.html">http://dev.mysql.com/doc/refman/5.1/en/mysql-indexes.html</a> )</p>
<p>What we&#8217;ll also be looking at co-incidentally is how MySQL stored procedures can be used to populate tables quickly and easily with data (in this case random data).</p>
<p><strong>Demo of Index Performance</strong></p>
<p>In order to see how much indexes can improve query performance across multiple tables, we shall conduct a simple test involving three tables, t1, t2, and t3 with each table containing a column i1,i2 and i3 respectively and each having 1000 rows that are populated as follows: column i1 has rows with values 1 &#8211; 1000, i2 random values in range: <img src="http://blog.sharedigest.com/wp-content/cache/tex_3861e3dced2dbfac66da34ca7b52203d.png" align="absmiddle" class="tex" alt=" \{ 0, 20,000 \} " />, and column i3 the same as i2 (but different values).</p>
<p>The following MySQL procedure achieves the above by creating and populating the tables as needed. Simply import it from your Linux command line as you would any sql file:</p>
<p>The pop_random.sql file can be found here, exposing the init_tbl(&#8217;tablename&#8217;, no_random_entries) procedure call to us.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;">DELIMITER $
<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> init_tbl $
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> init_tbl<span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> tbl <span style="color: #000099;">CHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">64</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">IN</span> <span style="color: #000099;">upper</span> <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">BEGIN</span>
	<span style="color: #990099; font-weight: bold;">DECLARE</span> <span style="color: #000099;">count</span> <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #000099;">count</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span><span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">-- rem all first</span>
	<span style="color: #990099; font-weight: bold;">SET</span> @s <span style="color: #CC0099;">:=</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'DELETE FROM '</span><span style="color: #000033;">,</span> tbl<span style="color: #000033;">,</span> <span style="color: #008000;">' WHERE 1'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	PREPARE stmt <span style="color: #990099; font-weight: bold;">FROM</span> @s<span style="color: #000033;">;</span>
	EXECUTE stmt<span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">-- Loop from 1 to upper--</span>
	WHILE <span style="color: #000099;">count</span> <span style="color: #CC0099;">&amp;</span>lt<span style="color: #000033;">;</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">upper</span> <span style="color: #CC0099;">+</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">DO</span>
		<span style="color: #990099; font-weight: bold;">SET</span> @s <span style="color: #CC0099;">:=</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'INSERT INTO '</span><span style="color: #000033;">,</span> tbl<span style="color: #000033;">,</span> <span style="color: #008000;">'(i0, i1,i2,i3) VALUES ('</span><span style="color: #000033;">,</span><span style="color: #000099;">count</span><span style="color: #000033;">,</span> <span style="color: #008000;">', 0, 0, 0)'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
		PREPARE stmt <span style="color: #990099; font-weight: bold;">FROM</span> @s<span style="color: #000033;">;</span>
		EXECUTE stmt<span style="color: #000033;">;</span>
		<span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #000099;">count</span> <span style="color: #CC0099;">=</span> <span style="color: #000099;">count</span><span style="color: #CC0099;">+</span><span style="color: #008080;">1</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span> WHILE<span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #990099; font-weight: bold;">CALL</span> pop_random<span style="color: #FF00FF;">&#40;</span>tbl<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #009900;">END</span> $
&nbsp;
<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> pop_random $
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> pop_random<span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> tbl <span style="color: #000099;">CHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">64</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">BEGIN</span>
	<span style="color: #990099; font-weight: bold;">SET</span> @r <span style="color: #CC0099;">:=</span> <span style="color: #008080;">0</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">SET</span> @s <span style="color: #CC0099;">:=</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'UPDATE '</span><span style="color: #000033;">,</span> tbl<span style="color: #000033;">,</span> <span style="color: #008000;">' SET '</span><span style="color: #000033;">,</span> tbl<span style="color: #000033;">,</span><span style="color: #008000;">'.i1= (@r := @r + 1) order by rand()'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	PREPARE stmt <span style="color: #990099; font-weight: bold;">FROM</span> @s<span style="color: #000033;">;</span>
	EXECUTE stmt<span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #990099; font-weight: bold;">SET</span> @r <span style="color: #CC0099;">:=</span> <span style="color: #008080;">0</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">SET</span> @s <span style="color: #CC0099;">:=</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'UPDATE '</span><span style="color: #000033;">,</span> tbl<span style="color: #000033;">,</span> <span style="color: #008000;">' SET '</span><span style="color: #000033;">,</span> tbl<span style="color: #000033;">,</span><span style="color: #008000;">'.i2= (@r := @r + 1) order by rand()'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	PREPARE stmt <span style="color: #990099; font-weight: bold;">FROM</span> @s<span style="color: #000033;">;</span>
	EXECUTE stmt<span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #990099; font-weight: bold;">SET</span> @r <span style="color: #CC0099;">:=</span> <span style="color: #008080;">0</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">SET</span> @s <span style="color: #CC0099;">:=</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'UPDATE '</span><span style="color: #000033;">,</span> tbl<span style="color: #000033;">,</span> <span style="color: #008000;">' SET '</span><span style="color: #000033;">,</span> tbl<span style="color: #000033;">,</span><span style="color: #008000;">'.i3= (@r := @r + 1) order by rand()'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	PREPARE stmt <span style="color: #990099; font-weight: bold;">FROM</span> @s<span style="color: #000033;">;</span>
	EXECUTE stmt<span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #990099; font-weight: bold;">SET</span> @msg <span style="color: #CC0099;">:=</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span>tbl<span style="color: #000033;">,</span> <span style="color: #008000;">' successfully created and populated.'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">SELECT</span> @msg<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #009900;">END</span> $
&nbsp;
DELIMITER <span style="color: #000033;">;</span>
&nbsp;
<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> <span style="color: #008000;">`t1`</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #008000;">`t1`</span> <span style="color: #FF00FF;">&#40;</span>
  <span style="color: #008000;">`i0`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</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>
  <span style="color: #008000;">`i1`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</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>
  <span style="color: #008000;">`i2`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</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>
  <span style="color: #008000;">`i3`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</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>
  <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span>  <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`i0`</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>MyISAM <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #FF9900; font-weight: bold;">CHARSET</span><span style="color: #CC0099;">=</span>latin1<span style="color: #000033;">;</span>
&nbsp;
<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> <span style="color: #008000;">`t2`</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #008000;">`t2`</span> <span style="color: #FF00FF;">&#40;</span>
  <span style="color: #008000;">`i0`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</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>
  <span style="color: #008000;">`i1`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</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>
  <span style="color: #008000;">`i2`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</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>
  <span style="color: #008000;">`i3`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</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>
  <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span>  <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`i0`</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>MyISAM <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #FF9900; font-weight: bold;">CHARSET</span><span style="color: #CC0099;">=</span>latin1<span style="color: #000033;">;</span>
&nbsp;
<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> <span style="color: #008000;">`t3`</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #008000;">`t3`</span> <span style="color: #FF00FF;">&#40;</span>
  <span style="color: #008000;">`i0`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</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>
  <span style="color: #008000;">`i1`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</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>
  <span style="color: #008000;">`i2`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</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>
  <span style="color: #008000;">`i3`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</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>
  <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span>  <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`i0`</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>MyISAM <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #FF9900; font-weight: bold;">CHARSET</span><span style="color: #CC0099;">=</span>latin1<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- _initialize the tables</span>
<span style="color: #990099; font-weight: bold;">call</span> init_tbl<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'t1'</span><span style="color: #000033;">,</span> <span style="color: #008080;">1000</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">call</span> init_tbl<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'t2'</span><span style="color: #000033;">,</span> <span style="color: #008080;">1000</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">call</span> init_tbl<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'t3'</span><span style="color: #000033;">,</span> <span style="color: #008080;">1000</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></div></div>

<p>Running the above script will give us the three tables each with 3 columns i1-i3, each with 1000 lines of random, unique numbers. Stored procedures can make it easy to keep common procedures in SQL grouped together and perform complex operations. For example, if you&#8217;d like to populate the table &#8216;t1&#8242; with more rows, simply run the query:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">call</span> init_tbl<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'t1'</span><span style="color: #000033;">,</span> <span style="color: #008080;">20000</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></div></div>

<p>Getting back to indexing, we&#8217;ll focus on the following three columns: t1.i1, t2.i2 and t3.i3. We write the following query to find all combinations of table rows in which the values are equal:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> t1.i1<span style="color: #000033;">,</span> t2.i2<span style="color: #000033;">,</span> t3.i3 <span style="color: #990099; font-weight: bold;">FROM</span> t1 <span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> t2 <span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> t3 <span style="color: #990099; font-weight: bold;">WHERE</span> t1.i1 <span style="color: #CC0099;">=</span> t2.i2 <span style="color: #CC0099; font-weight: bold;">AND</span> t2.i2 <span style="color: #CC0099;">=</span> t3.i3<span style="color: #000033;">;</span></pre></div></div>

<p>That query took 2 min 5.36 sec to finish &#8211; quite a while! By running this query without using indexes, we have to scan ALL rows to determine which rows contain which values. So all combinations must be tried to find the matches on the WHERE clause. The largest possible number of combinations is: 20,000 x 20,000 x 20,000 &#8211; a very large number! That&#8217;s alot of wasted effort. As the tables grow, the processing time increases even more in the absence of indexes, leading to poor performance. By using indexes, performance can be greatly improved due to the query being processed as follows:</p>
<ol>
<li>Determine the value of the first row from table t1;</li>
<li>Now, using the index on t2, immediately find the value that matches the value from the 1). (Similarly, an index on t3 will allow the value from t2 to be found immediately).</li>
<li>Go to the next row of t1 and repeat steps 1) &#8211; 2), until all rows in t1 have been processed.</li>
</ol>
<p>So the above procedure still does a full scan of table 1, but indexed lookups on t2 and t3 will cause the query to run many many times faster. Lets create indexes on the tables and run our above query again to evaluate performance improvement:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#Do same for t3.i3</span>
 <span style="color: #990099; font-weight: bold;">ALTER</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #008000;">`t2`</span> <span style="color: #990099; font-weight: bold;">ADD</span> <span style="color: #990099; font-weight: bold;">INDEX</span> <span style="color: #FF00FF;">&#40;</span> <span style="color: #008000;">`i2`</span> <span style="color: #FF00FF;">&#41;</span> <span style="color: #000033;">;</span></pre></div></div>

<p>Now running our select &amp; join query, produces the same output as before, but in 0.22 secs &#8211; a speed increase of roughly 568 times. This increase will be even more the larger the tables are.</p>
<p><strong>How To Decide Which Columns To Index</strong></p>
<table>
<tr>
<td width="50%">
Columns that are used for searching, sorting or grouping are ideal candidates for indexing. Columns that are used for output should be left out. Thus, the columns that appear in the WHERE clause, columns named in join clauses or columns that appear in ORDER BY or GROUP BY clauses are the type of columns you want to index. Be careful not to over-index, since indexes can slow down insert/update operations.
</td>
<td width="50%">
<div align="right">
<script type="text/javascript"><!--
google_ad_client = "pub-1739519678603459";
/* SDblog_300_250, created 9/24/09 */
google_ad_slot = "6287581496";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
</td>
</tr>
</table>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.sharedigest.com/handy-subversion-commands/" rel="bookmark">Handy Subversion Commands</a></li><li><a href="http://blog.sharedigest.com/algo-trading-101-part-2-backtesting/" rel="bookmark">Algo-Trading 101 Part 2: Backtesting</a></li><li><a href="http://blog.sharedigest.com/options-2-call-options/" rel="bookmark">Options 2: Call Options</a></li><li><a href="http://blog.sharedigest.com/cron-task-for-automated-backups/" rel="bookmark">Cron Task For Automated Backups</a></li><li><a href="http://blog.sharedigest.com/algo-trading-101-part-1-getting-started/" rel="bookmark">Algo-Trading 101 Part 1: Getting Started</a></li></ul></div><hr /><small>Copyright &copy; 2008<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small>]]></content:encoded>
			<wfw:commentRss>http://blog.sharedigest.com/optimizing-mysql-1-using-indexes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

