<?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>bassmadrigal&#039;s blog &#187; Query count</title>
	<atom:link href="http://bassmadrigal.com/blog/tag/query-count/feed/" rel="self" type="application/rss+xml" />
	<link>http://bassmadrigal.com/blog</link>
	<description>Bringing out the geekiness in all of us</description>
	<lastBuildDate>Fri, 20 Feb 2009 06:01:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL Query Time and Counter</title>
		<link>http://bassmadrigal.com/blog/2008/11/mysql-query-time-and-counter/</link>
		<comments>http://bassmadrigal.com/blog/2008/11/mysql-query-time-and-counter/#comments</comments>
		<pubDate>Sun, 09 Nov 2008 23:22:06 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[page created in]]></category>
		<category><![CDATA[Query count]]></category>
		<category><![CDATA[Query time]]></category>

		<guid isPermaLink="false">http://bassmadrigal.com/blog/?p=27</guid>
		<description><![CDATA[It has been a little while since I made an entry, but there is good reason for that. I have been hard at work redesigning my Gallery layout for my picture galleries on My Air Force Life. The main thing I have been pushing for was integration into MySQL. There are obvious benefits of switching [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a little while since I made an entry, but there is good reason for that. I have been hard at work redesigning my Gallery layout for my picture galleries on <a href="http://myairforcelife.net" target="_blank">My Air Force Life</a>. The main thing I have been pushing for was integration into MySQL. There are obvious benefits of switching to that rather than just having static pages, but that is not the point of this entry.</p>
<p>The reason for this entry is to show how I did the little counter at the bottom of the pages. You know the ones&#8230;</p>
<p style="padding-left: 30px;">Page created in x.xxx seconds with X queries.</p>
<p>Well I was wanting to figure out how to get this to work on my site. Part of the reason, is I am using my laptop as a testbed, so I want to see how resource intensive some of my queries may be. This is my first time actually developing something with MySQL, rather than editing existing code, and I wasn&#8217;t sure that my queries were going to be all that resource friendly.</p>
<p>Well to start since I will be doing many different queries on many different pages, I felt that I needed to create a function in a separate file that I would just include on my other files. I called the file func.query_time.php</p>
<p>Within that file, I had two separate functions. One which will time the query, add it to the total time, and add the amount of queries. The second function is just to display the actual info at the bottom of the pages. I will go through these functions separately, so people understand how they work. But I will have everything all together at the bottom.</p>
<p>So to start out we need to declare the function and we are calling it query.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</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: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$querycount</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$querycount</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$totaltime</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></pre></div></div>

<p>All this does is recieve the query ($sql) and the querycount and totaltime amounts. And if the latter two were not passed when the function was called then it automatically sets them at zero. Next we need to get the time it takes to process the query.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usec</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sec</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$querytime_before</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$usec</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// run the query</span>
   <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usec</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sec</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$querytime_after</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$usec</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The first part grabs the time before the query was run and stores it in $querytime_before. Then we actually run the MySQL query, then we get the time again and store it in $querytime_after. Now we need to get the totals of everything.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #000088;">$querytime</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$querytime_after</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$querytime_before</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$totaltime</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$querytime</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000088;">$querycount</span><span style="color: #339933;">++;</span></pre></div></div>

<p>The first line take the time before and subtracts it from the time after which gives us the time it takes to make that query. Then we add the $querytime to $totaltime (the += is for adding another variable to the one it will be stored in ie. $totaltime = $totaltime + $querytime;). The last line is to add 1 to the querycount. The final part of this function is to return the info so you can use it on your regular page.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> <span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This returns the results back into the orginal file and closes out the function. Now onto the file itself and how you call the query.</p>
<p>Now the first time you call the function &#8220;query&#8221; there will be no variables set for $querycount or $totaltime, so you can just leave those out, but those variables will be returned. And since they are returned in an array there are a couple of ways to get them out. I prefer the list() function as it is simple and you can name the variables however you want.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #0000ff;">&quot;sql = SELECT * FROM database&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> <span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So the $sql query we have stored above is being passed to the function on the right, and then it runs through the function and returns us our info. On the second query it is only slightly different.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM database WHERE id = 1&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this one we need to pass the $querycount and the $totaltime to the function. Otherwise it won&#8217;t know the numbers. Also note that you should change the variable for the MySQL query result depending on how your page is setup.</p>
<p>Now we have the displaying of the info. In the func.query_time.php file I had a second function declared. This function is small enough I won&#8217;t break it down.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> display_time<span style="color: #009900;">&#40;</span><span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$strQueryTime</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Page created in %01.4f seconds'</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;p class=&quot;querytime&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$strQueryTime</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' with '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$querycount</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' queries.&lt;/p&gt;
}</span></pre></div></div>

<p>So the function gets the querycount and totaltime variables passed to it. Then we store the string for the page created in x seconds into $strQueryTime. The %01.4f is used to shorten up the totaltime. Right now it will pad any unused spaces with 0&#8217;s and will go four decimal places to the right. We then use the sprintf function to display that with the totaltime added in and then the total number of queries that were counted. All that is left to do is show that on the page. This is done with</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">display_time <span style="color: #009900;">&#40;</span><span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now to show it all together. First the php file followed by the function file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'func.query_time.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$sql</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SELECT * FROM database&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> <span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span>query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$sql</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SELECT * FROM database WHERE id='1'&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span>query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  display_time<span style="color: #009900;">&#40;</span><span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$querycount</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$querycount</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$totaltime</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usec</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sec</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$querytime_before</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$usec</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usec</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sec</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$querytime_after</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$usec</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$querytime</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$querytime_after</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$querytime_before</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$totaltime</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$querytime</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$querycount</span><span style="color: #339933;">++;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> <span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> display_time<span style="color: #009900;">&#40;</span><span style="color: #000088;">$querycount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000088;">$strQueryTime</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Page created in %01.4f seconds'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;p class=&quot;querytime&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$strQueryTime</span><span style="color: #339933;">,</span> <span style="color: #000088;">$totaltime</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' with '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$querycount</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' queries.&lt;/p&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>So there you have it. Any questions or things you believe I could do better, let me know in the comments. And I am sure that there will be more code that I will post before the gallery overhaul is complete. Stay tuned&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://bassmadrigal.com/blog/2008/11/mysql-query-time-and-counter/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
