<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Feature Idea:  Finding columns which query needs to access</title>
	<atom:link href="http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/</link>
	<description>Everything about MySQL Performance</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:23:57 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jason</title>
		<link>http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/comment-page-1/#comment-578358</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Mon, 08 Jun 2009 02:35:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/#comment-578358</guid>
		<description>Hah! Just getting ugly now. Last try:


$con = mysql_connect(&#039;ip&#039;, &#039;user&#039;, &#039;pass&#039;);
mysql_select_db(&quot;db&quot;, $con);

// Fill in your table name and the query you want to check.
$table = &quot;yourtable&quot;;
$checkquery= &quot;yourquery&quot;;

// We should create a tiny database since it&#039;s much faster and we don&#039;t really need the data.
$sql=&quot;create table test like $table&quot;;
$result = mysql_query($sql);
$sql=&quot;insert into test select * from $table limit 1&quot;;
$result = mysql_query($sql);

// Here we get all of the columns from the table and create an index on each one.
$sql=&quot;show columns from test&quot;;
$result = mysql_query($sql);
	while($row = mysql_fetch_array($result))
	{
$field=$row[&#039;Field&#039;];
$sql2=&quot;create index $field on test($field);&quot;;
$result2 = mysql_query($sql2);
	}

// Now we run explain to see what columns (indexes) MySQL could possibly use. Here&#039;s the trick that should get you a column list from the query.	
$sql=&quot;explain $checkquery&quot;;
$result= mysql_query($sql);
	while($row = mysql_fetch_array($result))
	{
	echo &quot;Columns: &quot; . $row[&#039;possible_keys&#039;] . &quot; are used for this query&quot;;
	}
	
// Clean up the mess.	
$sql=&quot;drop table test;&quot;;
$result = mysql_query($sql);</description>
		<content:encoded><![CDATA[<p>Hah! Just getting ugly now. Last try:</p>
<p>$con = mysql_connect(&#8217;ip&#8217;, &#8216;user&#8217;, &#8216;pass&#8217;);<br />
mysql_select_db(&#8221;db&#8221;, $con);</p>
<p>// Fill in your table name and the query you want to check.<br />
$table = &#8220;yourtable&#8221;;<br />
$checkquery= &#8220;yourquery&#8221;;</p>
<p>// We should create a tiny database since it&#8217;s much faster and we don&#8217;t really need the data.<br />
$sql=&#8221;create table test like $table&#8221;;<br />
$result = mysql_query($sql);<br />
$sql=&#8221;insert into test select * from $table limit 1&#8243;;<br />
$result = mysql_query($sql);</p>
<p>// Here we get all of the columns from the table and create an index on each one.<br />
$sql=&#8221;show columns from test&#8221;;<br />
$result = mysql_query($sql);<br />
	while($row = mysql_fetch_array($result))<br />
	{<br />
$field=$row['Field'];<br />
$sql2=&#8221;create index $field on test($field);&#8221;;<br />
$result2 = mysql_query($sql2);<br />
	}</p>
<p>// Now we run explain to see what columns (indexes) MySQL could possibly use. Here&#8217;s the trick that should get you a column list from the query.<br />
$sql=&#8221;explain $checkquery&#8221;;<br />
$result= mysql_query($sql);<br />
	while($row = mysql_fetch_array($result))<br />
	{<br />
	echo &#8220;Columns: &#8221; . $row['possible_keys'] . &#8221; are used for this query&#8221;;<br />
	}</p>
<p>// Clean up the mess.<br />
$sql=&#8221;drop table test;&#8221;;<br />
$result = mysql_query($sql);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/comment-page-1/#comment-578356</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Mon, 08 Jun 2009 02:34:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/#comment-578356</guid>
		<description>wtf? Didn&#039;t go through. Code tags here? Trying again.


So yeah this is really old but I stumbled upon it looking for the same thing and ended up building myself a php script to tease the info out of mysql. Not sure if this will work in every scenario but if the column isn&#039;t a possible key you probably don&#039;t need to index it anyway. So here it is, pretty self explanatory but I added comments in anyway. This will only work on a single table, so no joins, and it&#039;s not prettied up but you can modify it for your needs.
[code]
 [/code]

Tada! Works for me anyway.</description>
		<content:encoded><![CDATA[<p>wtf? Didn&#8217;t go through. Code tags here? Trying again.</p>
<p>So yeah this is really old but I stumbled upon it looking for the same thing and ended up building myself a php script to tease the info out of mysql. Not sure if this will work in every scenario but if the column isn&#8217;t a possible key you probably don&#8217;t need to index it anyway. So here it is, pretty self explanatory but I added comments in anyway. This will only work on a single table, so no joins, and it&#8217;s not prettied up but you can modify it for your needs.<br />
[code]<br />
 [/code]</p>
<p>Tada! Works for me anyway.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/comment-page-1/#comment-578355</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Mon, 08 Jun 2009 02:33:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/#comment-578355</guid>
		<description>So yeah this is really old but I stumbled upon it looking for the same thing and ended up building myself a php script to tease the info out of mysql. Not sure if this will work in every scenario but if the column isn&#039;t a possible key you probably don&#039;t need to index it anyway. So here it is, pretty self explanatory but I added comments in anyway. This will only work on a single table, so no joins, and it&#039;s not prettied up but you can modify it for your needs.

 

Tada! Works for me anyway.</description>
		<content:encoded><![CDATA[<p>So yeah this is really old but I stumbled upon it looking for the same thing and ended up building myself a php script to tease the info out of mysql. Not sure if this will work in every scenario but if the column isn&#8217;t a possible key you probably don&#8217;t need to index it anyway. So here it is, pretty self explanatory but I added comments in anyway. This will only work on a single table, so no joins, and it&#8217;s not prettied up but you can modify it for your needs.</p>
<p>Tada! Works for me anyway.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrisi</title>
		<link>http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/comment-page-1/#comment-25741</link>
		<dc:creator>Andrisi</dc:creator>
		<pubDate>Fri, 29 Dec 2006 17:44:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/#comment-25741</guid>
		<description>Good idea. Does not have to be human-readable. Would be useful in many client applications, that do complex user-defined queries, or ones doing editing with auto-build forms.</description>
		<content:encoded><![CDATA[<p>Good idea. Does not have to be human-readable. Would be useful in many client applications, that do complex user-defined queries, or ones doing editing with auto-build forms.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/comment-page-1/#comment-15558</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Sat, 25 Nov 2006 23:06:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/#comment-15558</guid>
		<description>Thanks Philip,

This would be cool.   You can&#039;t get parser to be equivalent to MySQL parser in all cases as there are different versions and even different MySQL Server settings may change things. But it should be good enough for most cases.</description>
		<content:encoded><![CDATA[<p>Thanks Philip,</p>
<p>This would be cool.   You can&#8217;t get parser to be equivalent to MySQL parser in all cases as there are different versions and even different MySQL Server settings may change things. But it should be good enough for most cases.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Philip Stoev</title>
		<link>http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/comment-page-1/#comment-15557</link>
		<dc:creator>Philip Stoev</dc:creator>
		<pubDate>Sat, 25 Nov 2006 23:00:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/#comment-15557</guid>
		<description>Hello, the MySQL parser has been yanked out and is now available as a separate Perl CPAN module, DBIx::MyParse. The current downloadable version has considerable limitations, however I am working on a new one that should parse any and all SELECTS that MySQL accepts. Once I get there, I will try to provide code that dumps the list of columns.</description>
		<content:encoded><![CDATA[<p>Hello, the MySQL parser has been yanked out and is now available as a separate Perl CPAN module, DBIx::MyParse. The current downloadable version has considerable limitations, however I am working on a new one that should parse any and all SELECTS that MySQL accepts. Once I get there, I will try to provide code that dumps the list of columns.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/comment-page-1/#comment-12920</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Tue, 21 Nov 2006 08:38:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/#comment-12920</guid>
		<description>To write a script you need infrastructure on MySQL Server side still.

You can&#039;t really  write full query parser or reliably check which clauses are resolved which way. 

I do not really care if it is part of explain or available some other way.</description>
		<content:encoded><![CDATA[<p>To write a script you need infrastructure on MySQL Server side still.</p>
<p>You can&#8217;t really  write full query parser or reliably check which clauses are resolved which way. </p>
<p>I do not really care if it is part of explain or available some other way.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Apachez</title>
		<link>http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/comment-page-1/#comment-12873</link>
		<dc:creator>Apachez</dc:creator>
		<pubDate>Tue, 21 Nov 2006 05:59:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/#comment-12873</guid>
		<description>Does it have to be an extension to EXPLAIN?

A standalone script (say written in perl or so) would be for me more interresting. This way you could give the script various queries and it will spit out suggestions for indexes needed. Of course in the longer run it would be interresting to have it also available like EXPLAIN EXTENDED or something...</description>
		<content:encoded><![CDATA[<p>Does it have to be an extension to EXPLAIN?</p>
<p>A standalone script (say written in perl or so) would be for me more interresting. This way you could give the script various queries and it will spit out suggestions for indexes needed. Of course in the longer run it would be interresting to have it also available like EXPLAIN EXTENDED or something&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/comment-page-1/#comment-12547</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Mon, 20 Nov 2006 16:13:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/#comment-12547</guid>
		<description>Sergey, 

You can run EXPLAIN ... \G in which case number of columns is not the problem any more. 

Adding two columns will be great, first listing columns which are used from the table and second listing where/on clauses applying to this table and how they are resolved, something like 

SELECT count(*) FROM A,B WHERE A.ID=B.ID and A.C&gt;5 and B.D&gt;6;

A: 
Used_Columns:  ID,C
Checks:  C&gt;5(range)

B:
Used_Columns:  ID,D
Checks:  ID=A.ID(ref), D&gt;6(row) 


This would allow to very easy to see which of the checks are not using index which are great candidates for optimization.</description>
		<content:encoded><![CDATA[<p>Sergey, </p>
<p>You can run EXPLAIN &#8230; \G in which case number of columns is not the problem any more. </p>
<p>Adding two columns will be great, first listing columns which are used from the table and second listing where/on clauses applying to this table and how they are resolved, something like </p>
<p>SELECT count(*) FROM A,B WHERE A.ID=B.ID and A.C>5 and B.D>6;</p>
<p>A:<br />
Used_Columns:  ID,C<br />
Checks:  C>5(range)</p>
<p>B:<br />
Used_Columns:  ID,D<br />
Checks:  ID=A.ID(ref), D>6(row) </p>
<p>This would allow to very easy to see which of the checks are not using index which are great candidates for optimization.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/comment-page-1/#comment-12520</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Mon, 20 Nov 2006 16:02:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/11/17/feature-idea-finding-columns-which-query-needs-to-access/#comment-12520</guid>
		<description>Roland,

I think it would be good, however I&#039;m not sure passing query text is good idea. Might be EXPLAIN can simply feel these tables ready to use or something similar. 

The problem with such solution is - it will be unlikely implemented any time soon as it not that trivial. 

The other problem it may be a bit complicated to use - some people hate SHOW statements but they are much easier to use compared to information schema :)</description>
		<content:encoded><![CDATA[<p>Roland,</p>
<p>I think it would be good, however I&#8217;m not sure passing query text is good idea. Might be EXPLAIN can simply feel these tables ready to use or something similar. </p>
<p>The problem with such solution is &#8211; it will be unlikely implemented any time soon as it not that trivial. </p>
<p>The other problem it may be a bit complicated to use &#8211; some people hate SHOW statements but they are much easier to use compared to information schema <img src='http://www.mysqlperformanceblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
