<?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>Saiweb &#187; query</title>
	<atom:link href="http://www.saiweb.co.uk/tag/query/feed" rel="self" type="application/rss+xml" />
	<link>http://www.saiweb.co.uk</link>
	<description>Ramblings of a Sys admin</description>
	<lastBuildDate>Mon, 06 Feb 2012 14:57:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>mySQL &gt;= 5.0.37 community profiling SQL queries.</title>
		<link>http://www.saiweb.co.uk/mysql/mysql-5-0-37-community-profiling-sql-queries</link>
		<comments>http://www.saiweb.co.uk/mysql/mysql-5-0-37-community-profiling-sql-queries#comments</comments>
		<pubDate>Thu, 15 Jul 2010 19:30:06 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[mySQL]]></category>
		<category><![CDATA[5.0.37]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[slow]]></category>
		<category><![CDATA[slow query]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/?p=885</guid>
		<description><![CDATA[Whilst there indeed seems to be a veritable plethora of SQL profiling / benchmarking tools, most of them with insane commercial license fees (&#62;= $400 per annum on most) I have found it intriguing that as of mySQL community edition &#62;= 5.0.37 mySQL offers an inbuilt method for profiling SQL queries, as can be see [...]]]></description>
			<content:encoded><![CDATA[<p>Whilst there indeed seems to be a veritable plethora of SQL profiling / benchmarking tools, most of them with insane commercial license fees (&gt;= $400 per annum on most)</p>
<p>I have found it intriguing that as of mySQL community edition &gt;= 5.0.37 mySQL offers an inbuilt method for profiling SQL queries, as can be see <a href="http://dev.mysql.com/doc/refman/5.0/en/show-profiles.html">here</a> the downside is that this is session based, meaning it can only provide profiling information for the current connection, almost useless for trying to profile a running web app (that is without code modification to set profiling and harvest the data).</p>
<p>However it can be useful if you have a known slow query.</p>
<p>So lets work on the basis that we have a known slow SQL query we&#8217;d like profiling information for,</p>
<p>check to see if profiling is enabled:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><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> &nbsp;@@profiling;</div></td></tr></tbody></table></div>
<p>The returned value is generally 0 so lets enable it.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><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;">SET</span> profiling_history_size<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">100</span>;<br />
<span style="color: #993333; font-weight: bold;">SET</span> profiling<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>;</div></td></tr></tbody></table></div>
<p>This tells mySQL to retain the profile of 100 queries in memory, and to enable profiling.</p>
<p>Now at this point this can also be used to diagnose slow loading datases, simply</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><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;">USE</span> <span style="color: #66cc66;">&lt;</span>dbname<span style="color: #66cc66;">&gt;</span>;<br />
<span style="color: #993333; font-weight: bold;">SHOW</span> profiles;<br />
<span style="color: #993333; font-weight: bold;">SHOW</span> profile <span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #cc66cc;">1</span>;</div></td></tr></tbody></table></div>
<p>Upon running the above you will now be using your database and will see an output similar to</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">+----------+------------+-------------------+<br />
| Query_ID | Duration &nbsp; | Query &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
+----------+------------+-------------------+<br />
| &nbsp; &nbsp; &nbsp; &nbsp;1 | 0.00011400 | SELECT DATABASE() |<br />
| &nbsp; &nbsp; &nbsp; &nbsp;2 | 0.00048900 | show databases &nbsp; &nbsp;|<br />
| &nbsp; &nbsp; &nbsp; &nbsp;3 | 0.00026600 | show tables &nbsp; &nbsp; &nbsp; |<br />
+----------+------------+-------------------+</div></td></tr></tbody></table></div>
<p>Followed by</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">+----------------------+----------+<br />
| Status &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Duration |<br />
+----------------------+----------+<br />
| starting &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 0.000053 |<br />
| checking permissions | 0.000004 |<br />
| Opening tables &nbsp; &nbsp; &nbsp; | 0.000009 |<br />
| init &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 0.000011 |<br />
| optimizing &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 0.000004 |<br />
| executing &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000017 |<br />
| end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000003 |<br />
| end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000002 |<br />
| query end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000002 |<br />
| freeing items &nbsp; &nbsp; &nbsp; &nbsp;| 0.000005 |<br />
| logging slow query &nbsp; | 0.000002 |<br />
| cleaning up &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000002 |<br />
+----------------------+----------+</div></td></tr></tbody></table></div>
<p>In my case here nothing really eventful, lets assume for the moment you are using a wordpress database, and you have numerous posts</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><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> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> wp_posts <span style="color: #993333; font-weight: bold;">WHERE</span> ID <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">100</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span>ID<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> wp_posts <span style="color: #993333; font-weight: bold;">WHERE</span> ID <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">100</span></div></td></tr></tbody></table></div>
<p>in my case I got the following results:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0.00072600 | select count(*) from wp_posts where ID &amp;gt; 100<br />
0.00069900 | select count(ID) from wp_posts where ID &amp;gt; 100</div></td></tr></tbody></table></div>
<p>a simple demonstration showing the difference between a count() on an indexed field vs *, in this case the saving is ~4%.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><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;">SHOW</span> profiles;<br />
<span style="color: #993333; font-weight: bold;">SHOW</span> profile <span style="color: #993333; font-weight: bold;">FOR</span> query <span style="color: #66cc66;">&lt;</span>n<span style="color: #66cc66;">&gt;</span>;</div></td></tr></tbody></table></div>
<p>Will give you an output similar to:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">+--------------------+----------+<br />
| Status &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Duration |<br />
+--------------------+----------+<br />
| starting &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 0.000079 |<br />
| Opening tables &nbsp; &nbsp; | 0.000014 |<br />
| System lock &nbsp; &nbsp; &nbsp; &nbsp;| 0.000005 |<br />
| Table lock &nbsp; &nbsp; &nbsp; &nbsp; | 0.000008 |<br />
| init &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 0.000025 |<br />
| optimizing &nbsp; &nbsp; &nbsp; &nbsp; | 0.000012 |<br />
| statistics &nbsp; &nbsp; &nbsp; &nbsp; | 0.000049 |<br />
| preparing &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000012 |<br />
| executing &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000006 |<br />
| Sending data &nbsp; &nbsp; &nbsp; | 0.000461 |<br />
| end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000004 |<br />
| end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000003 |<br />
| query end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000003 |<br />
| freeing items &nbsp; &nbsp; &nbsp;| 0.000007 |<br />
| closing tables &nbsp; &nbsp; | 0.000005 |<br />
| logging slow query | 0.000003 |<br />
| cleaning up &nbsp; &nbsp; &nbsp; &nbsp;| 0.000003 |<br />
+--------------------+----------+<br />
17 rows in set (0.00 sec)</div></td></tr></tbody></table></div>
<p>this is very similar to a <a href="http://en.wikipedia.org/wiki/Strace">stack trace</a> you may run on a problematic script, or <a href="http://www.xdebug.org">xdebug</a> + <a href="http://code.google.com/p/webgrind/">webgrind</a>, and will gain futher insight into your SQL should <a href="http://dev.mysql.com/doc/refman/5.0/en/explain.html">EXPLAIN</a> no give you enough of an insight.</p>
<p>I&#8217;ll post more information on this as I get time to work with it more, this is still knew to me, and aside from knowing how to use it I know relatively little about this profiling functionality, please feel free to post references / examples in the comments.</p>
<p>Cheers</p>
<p>Buzz<span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/mysql/mysql-5-0-37-community-profiling-sql-queries" data-text="mySQL >= 5.0.37 community profiling SQL queries.&#8221; href=&#8221;http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fmysql%2Fmysql-5-0-37-community-profiling-sql-queries&#038;text=mySQL%20%3E%3D%205.0.37%20community%20profiling%20SQL%20queries.&#8221; >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/mysql/mysql-5-0-37-community-profiling-sql-queries/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP A2S Class (Valve Server Query Protocol)</title>
		<link>http://www.saiweb.co.uk/hacking/php-a2s-class-valve-server-query-protocol</link>
		<comments>http://www.saiweb.co.uk/hacking/php-a2s-class-valve-server-query-protocol#comments</comments>
		<pubDate>Thu, 28 Feb 2008 18:23:34 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[a2s]]></category>
		<category><![CDATA[php source class]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[valve]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/hacking/php-a2s-class-valve-server-query-protocol</guid>
		<description><![CDATA[I was reminded today on MSN that I had in fact written (albeit a long time ago) a class for querying the A2S protocol using PHP sockets. So I have decided to release the source code for this under the MIT licence. Bear in mind this was written when the Source SDK was leaked, so [...]]]></description>
			<content:encoded><![CDATA[<p>I was reminded today on MSN that I had in fact written (albeit a long time ago) a class for querying the A2S protocol using PHP sockets.</p>
<p>So I have decided to release the source code for this under the MIT licence.</p>
<p>Bear in mind this was written when the Source SDK was leaked, so it is for PHP 4 (Which reaches &#8220;end of life&#8221; 08/08/2008 ). And I havent re-tested wheter it works in a LONG long long &#8230; etc time.</p>
<p>However  With the exception of the addition of RCON functionality (which should of been put in originally Valve!), the A2S protocol has not changed</p>
<p>Anyway check the attached files, the licence is included at the top of the file.</p>
<p><a href="http://cdn.saiweb.co.uk/wp-content/uploads/2008/02/sourceclass.rar" title="PHP 4 A2S class (Valve Server Query)">PHP 4 A2S class (Valve Server Query)</a></p>
<p><a href="http://digg.com/programming/PHP_A2S_Class_Valve_Server_Query_Protocol">Digg this</a><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/hacking/php-a2s-class-valve-server-query-protocol" data-text="PHP A2S Class (Valve Server Query Protocol)" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fhacking%2Fphp-a2s-class-valve-server-query-protocol&#038;text=PHP%20A2S%20Class%20%28Valve%20Server%20Query%20Protocol%29" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/hacking/php-a2s-class-valve-server-query-protocol/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 apc
Database Caching 6/20 queries in 0.020 seconds using apc
Object Caching 882/914 objects using apc
Content Delivery Network via Rackspace Cloud Files: cdn.saiweb.co.uk

Served from: www.saiweb.co.uk @ 2012-02-07 16:52:28 -->
