<?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>Mike Willbanks &#187; MySQL</title>
	<atom:link href="http://blog.digitalstruct.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.digitalstruct.com</link>
	<description>Getting inside the mind of a php developer.</description>
	<lastBuildDate>Thu, 22 Dec 2011 04:04:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Minnesota MySQL + PHP User Group</title>
		<link>http://blog.digitalstruct.com/2009/05/13/minnesota-mysql-php-user-group/</link>
		<comments>http://blog.digitalstruct.com/2009/05/13/minnesota-mysql-php-user-group/#comments</comments>
		<pubDate>Thu, 14 May 2009 03:06:34 +0000</pubDate>
		<dc:creator>Mike Willbanks</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.digitalstruct.com/?p=82</guid>
		<description><![CDATA[With Oracle purchasing up Sun Microsystems (who hasn&#8217;t heard of all the rage), MySQL has decided that they were no longer going to support Meetup.com which they have been doing for the last 4 years. Their response to ending the support for Meetup is to have the user groups go to Facebook. This in my [...]]]></description>
			<content:encoded><![CDATA[<p>With Oracle purchasing up Sun Microsystems (who hasn&#8217;t heard of all the rage), MySQL has decided that they were no longer going to support Meetup.com which they have been doing for the last 4 years.  Their response to ending the support for Meetup is to have the user groups go to Facebook.  This in my opinion is not a great place for a meetup and I will likely be moving the meetup to it&#8217;s own site.</p>
<p>I want to take this opportunity to thank the efforts of David Hedrick Skarjune for keeping this user group going for the last 4 1/2 years and look forward to guiding this group in the future as David has made me organizer.</p>
<p>Currently we take on PHP as a subset item, my plan is to separate this completely and focus on the MySQL User Group being more of a MySQL/Drizzle User Group.  With the latest Oracle takeover I truly believe that we will see Drizzle take more strength and the project will see enormous growth as it becomes stable and starts sending out releases.</p>
<p>The current plan is to keep the Meetup membership going for a few months as we transition either to a new system or a custom built system that will give the flexibility that I feel that the user group will need going forward.  Additionally I believe that we will see more presentations happening and several discussions spawned at the TC Web Design meetup tonight.</p>
<p>I look forward to organizing the group and we already have a few companies ready to provide space, pizza, pop and beer.  If you are in the area and want to drop by or are willing to present or have any ideas that you would like to see this meetup produce, please feel free to let me know.  I will be planning to have a meeting in June at some point.  Presently I have no additional details on what we will be presenting or what topics are going to be coming up.</p>
<p>What are the other user groups using out there for a system to maintain their user groups?  While I could build something custom, I certainly do not have the time to dedicate to building another system as I already have quite a few in the works.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.digitalstruct.com/2009/05/13/minnesota-mysql-php-user-group/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Helping to Pay for Drizzle.org</title>
		<link>http://blog.digitalstruct.com/2008/10/27/helping-to-pay-for-drizzleorg/</link>
		<comments>http://blog.digitalstruct.com/2008/10/27/helping-to-pay-for-drizzleorg/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 03:13:38 +0000</pubDate>
		<dc:creator>Mike Willbanks</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.digitalstruct.com/?p=62</guid>
		<description><![CDATA[Well, it has certainly been sometime since I had blogged last, however, that does not mean that I have not been keeping note of what is going on in the industry. If you have not heard about the Drizzle Project, then you must be living in a cave! The Drizzle project is building a database [...]]]></description>
			<content:encoded><![CDATA[<p>Well, it has certainly been sometime since I had blogged last, however, that does not mean that I have not been keeping note of what is going on in the industry.  If you have not heard about the <a href="http://drizzleproject.org/">Drizzle Project</a>, then you must be living in a cave! <img src='http://blog.digitalstruct.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<blockquote><p>The Drizzle project is building a database optimized for computing cloud and web applications. It is being designed for massive concurrency on modern multi-cpu/core architecture. The code is originally derived from MySQL. </p></blockquote>
<p><a href="http://michaelshadle.com/">Mike Shadle</a> recently made a contribution by negotiating and purchasing Drizzle.org for $1K.  If you haven&#8217;t contributed back to open source, here is a chance to show your token of appreciation.  <a href="http://michaelshadle.com/2008/10/24/establishing-a-permanent-address-for-the-drizzle/">Help to support this project and help pay for Drizzle.org</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.digitalstruct.com/2008/10/27/helping-to-pay-for-drizzleorg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Performance Series: Maximizing Your MySQL Database</title>
		<link>http://blog.digitalstruct.com/2008/06/18/php-performance-series-maximizing-your-mysql-database/</link>
		<comments>http://blog.digitalstruct.com/2008/06/18/php-performance-series-maximizing-your-mysql-database/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 05:57:10 +0000</pubDate>
		<dc:creator>Mike Willbanks</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.digitalstruct.com/?p=55</guid>
		<description><![CDATA[In the first article of the PHP Performance Series, I focused on PHP Caching Techniques. This time I want to talk about maximizing your database. This article will deal mostly with MySQL, however, you should be able to note many of the different aspects even if you do not directly utilize MySQL. Application SQL Performance [...]]]></description>
			<content:encoded><![CDATA[<p>In the first article of the PHP Performance Series, I focused on <a href="/2008/02/27/php-performance-series-caching-techniques/">PHP Caching Techniques</a>.  This time I want to talk about maximizing your database.  This article will deal mostly with MySQL, however, you should be able to note many of the different aspects even if you do not directly utilize MySQL.</p>
<h2>Application SQL Performance</h2>
<p>Application level SQL performance is much different than the performance of the SQL query itself but rather how it has been designed to work in the application.  Many of the items I will be addressing in this area is designing your application to make less queries thus improving scalability and likely performance.  However, performance does not always equal scalability as the same with scalability does not always equal performance.</p>
<p>If you have read my blog before you may notice that I have used some of this content before but putting in this section for terms of completeness.</p>
<h3>Lazy Connections</h3>
<p>Utilizing lazy connections for your database is a great step in applications that do not need to utilize the database through a full request or even if it needs to be utilized at all.  The concept here is to not initialize the connection to the database unless absolutely essential to keep your connection pool free of massive amounts of sleeping connections.</p>
<h4>Simple Lazy Connection Example</h4>
<p>While not a full example, I believe this shows you a simple technique in handling lazy connections.</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_Db <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_connected</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_connection</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> connect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//method will simply set the connection variables</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connected <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connected<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></p>
<h3>Iterating Queries</h3>
<p>This is one of the most common items I usually see when looking over another developers code or even several of the open source projects out there.  I am defining iterating queries as a query that executes on a loop.  These can be very expensive and often times are definitely not needed.</p>
<h4>An Iterating Query Example</h4>
<p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ids'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ids'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$rs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT * FROM my_table WHERE my_id = '</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></p>
<h4>Fixing The Iterating Query Example</h4>
<p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ids'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$ids</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'intval'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ids'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$ids</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ids</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$rs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT * FROM my_table WHERE my_id IN ('</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ids</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">')'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></p>
<h3>Need Based Selects</h3>
<p>There is no need to select information that you do not need.  For starters this increases the memory usage on the request as well as the I/O time in fetching all of the record data from the columns that are not being utilized and transferring them through PHP.  The more data you select the slower the query and larger the memory footprint.  This is especially true with the TEXT and BLOG column types.</p>
<h4>SELECT * IS BAD!</h4>
<p>This actually is bad in 2 different areas.  First and of a higher concern is what is actually being utilized from the query if you ever need to change something in the application?  Say you have a large application with thousands of files that utilize a select query and you will have no idea where the variables are without actually researching each and every area of the application that has the wonderful SELECT *.  Basically your maintainability slowly dies.  Secondly as I stated before, there is also a performance and memory hit here, simply stated, do not use SELECT *.</p>
<p>One question that is commonly asked after this comment is what if I am using all of the columns?  Still, are you going to be using all of the columns in 3 months, 6 months, 1 year, 5 years?</p>
<h3>Use the Correct Data Type (Don&#8217;t Quote Everything)</h3>
<p>Yes, utilizing the correct type of data in your query does matter.  You can cause the database to miss indexes or come back with invalid results.  Besides that aspect it is slower since the database has to convert the data into the correct type.</p>
<h4>Example of Incorrect Data Types</h4>
<p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$rs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM my_table WHERE my_id = '<span style="color: #006699; font-weight: bold;">{$id}</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></p>
<h4>Example of Correct Data Types</h4>
<p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$rs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT * FROM my_table WHERE my_id = '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></p>
<p>By the way, the 2nd one is also quicker because we only had to type cast for the numeric instead of running it through mysql_real_escape_string.</p>
<h4>Hierarchical Data</h4>
<p>When utilizing navigation trees, you should ensure that you are utilizing a proper technique and not pushing your database for it.  Further, cache it instead of hitting it every request at the same time!  There is already a great article over at <a href="http://dev.mysql.com/tech-resources/articles/hierarchical-data.html" onclick="window.open(this.href, '_blank');return false;">MySQL on Managing Hierarchical Data</a> so I am not going to go deep into this.  But anytime you are managing a tree of data please spare other developers and utilize it correctly.</p>
<h2>Database Design</h2>
<p>Database design is typically the first area that you find problems start.  A bad data model can plague your application with both performance and maintainability concerns.  However, also note, the more performance driven that you make your database, the less maintainable that it can become (debatable &#8211; can really depend on size and scale).</p>
<p>Please note that this is not an all encompassing list as I wanted to give a little more of an idea from a developer point of view on the optimization techniques in database design rather than providing a full guide.  If you are looking for a deep level of information I invite you to please go to the <a href="http://dev.mysql.com/doc/refman/5.0/en/optimizing-database-structure.html" onclick="window.open(this.href, '_blank');return false;">MySQL Manual on Database Design</a> Feel free to add recommendations in the comments.</p>
<h3>Normalization</h3>
<p>Normalization is a technique utilized for minimizing the duplication of information.  This is typically the best thing to start out with, if you are currently having problems and your database is not well normalized focus on that first.  Likely you have tables, columns or data that shouldn&#8217;t need to work as it currently is.</p>
<h4>Columns instead of Table Example</h4>
<p>A common problem that many applications you find have is that they store the data into just columns instead of actually making a one-to-many table.  Take for instance the following table:</p>
<table>
<caption><strong>app_user</strong></caption>
<thead>
<tr>
<th>Column Name</th>
<th>Column Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>user_id</td>
<td>integer</td>
</tr>
<tr>
<td>user_email</td>
<td>varchar</td>
</tr>
<tr>
<td>user_website</td>
<td>varchar</td>
</tr>
<tr>
<td>user_website_2</td>
<td>varchar</td>
</tr>
<tr>
<td>user_website_3</td>
<td>varchar</td>
</tr>
</tbody>
</table>
<p>What you are likely seeing here is a user requested to have up to 3 websites and the developer working on it figured there would never need to be anymore.  Now this is a focus for gaining further modularity as well as normalization.</p>
<h4>Table instead of Columns Example</h4>
<p>Using a table instead of columns would better support this feature as well as allow for further growth in the future.</p>
<table>
<caption><strong>app_user</strong></caption>
<thead>
<tr>
<th>Column Name</th>
<th>Column Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>user_id</td>
<td>integer</td>
</tr>
<tr>
<td>user_email</td>
<td>varchar</td>
</tr>
</tbody>
</table>
<table>
<caption><strong>app_user_website</strong></caption>
<thead>
<tr>
<th>Column Name</th>
<th>Column Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>website_id</td>
<td>integer</td>
</tr>
<tr>
<td>user_id</td>
<td>integer</td>
</tr>
<tr>
<td>website_url</td>
<td>varchar</td>
</tr>
</tbody>
</table>
<p>You might be thinking, what does this have to do with performance?  Well it has to deal with maintainability, better handling of your indexes, as well as lowering the table size in the initial table.  Size of your tables does matter definitely when you need to start adding more indexes to columns that you might not need to.  Take both examples and attempt to find users without a website entered without an index on the user_website columns.  This becomes very slow instead of doing a simple select au.user_id from app_user au where au.user_id NOT IN (select distinct user_id from app_user_website) instead of having to add an additional index instead of simply using the foreign key that you would likely have defined in the app_user_website table for the user_id.</p>
<h3>Denormalization</h3>
<p>Denormalization is the concept of copying data into other tables in order to reduce the amount of joins that you need to create.  These should typically always be handled with triggers and if you are unable to do triggers, please ensure that anything that creates, modifies or deletes handles through a layer of encapsulation.  Otherwise you will end up with stale records.</p>
<p>You will only need to do this when you have exhausted all of the other routes such as checking and creating indexes, if data access was really needed and lastly there was no other method for the needs that you were going after.</p>
<h3>Table Types</h3>
<p>Use the correct table type for what you are doing or attempting to do.  I suggest setting up a matrix of what you need your table to do as well as your database as a whole.  This first step is often neglected by many developers.</p>
<table>
<caption>Example List</caption>
<thead>
<tr>
<th>Feature</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<tr>
<td>Read vs. Write &gt; 15%</td>
<td>Yes/No</td>
</tr>
<tr>
<td>Transactions</td>
<td>Yes/No</td>
</tr>
<tr>
<td>Foreign Key Support</td>
<td>Yes/No</td>
</tr>
<tr>
<td>Full-Text Indexes</td>
<td>Yes/No</td>
</tr>
</tbody>
</table>
<p>The list above is by far no means a full list but you should document what you need and what you are using the database for.  For example if you are reading and writing on the same database table, MyISAM is likely a bad idea if you have greater than 15% of reads or writes.  MyISAM tables will easily lock when there is a long read and a subsequent insert with a table lock thus not allowing any further reads until that insert has completed.  So make a list and figure out what needs to be there.  This will certainly help you with furthering your database.</p>
<h2>SQL Query Optimizations</h2>
<p>Optimizing your SQL to perform is not rocket science, however, this seems to be one area where applications seem to start crumbling down from the point of an application hitting popularity.  Simply the features that are developed and time change the database is rarely taken into effect in what might be affected.</p>
<p>A simple rule to follow is when utilizing your database to design your queries to your database architecture and current rules, when those cannot be achieved refactor and adjust the database to be able to handle the new situations.</p>
<h3>The Simple Rules</h3>
<ul>
<li>Use your explain/execution plan</li>
<li>The less joins the better</li>
<li>Ensure you are utilizing your indexes (see first bullet)</li>
<li>Temporary tables can be good when doing operations on complex data sets</li>
<li>Stay away from derived tables and non-materialized views (see above bullet)</li>
<li>Roll up data that can be aggregated</li>
<li><strong>Select the columns you need, not SELECT *</strong></li>
</ul>
<p>If you are looking for ways to better optimize your queries, again, please go to the <a href="http://dev.mysql.com/doc/refman/5.0/en/query-speed.html" onclick="window.open(this.href, '_blank');return false;">MySQL Manual on Query Optimization</a></p>
<h2>Exit(0);</h2>
<p>You may have noticed that this blog post has taken me quite a while to push this out.  Besides in  the middle of purchasing a home, some resource constraints at work, a couple side projects and maintaining my life I just didn&#8217;t have much time to finish writing a more complete post.</p>
<p>To go a little further, I&#8217;ve cut down the contents in this blog as you may have been sick of reading already as well as the amount of information that could have been potentially written here could easily have been a full book if you wanted to get into each and every aspect.  I figured for my sanity as well as yours I should cut it shorter.  If you have any information to add please submit comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.digitalstruct.com/2008/06/18/php-performance-series-maximizing-your-mysql-database/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using apc
Page Caching using apc
Database Caching 1/19 queries in 0.008 seconds using apc
Object Caching 300/327 objects using apc

Served from: blog.digitalstruct.com @ 2012-02-04 06:41:33 -->
