<?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>damonparker.org &#187; SQL</title>
	<atom:link href="http://damonparker.org/blog/category/technical/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://damonparker.org</link>
	<description>code / ecommerce / life</description>
	<lastBuildDate>Thu, 24 Feb 2011 21:29: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>MySQL Error Function Wrapper for PHP</title>
		<link>http://damonparker.org/blog/2011/02/07/mysql-error-function-wrapper-for-php/</link>
		<comments>http://damonparker.org/blog/2011/02/07/mysql-error-function-wrapper-for-php/#comments</comments>
		<pubDate>Tue, 08 Feb 2011 00:01:32 +0000</pubDate>
		<dc:creator>damonp</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://damonparker.org/?p=634</guid>
		<description><![CDATA[I was recently asked to supply some development and debugging functionality to a web application built for a client. The following function can be configured to popup for only your IP, and gives an error message containing the SQL, line number and file for easy debugging of the error. I like this function over my [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I was recently asked to supply some development and debugging functionality to a web application built for a client.  The following function can be configured to popup for only your IP, and gives an error message containing the SQL, line number and file for easy debugging of the error.</p>
<p>I like this function over my current hobbled together debug kit because the display is positioned top center of the page via CSS no matter where the error occurs.  Plus it includes a hide button to hide the majority of the error output and remove button to remove the error display altogether.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> mysql_error_wrapper<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$q</span>, <span style="color: #0000ff;">$sql</span>=<span style="color: #ff0000;">''</span>, <span style="color: #0000ff;">$line</span>=<span style="color: #ff0000;">''</span>, <span style="color: #0000ff;">$file</span>=<span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'REMOTE_ADDR'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">'YOUR LOCAL IP'</span><span style="color: #66cc66;">&#41;</span>&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&lt;script type=&quot;text/javascript&quot; language=&quot;JavaScript&quot;&gt;&lt;!--<br />
function showhide(d) {<br />
&nbsp; &nbsp; if(document.getElementById(d).style.display == &quot;none&quot;) { <br />
&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(d).style.display = &quot;block&quot;; <br />
&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('shde').innerHTML = &quot;hide&quot;;<br />
&nbsp; &nbsp; } &nbsp; else {<br />
&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(d).style.display = &quot;none&quot;; <br />
&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('shde').innerHTML = &quot;show&quot;; &nbsp; &nbsp; <br />
&nbsp; &nbsp; }<br />
}<br />
<br />
function remove() &nbsp; {<br />
&nbsp; &nbsp; document.getElementById('errorblock').style.display = &quot;none&quot;;<br />
}<br />
//--&gt;&lt;/script&gt;<br />
<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;div id=&quot;errorblock&quot; style=&quot;display:block;position:absolute;top:10px;left:10%;width:80%;background:#efefef;text-align:left;border:1px solid #CD0000;padding:10px;&quot;&gt;'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;a href=&quot;#&quot; onclick=&quot;showhide(<span style="color: #000099; font-weight: bold;">\'</span>sqlerror<span style="color: #000099; font-weight: bold;">\'</span>)&quot;&gt;&lt;span id=&quot;shde&quot;&gt;hide&lt;/span&gt;&lt;/a&gt; - &lt;a href=&quot;#&quot; onclick=&quot;remove();&quot;&gt;remove&lt;/a&gt;'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;h3 style=&quot;color:#CD0000;&quot;&gt;MySQL error: '</span>.<a href="http://www.php.net/mysql_errno"><span style="color: #000066;">mysql_errno</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">' &lt;small style=&quot;font-weight:normal;&quot;&gt;Line: #'</span>.<span style="color: #0000ff;">$line</span>.<span style="color: #ff0000;">' '</span>.<a href="http://www.php.net/basename"><span style="color: #000066;">basename</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">'&lt;/small&gt;&lt;/h3&gt;'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;pre id=&quot;sqlerror&quot; style=&quot;height:250px;overflow:auto;padding:10px;&quot;&gt;'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <a href="http://www.php.net/wordwrap"><span style="color: #000066;">wordwrap</span></a><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #000066;">mysql_error</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sql</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>&nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;h4&gt;SQL&lt;/h4&gt;'</span>.<a href="http://www.php.net/wordwrap"><span style="color: #000066;">wordwrap</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$line</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;h4&gt;Line&lt;/h4&gt;'</span>.<span style="color: #0000ff;">$line</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$file</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;h4&gt;File&lt;/h4&gt;'</span>.<span style="color: #0000ff;">$file</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;/pre&gt;'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;/div&gt;'</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>Usage example:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">&quot;YOUR SQL STATEMENT&quot;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">$q</span> = <a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sql</span>, <span style="color: #0000ff;">$db</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #0000ff;">$q</span><span style="color: #66cc66;">&#41;</span> mysql_error_wrapper<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$q</span>, <span style="color: #0000ff;">$sql</span>, <span style="color: #000000; font-weight: bold;">__LINE__</span>, <span style="color: #000000; font-weight: bold;">__FILE__</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
 <img src="http://damonparker.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=634" width="1" height="1" style="display: none;" /><img src="http://damonparker.org/?ak_action=api_record_view&id=634&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://damonparker.org/blog/2011/02/07/mysql-error-function-wrapper-for-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Quickly Copy MySQL Database to Remote Host</title>
		<link>http://damonparker.org/blog/2007/08/31/quickly-copy-mysql-database-to-remote-host/</link>
		<comments>http://damonparker.org/blog/2007/08/31/quickly-copy-mysql-database-to-remote-host/#comments</comments>
		<pubDate>Fri, 31 Aug 2007 16:29:50 +0000</pubDate>
		<dc:creator>damonp</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://damonparker.org/blog/2007/08/31/quickly-copy-mysql-database-to-remote-host/</guid>
		<description><![CDATA[Copy an entire database: mysqldump LOCAL_DBNAME &#124; ssh USER@REMOTE_HOST mysql -p REMOTE_DBNAME Copy a single table: mysqldump LOCAL_DBNAME LOCAL_TABLE &#124; ssh USER@REMOTE_HOST -p REMOTE_DBNAME REMOTE_TABLE This shortcut only works if you can access the local DB without a password. If you have to login to both local and remote MySQL servers, the MySQL password prompts [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Copy an entire database:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysqldump LOCAL_DBNAME <span style="color: #66cc66;">|</span> ssh USER<span style="color: #66cc66;">@</span>REMOTE_HOST mysql -p REMOTE_DBNAME</div></div>
<p>Copy a single table:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysqldump LOCAL_DBNAME LOCAL_TABLE <span style="color: #66cc66;">|</span> ssh USER<span style="color: #66cc66;">@</span>REMOTE_HOST -p REMOTE_DBNAME REMOTE_TABLE</div></div>
<p>This shortcut only works if you can access the local DB without a password.  If you have to login to both local and remote MySQL servers, the MySQL password prompts get mashed together.  You could specify the password on the command line like</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-pPASSWORD</div></div>
<p>but your shell may keep the password in its history so that anyone with access to your account could pick through your shell history and retrieve the password.  Using the MySQL password prompt doesn&#8217;t do this.</p>
 <img src="http://damonparker.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=377" width="1" height="1" style="display: none;" /><img src="http://damonparker.org/?ak_action=api_record_view&id=377&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://damonparker.org/blog/2007/08/31/quickly-copy-mysql-database-to-remote-host/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Data Manipulation &#8211; Splitting Surname and Name</title>
		<link>http://damonparker.org/blog/2007/06/21/data-manipulation-splitting-names/</link>
		<comments>http://damonparker.org/blog/2007/06/21/data-manipulation-splitting-names/#comments</comments>
		<pubDate>Thu, 21 Jun 2007 13:04:55 +0000</pubDate>
		<dc:creator>damonp</dc:creator>
				<category><![CDATA[Data Architecture]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://damonparker.org/blog/2007/06/21/data-manipulation-spitting-up-names/</guid>
		<description><![CDATA[For a new client project I was provided with database of users consisting of names, email addresses and managers. CREATE TABLE `users` &#40; &#160; `name` varchar&#40;255&#41; NOT NULL, &#160; `email` varchar&#40;255&#41; NOT NULL, &#160; `manager` varchar&#40;255&#41; NOT NULL, &#160; `status` varchar&#40;255&#41; NOT NULL &#41; The name column contained both first and last names formatted last [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>For a new client project I was provided with database of users consisting of names, email addresses and managers.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`users`</span> <span style="color: #66cc66;">&#40;</span><br />
&nbsp; <span style="color: #ff0000;">`name`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,<br />
&nbsp; <span style="color: #ff0000;">`email`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,<br />
&nbsp; <span style="color: #ff0000;">`manager`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,<br />
&nbsp; <span style="color: #ff0000;">`status`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><br />
<span style="color: #66cc66;">&#41;</span></div></div>
<p>The name column contained both first and last names formatted last name first like:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">lastname, fistname<br />
Parker, Damon</div></div>
<p>MySQL&#8217;s <a href="<br />
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html" rel="external">SUBSTRING_INDEX()</a> and <a href="<br />
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html" rel="external">TRIM()</a> functions allow us to make quick work of this data manipulation.  Using the query below and a new table similar to the first except with separate fields for first and last name we can copy the data to the new table and split the names in the process.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> users_clean <span style="color: #66cc66;">&#40;</span>first_name, last_name, email, manager, <span style="color: #993333; font-weight: bold;">STATUS</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> TRIM<span style="color: #66cc66;">&#40;</span>SUBSTRING_INDEX<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">`name`</span> , <span style="color: #ff0000;">','</span>, <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> first_name, TRIM<span style="color: #66cc66;">&#40;</span>SUBSTRING_INDEX<span style="color: #66cc66;">&#40;</span> SUBSTRING_INDEX<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">`name`</span> , <span style="color: #ff0000;">','</span>, <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#41;</span> , <span style="color: #ff0000;">','</span>, -<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> last_name, email, <span style="color: #993333; font-weight: bold;">STATUS</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`users`</span></div></div>
 <img src="http://damonparker.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=361" width="1" height="1" style="display: none;" /><img src="http://damonparker.org/?ak_action=api_record_view&id=361&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://damonparker.org/blog/2007/06/21/data-manipulation-splitting-names/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding Human Readable Date to MySQL Query</title>
		<link>http://damonparker.org/blog/2007/05/15/adding-human-readable-date-to-mysql-query/</link>
		<comments>http://damonparker.org/blog/2007/05/15/adding-human-readable-date-to-mysql-query/#comments</comments>
		<pubDate>Tue, 15 May 2007 21:43:40 +0000</pubDate>
		<dc:creator>damonp</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://damonparker.org/blog/2007/05/15/adding-human-readable-date-to-mysql-query/</guid>
		<description><![CDATA[SELECT *, FROM_UNIXTIME&#40; 't_stamp_column'&#41; AS date FROM 'tablename' For formatting the date see MySQL&#8217;s date functions information.]]></description>
			<content:encoded><![CDATA[<p></p><div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> *, FROM_UNIXTIME<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'t_stamp_column'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> date <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'tablename'</span></div></div>
<p>For formatting the date see <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html" rel="external">MySQL&#8217;s date functions</a> information.</p>
 <img src="http://damonparker.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=355" width="1" height="1" style="display: none;" /><img src="http://damonparker.org/?ak_action=api_record_view&id=355&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://damonparker.org/blog/2007/05/15/adding-human-readable-date-to-mysql-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easy MySQL Sorting Mistake</title>
		<link>http://damonparker.org/blog/2007/04/05/easy-mysql-sorting-mistake/</link>
		<comments>http://damonparker.org/blog/2007/04/05/easy-mysql-sorting-mistake/#comments</comments>
		<pubDate>Thu, 05 Apr 2007 17:00:57 +0000</pubDate>
		<dc:creator>damonp</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://damonparker.org/blog/2007/04/05/easy-mysql-sorting-mistake/</guid>
		<description><![CDATA[If you have a compound ORDER BY clause with a direction (ASC or DESC), make sure to specify the directions of all columns. Correct: ORDER BY col1 DESC, col2 DESC Wrong: ORDER BY col1, col2 DESC The latter forces a filesort.]]></description>
			<content:encoded><![CDATA[<p></p><p>If you have a compound ORDER BY clause with a direction (ASC or DESC), make sure to specify the directions of all columns.</p>
<p>Correct:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> col1 <span style="color: #993333; font-weight: bold;">DESC</span>, col2 <span style="color: #993333; font-weight: bold;">DESC</span></div></div>
<p>Wrong:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> col1, col2 <span style="color: #993333; font-weight: bold;">DESC</span></div></div>
<p>The latter forces a filesort.</p>
 <img src="http://damonparker.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=312" width="1" height="1" style="display: none;" /><img src="http://damonparker.org/?ak_action=api_record_view&id=312&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://damonparker.org/blog/2007/04/05/easy-mysql-sorting-mistake/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick and Dirty MySQL Backup Script</title>
		<link>http://damonparker.org/blog/2007/03/14/quick-and-dirty-mysql-backup-script/</link>
		<comments>http://damonparker.org/blog/2007/03/14/quick-and-dirty-mysql-backup-script/#comments</comments>
		<pubDate>Wed, 14 Mar 2007 10:22:53 +0000</pubDate>
		<dc:creator>damonp</dc:creator>
				<category><![CDATA[Sites]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SysAdmin]]></category>

		<guid isPermaLink="false">http://damonparker.org/blog/2007/03/14/quick-and-dirty-mysql-backup-script/</guid>
		<description><![CDATA[I previously posted a Quick and Dirty MySQL Backup snippet that has gotten a bit of traffic. It works well in a pinch, but it doesn&#8217;t have any features. I use it primarily as a safety backup when performing maintenance on a MySQL server. Several of my clients have requested a script that they can [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I previously posted a <a href="http://damonparker.org/blog/2006/08/04/quick-and-dirty-mysql-backup/">Quick and Dirty MySQL Backup snippet</a> that has gotten a bit of traffic.  It works well in a pinch, but it doesn&#8217;t have any features.  I use it primarily as a safety backup when performing maintenance on a MySQL server.  </p>
<p>Several of my clients have requested a script that they can use to do on demand one-off backups.   I rolled the snippet up into a very quick-and-dirty MySQL backup.  </p>
<h4>Download <a href="http://damonparker.org/files/mysql_quick_back.sh">mysql_quick_back.sh</a></h4>
<h4>Installation</h4>
<ol>
<li>Copy mysql_quick_back.sh to a directory in your path (<em>~/bin/</em> for me).</li>
<li>Set execute bits with
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">chmod <span style="color: #cc66cc;">755</span> mysql_quick_back.sh</div></div>
</li>
<li>Set MySQL user/pass, directory to save backups to and notification email in script configuration
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># START configure </span><br />
<span style="color: #808080; font-style: italic;"># MySQL user, needs SELECT privileges </span><br />
<span style="color: #808080; font-style: italic;"># Create a new user with these permissions! &nbsp;More secure than using a user</span><br />
<span style="color: #808080; font-style: italic;"># with full permissions</span><br />
<span style="color: #0000ff;">user=</span><br />
<br />
<span style="color: #808080; font-style: italic;"># MySQL password</span><br />
<span style="color: #0000ff;">pass=</span><br />
<br />
<span style="color: #808080; font-style: italic;"># admin email address</span><br />
<span style="color: #0000ff;">adminemail=</span><br />
<br />
<span style="color: #808080; font-style: italic;"># backup location without trailing slash</span><br />
<span style="color: #808080; font-style: italic;"># set to current directory</span><br />
<span style="color: #0000ff;">backuppath=</span>.<br />
<span style="color: #808080; font-style: italic;"># or set to your home directory</span><br />
<span style="color: #808080; font-style: italic;">#backuppath=/home/admin/db_backs</span></div></div>
</li>
</ol>
<h4>Usage</h4>
<p>Simply execute the script</p>
<h4>NOTE</h4>
<p>For a full MySQL backup application, check out the original <a href="http://worldcommunity.com/opensource/utilities/mysql_backup.html" rel="external">MySQL Backup</a> by Peter Falkenberg Brown.   MySQL Backup is robust enough to use as a daily backup.  I use it on most of the servers I manage.</p>
 <img src="http://damonparker.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=326" width="1" height="1" style="display: none;" /><img src="http://damonparker.org/?ak_action=api_record_view&id=326&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://damonparker.org/blog/2007/03/14/quick-and-dirty-mysql-backup-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ZenCart Manufacturers System Errors</title>
		<link>http://damonparker.org/blog/2007/01/12/zencart-manufacturers-system-errors/</link>
		<comments>http://damonparker.org/blog/2007/01/12/zencart-manufacturers-system-errors/#comments</comments>
		<pubDate>Fri, 12 Jan 2007 17:46:54 +0000</pubDate>
		<dc:creator>damonp</dc:creator>
				<category><![CDATA[Ecommerce]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Broken]]></category>

		<guid isPermaLink="false">http://damonparker.org/blog/2007/01/12/zencart-manufacturers-system-errors/</guid>
		<description><![CDATA[In ZenCart versions immediately prior to 1.3.6 using many of the public side manufacturers functions may produce this error: Warning: constant&#40;&#41;: Couldn't find constant in &#160;.../includes/init_includes/init_add_crumbs.php on line 45 The error is caused by a missing database column that was added in the official 1.3.6 release. If upgrade is not immediately possible the following SQL [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In ZenCart versions immediately prior to 1.3.6 using many of the public side manufacturers functions may produce this error:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Warning: <a href="http://www.php.net/constant"><span style="color: #000066;">constant</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>: Couldn<span style="color: #ff0000;">'t find constant in &nbsp;.../includes/init_includes/init_add_crumbs.php on line 45</span></div></div>
<p>The error is caused by a missing database column that was added in the official 1.3.6 release.  If upgrade is not immediately possible the following SQL snippet will correct the problem in most cases:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> get_terms_to_filter <span style="color: #993333; font-weight: bold;">ADD</span> get_term_table varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> get_terms_to_filter <span style="color: #993333; font-weight: bold;">ADD</span> get_term_name_field varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">UPDATE</span> get_terms_to_filter <span style="color: #993333; font-weight: bold;">SET</span> get_term_table <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'TABLE_MANUFACTURERS'</span>, get_term_name_field <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'manufacturers_name'</span> <span style="color: #993333; font-weight: bold;">WHERE</span> get_term_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'manufacturers_id'</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">UPDATE</span> get_terms_to_filter <span style="color: #993333; font-weight: bold;">SET</span> get_term_table <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'TABLE_MUSIC_GENRE'</span>, get_term_name_field <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'music_genre_name'</span> <span style="color: #993333; font-weight: bold;">WHERE</span> get_term_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'music_genre_id'</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">UPDATE</span> get_terms_to_filter <span style="color: #993333; font-weight: bold;">SET</span> get_term_table <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'TABLE_RECORD_COMPANY'</span>, get_term_name_field <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'record_company_name'</span> <span style="color: #993333; font-weight: bold;">WHERE</span> get_term_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'record_company_id'</span>;</div></div>
<p>If you are using other product types, some additional UPDATE statements will be required for your new product types.</p>
<p>I found this solution after some searching on the ZenCart <a href="http://www.zen-cart.com/forum/showthread.php?t=50738" rel="external">forums</a>.</p>
 <img src="http://damonparker.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=293" width="1" height="1" style="display: none;" /><img src="http://damonparker.org/?ak_action=api_record_view&id=293&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://damonparker.org/blog/2007/01/12/zencart-manufacturers-system-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Slow Query Logging</title>
		<link>http://damonparker.org/blog/2006/12/05/mysql-slow-query-logging/</link>
		<comments>http://damonparker.org/blog/2006/12/05/mysql-slow-query-logging/#comments</comments>
		<pubDate>Tue, 05 Dec 2006 23:52:27 +0000</pubDate>
		<dc:creator>damonp</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SysAdmin]]></category>

		<guid isPermaLink="false">http://damonparker.org/blog/2006/12/05/mysql-slow-query-logging/</guid>
		<description><![CDATA[Add to /etc/my.cnf under [mysqld] stanza MySQL5 log_slow_queries &#160;= /var/log/mysqld-slow.log long_query_time &#160; = 1 MySQL3, MySQL4 log-slow-queries=/var/log/mysqld-slow.log set-variable &#160; &#160;= long_query_time=1]]></description>
			<content:encoded><![CDATA[<p></p><p>Add to /etc/my.cnf under [mysqld] stanza</p>
<p>MySQL5</p>
<div class="codecolorer-container apache default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">log_slow_queries &nbsp;= /var/log/mysqld-slow.log<br />
long_query_time &nbsp; = <span style="color: #ff0000;">1</span></div></div>
<p>MySQL3, MySQL4</p>
<div class="codecolorer-container apache default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">log-slow-queries=/var/log/mysqld-slow.log<br />
set-variable &nbsp; &nbsp;= long_query_time=<span style="color: #ff0000;">1</span></div></div>
 <img src="http://damonparker.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=286" width="1" height="1" style="display: none;" /><img src="http://damonparker.org/?ak_action=api_record_view&id=286&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://damonparker.org/blog/2006/12/05/mysql-slow-query-logging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ZenCart Orders Total Weight Query</title>
		<link>http://damonparker.org/blog/2006/11/29/zencart-orders-total-weight-query/</link>
		<comments>http://damonparker.org/blog/2006/11/29/zencart-orders-total-weight-query/#comments</comments>
		<pubDate>Wed, 29 Nov 2006 19:00:05 +0000</pubDate>
		<dc:creator>damonp</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://damonparker.org/blog/2006/11/29/zencart-orders-total-weight-query/</guid>
		<description><![CDATA[SELECT op.products_quantity * SUM&#40; products_weight &#41; FROM products p JOIN orders_products op WHERE p.products_id IN &#40; SELECT products_id FROM orders_products WHERE orders_id = 'ORDER_ID' &#41; AND op.orders_id = 'ORDER_ID' Replace ORDER_ID with the orders_id in question. Requires MySQL 4.1 or better for subselect support]]></description>
			<content:encoded><![CDATA[<p></p><div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> op.products_quantity * SUM<span style="color: #66cc66;">&#40;</span> products_weight <span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> products p<br />
<span style="color: #993333; font-weight: bold;">JOIN</span> orders_products op<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> p.products_id<br />
<span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> products_id<br />
<span style="color: #993333; font-weight: bold;">FROM</span> orders_products<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> orders_id <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'ORDER_ID'</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> op.orders_id <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'ORDER_ID'</span></div></div>
<p>Replace <em>ORDER_ID</em> with the orders_id in question.</p>
<p>Requires <a href="http://mysql.com" rel="external">MySQL</a> 4.1 or better for <a href="http://damonparker.org/blog/2005/10/07/using-subselect-queries-in-mysql/">subselect support</a></p>
 <img src="http://damonparker.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=283" width="1" height="1" style="display: none;" /><img src="http://damonparker.org/?ak_action=api_record_view&id=283&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://damonparker.org/blog/2006/11/29/zencart-orders-total-weight-query/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL Database Performance &#8211; ZenCart Sales Report</title>
		<link>http://damonparker.org/blog/2006/10/23/mysql-database-performance-zencart-sales-report/</link>
		<comments>http://damonparker.org/blog/2006/10/23/mysql-database-performance-zencart-sales-report/#comments</comments>
		<pubDate>Mon, 23 Oct 2006 19:13:36 +0000</pubDate>
		<dc:creator>damonp</dc:creator>
				<category><![CDATA[Ecommerce]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://damonparker.org/blog/2006/10/23/mysql-database-performance-zencart-sales-report/</guid>
		<description><![CDATA[One of my client&#8217;s ZenCart installs was loading the server down so much to generate sales reports with the Sales Report v1 contribution that the public side was noticeably less responsive for the couple of minutes it took to build the report. The site has been running for over three years and contains 9k orders, [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>One of my client&#8217;s ZenCart installs was loading the server down so much to generate sales reports with the <a href="http://www.zen-cart.com/index.php?main_page=product_contrib_info&#038;cPath=40_41&#038;products_id=83" rel="external">Sales Report v1</a> contribution that the public side was noticeably less responsive for the couple of minutes it took to build the report.  The site has been running for over three years and contains 9k orders, so there is a lot of data to weed through even for a weekly report.  A quick look at the <a href="http://dev.mysql.com/doc/refman/5.0/en/slow-query-log.html" rel="external">slow query log</a> listed the following query as the culprit:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> count<span style="color: #66cc66;">&#40;</span> op.products_id <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> attr_cnt, o.orders_id, opa.orders_products_id, opa.products_options, opa.products_options_values, opa.options_values_price, opa.price_prefix<br />
<span style="color: #993333; font-weight: bold;">FROM</span> orders_products_attributes opa, orders o, orders_products op<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> o.orders_id <span style="color: #66cc66;">=</span> opa.orders_id<br />
<span style="color: #993333; font-weight: bold;">AND</span> op.orders_products_id <span style="color: #66cc66;">=</span> opa.orders_products_id<br />
<span style="color: #993333; font-weight: bold;">AND</span> o.date_purchased <span style="color: #66cc66;">&gt;=</span> <span style="color: #ff0000;">'2006-10-18T00:00:00'</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> o.date_purchased <span style="color: #66cc66;">&lt;</span> <span style="color: #ff0000;">'2006-10-19T00:00:00'</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> op.products_id <span style="color: #66cc66;">=</span><span style="color: #cc66cc;">179</span><br />
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> products_options_values<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> orders_products_id;</div></div>
<p>Running this query through <a href="http://dev.mysql.com/doc/refman/5.0/en/explain.html" rel="external">EXPLAIN</a> <a href="http://damonparker.org/html/explain_sales_report.html#one">shows 20978 rows examined</a>.  In this case, thats 20978 rows per day included in the sales report.</p>
<p>The main problem with this query is the ORDER BY clause which cannot utilize an exisiting index.  Adding the following index:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">'orders_products_attributes'</span> <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">INDEX</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'orders_products_id'</span> <span style="color: #66cc66;">&#41;</span></div></div>
<p>limits the rows examined for each query to 68 (<a href="http://damonparker.org/html/explain_sales_report.html#two">results</a>).  </p>
<p>This simple change allows the sales report to load in seconds as opposed to minutes with no noticeable lag on the public side.</p>
<p>The create table SQL for the current version of ZenCart (v1.3.5) contains the following index in orders_products_attributes:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">...<br />
<span style="color: #993333; font-weight: bold;">KEY</span> idx_orders_id_prod_id_zen <span style="color: #66cc66;">&#40;</span>orders_id,orders_products_id<span style="color: #66cc66;">&#41;</span><br />
...</div></div>
<p>This index is used in the query in question.  In my tests it was only slightly slower than the single column index above.  EXPLAIN showed about 270 rows examined with the two column index on my dataset while  the single column index only examined 68 rows.</p>
 <img src="http://damonparker.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=275" width="1" height="1" style="display: none;" /><img src="http://damonparker.org/?ak_action=api_record_view&id=275&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://damonparker.org/blog/2006/10/23/mysql-database-performance-zencart-sales-report/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using xcache
Database Caching 72/91 queries in 0.012 seconds using xcache
Content Delivery Network via cdn.damonparker.org/q9i5a4w8/cds/dp

Served from: damonparker.org @ 2012-02-04 14:03:58 -->
