<?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>TheUnical Technologies Blog &#187; SQL Profiler</title>
	<atom:link href="http://blog.theunical.com/category/databases/sql-rofiler/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.theunical.com</link>
	<description>TheUnical Technologies Official Blog</description>
	<lastBuildDate>Fri, 27 Jan 2012 05:48:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>MySQL Query Profiler</title>
		<link>http://blog.theunical.com/databases/mysql/mysql-query-profiler/</link>
		<comments>http://blog.theunical.com/databases/mysql/mysql-query-profiler/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 09:56:00 +0000</pubDate>
		<dc:creator>Steven Wall</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[MySQL 5.1]]></category>
		<category><![CDATA[MySql]]></category>
		<category><![CDATA[MySql 5.0]]></category>
		<category><![CDATA[SQL Profiler]]></category>
		<category><![CDATA[MySql Profiler]]></category>

		<guid isPermaLink="false">http://rachasatish.wordpress.com/2008/09/28/mysql-query-profiler/</guid>
		<description><![CDATA[Using the New MySQL Query Profiler One of the great things about MySQL is the superior innovation model that’s used to deliver database server software. Rather than relying solely on internal engineers who create and maintain a piece of software (as in a traditional software company), MySQL partners with the millions of active users across [...]]]></description>
			<content:encoded><![CDATA[<div class="Section1">
<p class="MsoNormal"><span style="font-family:Calibri;font-size:85%;"><span style="font-size:11px;"> </span></span></p>
<h1><b><span style="font-family:Times New Roman;font-size:6px;"><span style="font-size:24px;">Using the New MySQL Query Profiler</span></span></b></h1>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">One of the great things about MySQL is the superior innovation model that’s used to deliver database server software. Rather than relying solely on internal engineers who create and maintain a piece of software (as in a traditional software company), MySQL partners with the millions of active users across the world who take advantage of the open source model and daily extend the MySQL server to do new and pioneering things. These innovations can then be submitted to MySQL AB, tested, validated, and rolled into the database server so everyone can benefit from the creativity of the very active MySQL community.</span></span></p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">An example of this model in action is the recent release of a new SQL diagnostic facility that assists in the debugging and analysis of running SQL &#8211; the SQL Profiler. The new profiler became available in the 5.0.37 version of the MySQL Community Server and was created and submitted by Jeremy Cole of Proven Scaling (<a href="http://www.provenscaling.com/">http://www.provenscaling.com/</a>).</span></span></p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Let&#8217;s take a look at this new diagnostic utility Jeremy developed and see how it can help you create high-performing SQL code as well as assist in troubleshooting existing queries that aren&#8217;t providing the response times you&#8217;d like.</span></span></p>
<h2><b><span style="font-family:Times New Roman;font-size:180%;"><span style="font-size:18px;">The Best Way to Diagnose Performance Problems</span></span></b></h2>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">When it comes to overall performance, it&#8217;s important to remember that the #1 contributor is always a good database design. The second highest contributor to performance is well-tuned SQL code. Some try and switch the two in priority, but this is a mistake because a bad design has the potential to simply drown even the best-tuned SQL code (e.g. you can&#8217;t get index access in your SQL queries if the physical index design is poorly done). But make no mistake &#8211; bad SQL code that&#8217;s introduced into an otherwise well-running database can make a real train wreck of things.</span></span></p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">So how do you go about analyzing database performance? There are three forms of performance analysis that are used to troubleshoot and tune database systems:</span></span></p>
<ol start="1" type="1">
<li class="MsoNormal"><span style="font-family:Calibri;font-size:85%;"><span style="font-size:11px;">Bottleneck analysis &#8211; focuses on answering the questions: What is      my database server waiting on; what is a user connection waiting on; what      is a piece of SQL code waiting on?</span></span></li>
<li class="MsoNormal"><span style="font-family:Calibri;font-size:85%;"><span style="font-size:11px;">Workload analysis &#8211; examines the server and who is logged on to      determine the resource usage and activity of each.</span></span></li>
<li class="MsoNormal"><span style="font-family:Calibri;font-size:85%;"><span style="font-size:11px;">Ratio-based analysis &#8211; utilizes a number of rule-of-thumb ratios      to gauge performance of a database, user connection, or piece of code.</span></span></li>
</ol>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Of the three, bottleneck analysis is the most efficient approach in terms of fast problem resolution. By determining where the server, a user connection, or set of SQL code is spending its time, you can then work to eliminate the discovered blockages, increase throughput, and reduce overall response times. Unfortunately, this hasn&#8217;t always been easy in MySQL because the server hasn&#8217;t traditionally exposed the type of wait-based and resource statistics to quickly uncover bottlenecks.</span></span></p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">But a better day has come along in MySQL 5.0.37 with the inclusion of the SQL Profiling utility. This interactive diagnostic aid allows a developer or DBA to gain insight into where a set of SQL code spends its time and the resources it is using. The best way to see the power of this new utility is to walk through some examples of how it&#8217;s used and see the clear benefits it supplies, so let&#8217;s do just that.</span></span></p>
<h2><b><span style="font-family:Times New Roman;font-size:180%;"><span style="font-size:18px;">Getting started with the SQL Profiler</span></span></b></h2>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">The SQL Profiler is built into the database server and can be dynamically enabled/disabled via the MySQL client utility. To begin profiling one or more SQL queries, simply issue the following command:</span></span></p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; set profiling=1;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">Query OK, 0 rows affected (0.00 sec)</span></span></pre>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Two things happen once you issue this command. First, any query you issue from this point on will be traced by the server with various performance diagnostics being created and attached to each distinct query. Second, a memory table named </span></span><code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">profiling</span></span></code> is created in the INFORMATION_SCHEMA database for your particular session (not viewable by any other MySQL session) that stores all the SQL diagnostic results. This table remains persistent until you disconnect from MySQL at which point it is destroyed.</p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Now, simply execute a SQL query:</span></span></p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; select count(*) from client where broker_id=2;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| count(*) |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|      200 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">1 row in set (0.00 sec)</span></span></pre>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Once the query completes, you can issue the following command to view the SQL profiles that have currently been stored for you:</span></span></p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; show profiles;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+------------+-----------------------------------------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Query_ID | Duration   | Query
  |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+------------+-----------------------------------------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        0 | 0.00007300 | set profiling=1                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        1 | 0.00044700 | select count(*) from client where broker_id=2 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+------------+-----------------------------------------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">2 rows in set (0.00 sec)</span></span></pre>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">You get a quick summary of all your captured SQL plus the total duration that the query took to complete. To get the same diagnostic info, you can also query the memory table that holds your statistical information:</span></span></p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; select sum(duration) from information_schema.profiling where query_id=1;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+---------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| sum(duration) |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+---------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|      0.000447 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+---------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">1 row in set (0.00 sec)</span></span></pre>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">I&#8217;ll show you why it&#8217;s good to be able to query the memory-based profiling table later in this article, but for now, let&#8217;s concentrate on other SHOW commands that you can use to get more detailed diagnostic info about one or more queries that you&#8217;ve profiled. The most basic command is one that lists the steps a profiled query went through to satisfy your SQL request, along with each step&#8217;s time:</span></span></p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; show profile for query 1;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+--------------------+------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Status             | Duration   |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+--------------------+------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| (initialization)   | 0.00006300 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Opening tables     | 0.00001400 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| System lock        | 0.00000600 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Table lock         | 0.00001000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| init               | 0.00002200 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| optimizing         | 0.00001100 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| statistics         | 0.00009300 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| preparing          | 0.00001700 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| executing          | 0.00000700 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Sending data       | 0.00016800 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| end                | 0.00000700 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| query end          | 0.00000500 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| freeing items      | 0.00001200 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| closing tables     | 0.00000800 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| logging slow query | 0.00000400 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+--------------------+------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">15 rows in set (0.00 sec)</span></span></pre>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">You can also just issue </span></span><code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">SHOW PROFILE</span></span></code> and exclude the identification of a specific profile number to see the very last profile you created.</p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Regardless of how you obtain the output, you can see this data is extremely valuable in that it allows you to see where your query spends its time during execution. This is done for all types of queries, and not just </span></span><code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">SELECT</span></span></code> statements as this example shows:</p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; alter table t engine=myisam;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">Query OK, 112050 rows affected (0.64 sec)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">Records: 112050  Duplicates: 0  Warnings: 0</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;"> </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; show profiles;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+------------+-----------------------------------------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Query_ID | Duration   | Query                                         |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+------------+-----------------------------------------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        0 | 0.00007300 | set profiling=1                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        1 | 0.00044700 | select count(*) from client where broker_id=2 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        2 | 0.

00003400 | set profiling=0                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        3 | 0.00007400 | set profiling=1                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        4 | 0.63789700 | alter table t engine=myisam                   |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        5 | 0.00004000 | set profiling=0                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+------------+-----------------------------------------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">6 rows in set (0.00 sec)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;"> </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; show profile for query 4;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------------------+------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Status               | Duration   |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------------------+------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| (initialization)     | 0.00002900 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| checking permissions | 0.00000800 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| init                 | 0.00004000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Opening table        | 0.00009400 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| System lock          | 0.00000500 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Table lock           | 0.00000700 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| setup                | 0.00004200 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| creating table       | 0.00195800 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| After create         | 0.00010900 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| copy to tmp table    | 0.52264500 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| rename result table  | 0.11289400 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| end                  | 0.00004600 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| query end            | 0.00000700 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| freeing items        | 0.00001300 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------------------+------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">14 rows in set (0.00 sec)</span></span></pre>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">So as you can see in the above profile, the </span></span><code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">ALTER TABLE</span></span></code> statement spends the bulk of its time in the temporary table copy step. Armed with this type of information, you have more insight into the hoops your query is jumping through from start to finish, and therefore, you can then work to tune your queries to help eliminate any identified bottlenecks.</p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">There&#8217;s more information than just duration that you can get from your profiles &#8211; for example, CPU usage (which, unfortunately, is not available on all platforms; the below comes from Linux):</span></span></p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; show profile cpu for query 4;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------------------+------------+------------+------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Status               | Duration   | CPU_user   | CPU_system |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------------------+------------+------------+------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| (initialization)     | 0.00002900 | 0.00000000 | 0.00000000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| checking permissions | 0.00000800 | 0.00000000 | 0.00000000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| init                 | 0.00004000 | 0.00000000 | 0.00000000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Opening table        | 0.00009400 | 0.00100000 | 0.00000000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| System lock          | 0.00000500 | 0.00000000 | 0.00000000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Table lock           | 0.00000700 | 0.00000000 | 0.00000000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| setup                | 0.00004200 | 0.00000000 | 0.00000000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| creating table       | 0.00195800 | 0.00000000 | 0.00100000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| After create         | 0.00010900 | 0.00000000 | 0.00000000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| copy to tmp table    | 0.52264500 | 0.55591600 | 0.04199300 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| rename result table  | 0.11289400 | 0.00199900 | 0.00000000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| end                  | 0.00004600 | 0.00000000 | 0.00000000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| query end            | 0.00000700 | 0.00000000 | 0.00000000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| freeing items        | 0.00001300 | 0.00000000 | 0.00000000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------------------+------------+------------+------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">14 rows in set (0.00 sec)</span></span></pre>
<p><span style="font-fam</p>
<p>ily:Times New Roman;font-size:100%;"><span style="font-size:12px;">Other parameters that can be passed to the </span></span><code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">SHOW PROFILE</span></span></code> command include:</p>
<ul type="disc">
<li class="MsoNormal"><b><span style="font-family:Calibri;font-size:85%;"><span style="font-weight:bold;font-size:11px;">ALL</span></span></b> &#8211; displays      all information</li>
<li class="MsoNormal"><b><span style="font-family:Calibri;font-size:85%;"><span style="font-weight:bold;font-size:11px;">BLOCK IO</span></span></b> &#8211;      displays counts for block input and output operations</li>
<li class="MsoNormal"><b><span style="font-family:Calibri;font-size:85%;"><span style="font-weight:bold;font-size:11px;">CONTEXT SWITCHES</span></span></b>      &#8211; displays counts for voluntary and involuntary context switches</li>
<li class="MsoNormal"><b><span style="font-family:Calibri;font-size:85%;"><span style="font-weight:bold;font-size:11px;">IPC</span></span></b> &#8211; displays      counts for messages sent and received</li>
<li class="MsoNormal"><b><span style="font-family:Calibri;font-size:85%;"><span style="font-weight:bold;font-size:11px;">MEMORY</span></span></b> &#8211; is      not currently implemented</li>
<li class="MsoNormal"><b><span style="font-family:Calibri;font-size:85%;"><span style="font-weight:bold;font-size:11px;">PAGE FAULTS</span></span></b> &#8211;      displays counts for major and minor page faults</li>
<li class="MsoNormal"><b><span style="font-family:Calibri;font-size:85%;"><span style="font-weight:bold;font-size:11px;">SOURCE</span></span></b> &#8211;      displays the names of functions from the source code, together with the      name and line number of the file in which the function occurs</li>
<li class="MsoNormal"><b><span style="font-family:Calibri;font-size:85%;"><span style="font-weight:bold;font-size:11px;">SWAPS</span></span></b> &#8211;      displays swap counts</li>
</ul>
<h2><b><span style="font-family:Times New Roman;font-size:180%;"><span style="font-size:18px;">Using the Profiler for Tuning Queries</span></span></b></h2>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Let&#8217;s take a look at a quick example of how the new profiler can be a help to tune an inefficient query. I don&#8217;t know about you, but I&#8217;ve always been just so-so at being able to really use </span></span><code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">EXPLAIN</span></span></code>&#8216;s to troubleshoot queries. But the new profiler makes things pretty nice and easy.</p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">I have a MySQL database that&#8217;s used to track investment activity for a small brokerage house. There&#8217;s a view in the database that&#8217;s used to get a quick report of client accounts over a million dollars:</span></span></p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; select * from v_client_portfolio_high;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-----------+-------------------+------------------+-----------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| client_id | client_first_name | client_last_name | portfolio_value |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-----------+-------------------+------------------+-----------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|         5 | ABNER             | ROSSELLETT       |      1252115.50 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|       500 | CANDICE           | BARTLETT         |      1384877.50 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-----------+-------------------+------------------+-----------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">2 rows in set (3.73 sec)</span></span></pre>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Now a query running under four seconds isn&#8217;t necessarily a bad thing, but I wonder if things could be made a little bit better. So let&#8217;s profile the view and see what we can discover:</span></span></p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; set profiling=1;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">Query OK, 0 rows affected (0.00 sec)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;"> </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; select * from v_client_portfolio_high;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-----------+-------------------+------------------+-----------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| client_id | client_first_name | client_last_name | portfolio_value |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-----------+-------------------+------------------+-----------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|         5 | ABNER             | ROSSELLETT       |      1252115.50 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|       500 | CANDICE           | BARTLETT         |      1384877.50 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-----------+-------------------+------------------+-----------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">2 rows in set (4.01 sec)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;"> </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; set profiling=0;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">Query OK, 0 rows affected (0.00 sec)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;"> </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; show profiles;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+------------+-----------------------------------------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Query_ID | Duration   | Query                                         |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+------------+-----------------------------------------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        0 | 0.00007300 | set profiling=1                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        1 | 0.00044700 | select count(*) from client where broker_id=2 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        2 | 0.00003400 | set profiling=0                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        3 | 0.00007400
| set profiling=1                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        4 | 0.63789700 | alter table t engine=myisam                   |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        5 | 0.00004000 | set profiling=0                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        6 | 0.00007600 | set profiling=1                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        7 | 4.01965600 | select * from v_client_portfolio_high         |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        8 | 0.00003500 | set profiling=0                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+------------+-----------------------------------------------+</span></span></pre>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Now at first, I&#8217;m tempted to issue a </span></span><code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">SHOW PROFILE</span></span></code> for query 7, which is my view query, but instead let&#8217;s see just how many lines of diagnostic data the profiler has collected for me:</p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; select count(*) from information_schema.profiling where query_id=7;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| count(*) |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|    74734 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+</span></span></pre>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Hello! The profiler generated over 74,000 lines of output for me, which might be hard to wade through using the normal </span></span><code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">SHOW PROFILE</span></span></code> command (even though a LIMIT option is provided). Instead, let&#8217;s use a SQL query against the profiling table to see what our query did:</p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; select min(seq) seq,state,count(*) numb_ops,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; round(sum(duration),5) sum_dur, round(avg(duration),5) avg_dur,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; round(sum(cpu_user),5) sum_cpu, round(avg(cpu_user),5) avg_cpu</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; from information_schema.profiling</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; where query_id = 7</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; group by state</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; order by seq;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-------+----------------------+----------+---------+---------+---------+---------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| seq   | state                | numb_ops | sum_dur | avg_dur | sum_cpu | avg_cpu |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-------+----------------------+----------+---------+---------+---------+---------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     0 | (initialization)     |        1 | 0.00004 | 0.00004 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     1 | Opening tables       |        1 | 0.00023 | 0.00023 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     2 | System lock          |        1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     3 | Table lock           |        1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     4 | checking permissions |        1 | 0.00010 | 0.00010 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     5 | optimizing           |        4 | 0.00004 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     6 | statistics           |        4 | 0.00007 | 0.00002 | 0.00100 | 0.00025 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     7 | preparing            |        4 | 0.00005 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     8 | Creating tmp table   |        1 | 0.00003 | 0.00003 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     9 | executing            |    37352 | 0.16631 | 0.00000 | 0.05899 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|    10 | Copying to tmp table |        1 | 0.00006 | 0.00006 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|    15 | Sending data         |    37353 | 3.85151 | 0.00010 | 3.72943 | 0.00010 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 74717 | Sorting result       |        1 | 0.00112 | 0.00112 | 0.00100 | 0.00100 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 74719 | removing tmp table   |        2 | 0.00003 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 74721 | init                 |        1 | 0.00002 | 0.00002 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 74727 | end                  |        1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 74728 | query end            |        1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 74729 | freeing items        |        1 | 0.00002 | 0.00002 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 74730 | closing tables       |        2 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:

10px;">| 74733 | logging slow query   |        1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-------+----------------------+----------+---------+---------+---------+---------+</span></span></pre>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Looking at the diagnostics above, I can see that my view query spends the bulk of its time in the </span></span><code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">Sending data</span></span></code> step, with that step and the <code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">executing</span></span></code> step experiencing over 37,000 operations. Why would this be the case? Let&#8217;s take a look at the underlying view definition and see what the actual query looks like:</p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">create view v_client_portfolio_high </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">(</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    client_id,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    client_first_name,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    client_last_name,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    portfolio_value</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">as</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">select  b.client_id,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">        b.client_first_name,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">        b.client_last_name,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">       (select sum(number_of_units * price) </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">from client_transaction c </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">where c.action = 'buy' and </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">c.client_id = b.client_id) -</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">(select sum(number_of_units * price) </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">from client_transaction d </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">where d.action = 'sell' and </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">d.client_id = b.client_id) portfolio_value</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">from    client_transaction a,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">        client b</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">where   a.client_id = b.client_id </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">group by b.client_id,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">        b.client_first_name,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">        b.client_last_name</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">having portfolio_value &gt; 1000000</span></span></pre>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">The view does some normal column grabs from the underlying base tables, but then uses some inline views to calculate a client&#8217;s overall portfolio value. The inline views/subselects are obviously what&#8217;s causing the large number of data sends and executions. Let&#8217;s eliminate just one of the subselects by pulling it up into the main query and if that makes a difference. We&#8217;ll drop and recreate the view (with a little clean up on NULL values that may affect the computed outcome), profile the new view&#8217;s performance, and then examine the diagnostic output:</span></span></p>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; drop view v_client_portfolio_high;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">Query OK, 0 rows affected (0.00 sec)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;"> </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; create view v_client_portfolio_high</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; (</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;     client_id,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;     client_first_name,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;     client_last_name,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;     portfolio_value</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; )</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; as</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; select  b.client_id,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;  b.client_first_name,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;  b.client_last_name,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;  sum(number_of_units * price)  -</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;  case</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;          (select sum(number_of_units * price)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;          from client_transaction d</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;          where d.action = 'sell' and</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;          d.client_id = b.client_id)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;  when NULL then 0</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;  else</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;
         (select sum(number_of_units * price)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;          from client_transaction d</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;          where d.action = 'sell' and</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;          d.client_id = b.client_id)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;  end portfolio_value</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; from    client_transaction a,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;  client b</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; where   a.client_id = b.client_id and</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;  action = 'buy'</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; group    by b.client_id,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;  b.client_first_name,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt;  b.client_last_name</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; having   portfolio_value &gt; 1000000;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">Query OK, 0 rows affected (0.00 sec)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;"> </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; set profiling=1;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">Query OK, 0 rows affected (0.00 sec)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;"> </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; select * from v_client_portfolio_high;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-----------+-------------------+------------------+-----------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| client_id | client_first_name | client_last_name | portfolio_value |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-----------+-------------------+------------------+-----------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|         5 | ABNER             | ROSSELLETT       |      1252115.50 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|       500 | CANDICE           | BARTLETT         |      1384877.50 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-----------+-------------------+------------------+-----------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">2 rows in set (0.47 sec)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;"> </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; set profiling=0;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">Query OK, 0 rows affected (0.00 sec)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;"> </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; show profiles;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+------------+-----------------------------------------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| Query_ID | Duration   | Query                                         |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+------------+-----------------------------------------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        0 | 0.00007300 | set profiling=1                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        1 | 0.00044700 | select count(*) from client where broker_id=2 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        2 | 0.00003400 | set profiling=0                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        3 | 0.00007400 | set profiling=1                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        4 | 0.63789700 | alter table t engine=myisam                   |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        5 | 0.00004000 | set profiling=0                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        6 | 0.00007600 | set profiling=1                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        7 | 4.01965600 | select * from v_client_portfolio_high         |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        8 | 0.00003500 | set profiling=0                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|        9 | 0.00007500 | set profiling=1                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|       10 | 0.45292700 | select * from v_client_portfolio_high         |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|       11 | 0.00003800 | set profiling=0                               |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+----------+------------+-----------------------------------------------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">12 rows in set (0.00 sec)</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;"> </span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">mysql&gt; select min(seq) seq,state,count(*) numb_ops,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; round(sum(duration),5) sum_dur, round(avg(duration),5) avg_dur,</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; round(sum(cpu_user),5) sum_cpu, round(avg(cpu_user),5) avg_cpu</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; from infor

mation_schema.profiling</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; where query_id = 10</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; group by state</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">    -&gt; order by seq;</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-------+----------------------+----------+---------+---------+---------+---------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| seq   | state                | numb_ops | sum_dur | avg_dur | sum_cpu | avg_cpu |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-------+----------------------+----------+---------+---------+---------+---------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     0 | (initialization)     |        1 | 0.00004 | 0.00004 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     1 | Opening tables       |        1 | 0.00024 | 0.00024 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     2 | System lock          |        1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     3 | Table lock           |        1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     4 | checking permissions |        1 | 0.00012 | 0.00012 | 0.00100 | 0.00100 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     5 | optimizing           |        4 | 0.00005 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     6 | statistics           |        4 | 0.00007 | 0.00002 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     7 | preparing            |        4 | 0.00005 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     8 | Creating tmp table   |        1 | 0.00003 | 0.00003 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|     9 | executing            |    11194 | 0.04983 | 0.00000 | 0.01800 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|    10 | Copying to tmp table |        1 | 0.00008 | 0.00008 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">|    15 | Sending data         |    11195 | 0.39853 | 0.00004 | 0.36794 | 0.00003 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 22401 | Sorting result       |        1 | 0.00375 | 0.00375 | 0.00400 | 0.00400 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 22403 | removing tmp table   |        2 | 0.00005 | 0.00002 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 22405 | init                 |        1 | 0.00002 | 0.00002 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 22411 | end                  |        1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 22412 | query end            |        1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 22413 | freeing items        |        1 | 0.00002 | 0.00002 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 22414 | closing tables       |        2 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">| 22417 | logging slow query   |        1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">+-------+----------------------+----------+---------+---------+---------+---------+</span></span></pre>
<pre><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">20 rows in set (0.44 sec)</span></span></pre>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Much better! Although more tuning on the view can likely be done, this simple change has knocked the Sending data and the executing step counts down by over two-thirds, with the end result being an 88% reduction in overall response time. The profiler was very useful in this case as this particular query shows that even though an individual step in the SQL execution process may be handled very quickly (the average duration column in the above query), having that same step executed over and over again can put a major dent in total query performance.</span></span></p>
<h2><b><span style="font-family:Times New Roman;font-size:180%;"><span style="font-size:18px;">Some Last Thoughts on the Profiler</span></span></b></h2>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Here are some other things to keep in mind when using the new SQL profiler:</span></span></p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">Profiling is initially turned off for each session.</span></span></p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">By default, you can store 15 different query profiles for your session. You can increase this up to 100 by setting the </span></span><code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">profiling_history_size</span></span></code> session variable. Certain diagnostics rely on operating system support for the <code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">getrusage()</span></span></code> system call, so you may see NULL values for some statistics if you&#8217;re on a platform that doesn&#8217;t support this function.</p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">There were a few bugs in the initial release of the profiler. The first centered around a problem that developed if you issued a </span></span><code><span style="font-family:Courier New;font-size:85%;"><span style="font-size:10px;">SHOW PROFILES</span></span></code> command before enabling profiling. Another concerned duration statistics that were not accurately paired up for each SQL execution step. All these have been fixed now and will be in the next Community server source drop and binary.</p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">For more information on the SQL profiler, you can review the documentation found in the MySQL 5.0 manual at: <a href="http://dev.mysql.com/doc/refman/5.0/en/show-profiles.html">http://dev.mysql.com/doc/refman/5.0/en/show-profiles.html</a>.</span></span></p>
<p><span style="font-family:Times New Roman;font-size:100%;"><span style="font-size:12px;">The new SQL profiler is a nice addition to the DBA&#8217;s and developer&#8217;s toolbelt and is a great example of the vibrant MySQL Community (in this cas</p>
<p>e, Jeremy Cole) in action. I can&#8217;t wait to see what they come up with next!</span></span></p>
<p class="MsoNormal"><span style="font-family:Calibri;font-size:85%;"><span style="font-size:11px;"> </span></span></p>
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.theunical.com/databases/mysql/mysql-query-profiler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

