<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	>
<channel>
	<title>Comments on: MySQL Optimisation</title>
	<atom:link href="http://www.distantparts.com/2007/05/31/157/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.distantparts.com/2007/05/31/157/</link>
	<description>Random thoughts from a far off place</description>
	<pubDate>Tue, 06 Jan 2009 22:41:49 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Steven</title>
		<link>http://www.distantparts.com/2007/05/31/157/comment-page-1/#comment-9308</link>
		<dc:creator>Steven</dc:creator>
		<pubDate>Sun, 17 Jun 2007 09:17:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.distantparts.com/2007/05/31/157/#comment-9308</guid>
		<description>&lt;p&gt;Hi Gary,&lt;/p&gt;
&lt;p&gt;I've been using explain to see what indices are being used. It took me a while to get my head around the results displayed by explain, but it is very useful information.&lt;/p&gt;
&lt;p&gt;mysql_fetch_array is a superset of mysql_fetch_assoc, according the PHP manual. For that reason, I'd gone with mysql_fetch_array over mysql_fetch_assoc.&lt;/p&gt;
&lt;p&gt;Interestingly, I just found this comment that suggests that there is &lt;a href="http://uk2.php.net/manual/en/function.mysql-fetch-array.php#22361" rel="nofollow"&gt;a significant performance difference between mysql_fetch_assoc and mysql_fetch_array&lt;/a&gt;. I might have to rewrite some of my mysql_fetch_array calls. It's not worth it for most pages, but I do have some pages where several hundred rows are retrieved, and the difference there could be worth it.&lt;/p&gt;
&lt;p&gt;Anyway, both mysql_fetch_assoc and mysql_fetch_array are going to be far better than using my old friend mysql_result&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hi Gary,</p>
<p>I&#8217;ve been using explain to see what indices are being used. It took me a while to get my head around the results displayed by explain, but it is very useful information.</p>
<p>mysql_fetch_array is a superset of mysql_fetch_assoc, according the PHP manual. For that reason, I&#8217;d gone with mysql_fetch_array over mysql_fetch_assoc.</p>
<p>Interestingly, I just found this comment that suggests that there is <a href="http://uk2.php.net/manual/en/function.mysql-fetch-array.php#22361" rel="nofollow">a significant performance difference between mysql_fetch_assoc and mysql_fetch_array</a>. I might have to rewrite some of my mysql_fetch_array calls. It&#8217;s not worth it for most pages, but I do have some pages where several hundred rows are retrieved, and the difference there could be worth it.</p>
<p>Anyway, both mysql_fetch_assoc and mysql_fetch_array are going to be far better than using my old friend mysql_result</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gary</title>
		<link>http://www.distantparts.com/2007/05/31/157/comment-page-1/#comment-9293</link>
		<dc:creator>Gary</dc:creator>
		<pubDate>Sat, 16 Jun 2007 17:57:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.distantparts.com/2007/05/31/157/#comment-9293</guid>
		<description>... forgot to say, my preference is mysql_fetch_assoc, much better and cleaner to see the field names in the associative array.  Has anyone seen any speed comparisons for these functions?  I'm sure it doesn't actually make much difference as the most popular queries will be cached by mysql anyway, but I suppose it could make a difference on a heavily loaded system.</description>
		<content:encoded><![CDATA[<p>&#8230; forgot to say, my preference is mysql_fetch_assoc, much better and cleaner to see the field names in the associative array.  Has anyone seen any speed comparisons for these functions?  I&#8217;m sure it doesn&#8217;t actually make much difference as the most popular queries will be cached by mysql anyway, but I suppose it could make a difference on a heavily loaded system.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gary</title>
		<link>http://www.distantparts.com/2007/05/31/157/comment-page-1/#comment-9292</link>
		<dc:creator>Gary</dc:creator>
		<pubDate>Sat, 16 Jun 2007 17:55:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.distantparts.com/2007/05/31/157/#comment-9292</guid>
		<description>My process is to run all my queries through 'explain'  to make sure they are all at least using indexes, and checking as few rows as possible.

I did once find a colleague's statement with a subquery, where the subquery was checking about 5000 rows, and the main query was checking about 20,000 rows.  A quick rewrite with a correct join and some decent indexes and I got it down to checking less than 100 rows (and that query was called on every page, eek).</description>
		<content:encoded><![CDATA[<p>My process is to run all my queries through &#8216;explain&#8217;  to make sure they are all at least using indexes, and checking as few rows as possible.</p>
<p>I did once find a colleague&#8217;s statement with a subquery, where the subquery was checking about 5000 rows, and the main query was checking about 20,000 rows.  A quick rewrite with a correct join and some decent indexes and I got it down to checking less than 100 rows (and that query was called on every page, eek).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven</title>
		<link>http://www.distantparts.com/2007/05/31/157/comment-page-1/#comment-9042</link>
		<dc:creator>Steven</dc:creator>
		<pubDate>Thu, 07 Jun 2007 20:47:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.distantparts.com/2007/05/31/157/#comment-9042</guid>
		<description>I'm now doing things the way you do, but for a long time I was coding PHP circa 1999, which wasn't good. At least I got rid of the old default register globals many years ago :-)</description>
		<content:encoded><![CDATA[<p>I&#8217;m now doing things the way you do, but for a long time I was coding PHP circa 1999, which wasn&#8217;t good. At least I got rid of the old default register globals many years ago <img src='http://www.distantparts.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ichilton</title>
		<link>http://www.distantparts.com/2007/05/31/157/comment-page-1/#comment-9034</link>
		<dc:creator>ichilton</dc:creator>
		<pubDate>Thu, 07 Jun 2007 19:50:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.distantparts.com/2007/05/31/157/#comment-9034</guid>
		<description>Hi,

I always use while ($rsArray = mysql_fetch_array($qidQuery)) to loop through the result set, but i've never thought of mysql_num_rows as inefficient...

Ian</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I always use while ($rsArray = mysql_fetch_array($qidQuery)) to loop through the result set, but i&#8217;ve never thought of mysql_num_rows as inefficient&#8230;</p>
<p>Ian</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven</title>
		<link>http://www.distantparts.com/2007/05/31/157/comment-page-1/#comment-8970</link>
		<dc:creator>Steven</dc:creator>
		<pubDate>Thu, 07 Jun 2007 10:00:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.distantparts.com/2007/05/31/157/#comment-8970</guid>
		<description>Hi Ian,

Often you just need to process all the results of a query, without having to know beforehand how many rows you have. In that case, the mysql_num_rows is pointless, as mysql_fetch_array will automatically return false when there are no more results. I suspect that even with a counter, it's less overhead than asking MySQL for the number of rows, with all the associated communication overhead, unless your results set is very large.

Using mysql_result does a seek within a results set for every single field and row. And there's lots of communcations overhead. Whereas with mysql_fetch_array, it's just one row after another, with no need to jump back and forward in the results set.

The MySQL manual recommends avoiding mysql_result in favour of the row based alternatives (such mysql_fetch_array). I've also read similar recommendations elsewhere.

http://uk2.php.net/function.mysql-result

I'm using mysql_fetch_array, because I much prefer working with named fields, rather than numbered fields.

I haven't done any detailed tests, but things seem to be running significantly more quickly, although having made quite a few changes, it's difficult to be specific about which ones made the most difference. I suspect the change from mysql_result to mysql_fetch_array was more than important than removing a mysql_num_results calls.</description>
		<content:encoded><![CDATA[<p>Hi Ian,</p>
<p>Often you just need to process all the results of a query, without having to know beforehand how many rows you have. In that case, the mysql_num_rows is pointless, as mysql_fetch_array will automatically return false when there are no more results. I suspect that even with a counter, it&#8217;s less overhead than asking MySQL for the number of rows, with all the associated communication overhead, unless your results set is very large.</p>
<p>Using mysql_result does a seek within a results set for every single field and row. And there&#8217;s lots of communcations overhead. Whereas with mysql_fetch_array, it&#8217;s just one row after another, with no need to jump back and forward in the results set.</p>
<p>The MySQL manual recommends avoiding mysql_result in favour of the row based alternatives (such mysql_fetch_array). I&#8217;ve also read similar recommendations elsewhere.</p>
<p><a href="http://uk2.php.net/function.mysql-result" rel="nofollow">http://uk2.php.net/function.mysql-result</a></p>
<p>I&#8217;m using mysql_fetch_array, because I much prefer working with named fields, rather than numbered fields.</p>
<p>I haven&#8217;t done any detailed tests, but things seem to be running significantly more quickly, although having made quite a few changes, it&#8217;s difficult to be specific about which ones made the most difference. I suspect the change from mysql_result to mysql_fetch_array was more than important than removing a mysql_num_results calls.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian Chilton</title>
		<link>http://www.distantparts.com/2007/05/31/157/comment-page-1/#comment-8958</link>
		<dc:creator>Ian Chilton</dc:creator>
		<pubDate>Thu, 07 Jun 2007 07:26:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.distantparts.com/2007/05/31/157/#comment-8958</guid>
		<description>Hi,

Interesting! - Are you saying that a while loop on mysql_fetch_array with a counter in the middle is faster than mysql_num_rows?

I'm very surprised by that...

Did you read that somewhere or just test it?

Ian</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Interesting! - Are you saying that a while loop on mysql_fetch_array with a counter in the middle is faster than mysql_num_rows?</p>
<p>I&#8217;m very surprised by that&#8230;</p>
<p>Did you read that somewhere or just test it?</p>
<p>Ian</p>
]]></content:encoded>
	</item>
</channel>
</rss>
