<?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"
	>
<channel>
	<title>Comments on: Its a SQL Thing</title>
	<atom:link href="http://www.rubygeek.com/2005/11/09/its-a-sql-thing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rubygeek.com/2005/11/09/its-a-sql-thing/</link>
	<description>Ruby, PHP, Python and Perl</description>
	<pubDate>Thu, 20 Nov 2008 17:54:42 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Nola</title>
		<link>http://www.rubygeek.com/2005/11/09/its-a-sql-thing/#comment-6</link>
		<dc:creator>Nola</dc:creator>
		<pubDate>Tue, 31 Jan 2006 03:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubygeek.com/?p=20#comment-6</guid>
		<description>Thanks, I'll give that a try!</description>
		<content:encoded><![CDATA[<p>Thanks, I&#8217;ll give that a try!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lucidix</title>
		<link>http://www.rubygeek.com/2005/11/09/its-a-sql-thing/#comment-5</link>
		<dc:creator>Lucidix</dc:creator>
		<pubDate>Mon, 30 Jan 2006 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubygeek.com/?p=20#comment-5</guid>
		<description>This is going to be MySQL specific, and it has to do how MySQL internally handles WHERE vs. HAVING, which actually may have changed as of 5.0 (I remember reading something about planned changes for MySQL 5 some time ago, and it mentioned the behavior of HAVING, but TIAS)

I'll try to simplify it a bit:

Essentially any conditions in the WHERE clause are applied before the first pass over the data. At that point "user" doesn't exist yet.

HAVING is applied AFTER the table has been processed and WHERE has been applied, aliases have been created, etc.

You can write your query as:

SELECT
  h.username AS user
FROM
  userdata AS h
HAVING
  user = 'nstowe';

But, and I'm not entirely sure as I haven't looked at the MySQL source code, I believe what's happening internally, again, is this:

First, WHERE clauses are taken into consideration when performing the first table scan. Since there are none, the whole table is read. (Potential performance issue with large tables?)

Then, HAVING is taken into consideration during the second pass and everything but "nstowe" is thrown out.

I haven't really run into any performance issues myself, so maybe I'm wrong on the internals, or it simply doesn't affect performance as much as I thought.</description>
		<content:encoded><![CDATA[<p>This is going to be MySQL specific, and it has to do how MySQL internally handles WHERE vs. HAVING, which actually may have changed as of 5.0 (I remember reading something about planned changes for MySQL 5 some time ago, and it mentioned the behavior of HAVING, but TIAS)</p>
<p>I&#8217;ll try to simplify it a bit:</p>
<p>Essentially any conditions in the WHERE clause are applied before the first pass over the data. At that point &#8220;user&#8221; doesn&#8217;t exist yet.</p>
<p>HAVING is applied AFTER the table has been processed and WHERE has been applied, aliases have been created, etc.</p>
<p>You can write your query as:</p>
<p>SELECT<br />
  h.username AS user<br />
FROM<br />
  userdata AS h<br />
HAVING<br />
  user = &#8216;nstowe&#8217;;</p>
<p>But, and I&#8217;m not entirely sure as I haven&#8217;t looked at the MySQL source code, I believe what&#8217;s happening internally, again, is this:</p>
<p>First, WHERE clauses are taken into consideration when performing the first table scan. Since there are none, the whole table is read. (Potential performance issue with large tables?)</p>
<p>Then, HAVING is taken into consideration during the second pass and everything but &#8220;nstowe&#8221; is thrown out.</p>
<p>I haven&#8217;t really run into any performance issues myself, so maybe I&#8217;m wrong on the internals, or it simply doesn&#8217;t affect performance as much as I thought.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Ray</title>
		<link>http://www.rubygeek.com/2005/11/09/its-a-sql-thing/#comment-4</link>
		<dc:creator>Brian Ray</dc:creator>
		<pubDate>Fri, 11 Nov 2005 20:22:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubygeek.com/?p=20#comment-4</guid>
		<description>Hey PhpGirl,

What MySQL version. If greater than 4.1, you can use subselects. Totally untested, but something like:

SELECT * 
&#160;&#160;&#160;FROM ( 
&#160;&#160;&#160;&#160;&#160;&#160;SELECT h.username user 
&#160;&#160;&#160;&#160;&#160;&#160;FROM userdata h 
&#160;&#160;&#160;)  
WHERE user ="nstowe" ;

This of course is not effiecent so I am sure there is a better way. BTW, 'AS' is considered noise-- it's not needed.</description>
		<content:encoded><![CDATA[<p>Hey PhpGirl,</p>
<p>What MySQL version. If greater than 4.1, you can use subselects. Totally untested, but something like:</p>
<p>SELECT *<br />
&nbsp;&nbsp;&nbsp;FROM (<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT h.username user<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM userdata h<br />
&nbsp;&nbsp;&nbsp;)<br />
WHERE user =&#8221;nstowe&#8221; ;</p>
<p>This of course is not effiecent so I am sure there is a better way. BTW, &#8216;AS&#8217; is considered noise&#8211; it&#8217;s not needed.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
