<?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: On user variables evaluation order</title>
	<atom:link href="http://code.openark.org/blog/mysql/on-user-variables-evaluation-order/feed" rel="self" type="application/rss+xml" />
	<link>http://code.openark.org/blog/mysql/on-user-variables-evaluation-order</link>
	<description>Blog by Shlomi Noach</description>
	<lastBuildDate>Tue, 07 Sep 2010 05:54:55 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: SQL: ranking without self join, revisited &#124; code.openark.org</title>
		<link>http://code.openark.org/blog/mysql/on-user-variables-evaluation-order/comment-page-1#comment-4266</link>
		<dc:creator>SQL: ranking without self join, revisited &#124; code.openark.org</dc:creator>
		<pubDate>Fri, 25 Sep 2009 02:04:03 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1315#comment-4266</guid>
		<description>[...] On user variables evaluation order [...]</description>
		<content:encoded><![CDATA[<p>[...] On user variables evaluation order [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shlomi</title>
		<link>http://code.openark.org/blog/mysql/on-user-variables-evaluation-order/comment-page-1#comment-4239</link>
		<dc:creator>shlomi</dc:creator>
		<pubDate>Thu, 24 Sep 2009 05:16:35 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1315#comment-4239</guid>
		<description>Baron,

Thanks very much for this information! I suspected as much; I&#039;ll also take the time to browse the source code.
Meanwhile I&#039;ve submitted a documentation bug and a feature request for more elaborate documentation.

I made some experiments, and have reached what I think is a safe (and ugly) use of variables, where order of evaluation can be expected. But it&#039;s only based on &quot;empirical study&quot;... not on sound knowledge.

Regards</description>
		<content:encoded><![CDATA[<p>Baron,</p>
<p>Thanks very much for this information! I suspected as much; I&#8217;ll also take the time to browse the source code.<br />
Meanwhile I&#8217;ve submitted a documentation bug and a feature request for more elaborate documentation.</p>
<p>I made some experiments, and have reached what I think is a safe (and ugly) use of variables, where order of evaluation can be expected. But it&#8217;s only based on &#8220;empirical study&#8221;&#8230; not on sound knowledge.</p>
<p>Regards</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Baron</title>
		<link>http://code.openark.org/blog/mysql/on-user-variables-evaluation-order/comment-page-1#comment-4237</link>
		<dc:creator>Baron</dc:creator>
		<pubDate>Thu, 24 Sep 2009 04:54:41 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1315#comment-4237</guid>
		<description>Shlomi, I&#039;ve actually tried to read the mysqld source code to see if I could find out how variables are used, and came to the conclusion that it&#039;s buried deep in the optimizer and I don&#039;t understand it well enough to draw conclusions.</description>
		<content:encoded><![CDATA[<p>Shlomi, I&#8217;ve actually tried to read the mysqld source code to see if I could find out how variables are used, and came to the conclusion that it&#8217;s buried deep in the optimizer and I don&#8217;t understand it well enough to draw conclusions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shlomi</title>
		<link>http://code.openark.org/blog/mysql/on-user-variables-evaluation-order/comment-page-1#comment-4055</link>
		<dc:creator>shlomi</dc:creator>
		<pubDate>Thu, 17 Sep 2009 09:18:22 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1315#comment-4055</guid>
		<description>Hi Roland,

Thanks very much for this detailed review.
I see your point with you last example.
This is exactly why I&#039;m saying nothing is really defined. The docs only talk about &quot;parts of the statement&quot;, but have no discussion of the time at which variables are actually evaluated (e.g. before or after ORDER BY).

For all I know, there *could be* a well defined behavior that we are simply unaware of. This is where I&#039;d like to get more definitive answers. I realize this post may not be the best place to look for these answers, but I&#039;m not sure where is.

I&#039;ll be perfectly content if told there&#039;s a defined behavior in 1,2,3 simplest cases (e.g. full table scan, scan+ORDER BY, etc.). I can work from there to achieve results.</description>
		<content:encoded><![CDATA[<p>Hi Roland,</p>
<p>Thanks very much for this detailed review.<br />
I see your point with you last example.<br />
This is exactly why I&#8217;m saying nothing is really defined. The docs only talk about &#8220;parts of the statement&#8221;, but have no discussion of the time at which variables are actually evaluated (e.g. before or after ORDER BY).</p>
<p>For all I know, there *could be* a well defined behavior that we are simply unaware of. This is where I&#8217;d like to get more definitive answers. I realize this post may not be the best place to look for these answers, but I&#8217;m not sure where is.</p>
<p>I&#8217;ll be perfectly content if told there&#8217;s a defined behavior in 1,2,3 simplest cases (e.g. full table scan, scan+ORDER BY, etc.). I can work from there to achieve results.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roland Bouman</title>
		<link>http://code.openark.org/blog/mysql/on-user-variables-evaluation-order/comment-page-1#comment-4052</link>
		<dc:creator>Roland Bouman</dc:creator>
		<pubDate>Thu, 17 Sep 2009 08:26:13 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1315#comment-4052</guid>
		<description>Hi Shlomi, 

I think you raise a few valid points. For instance I think that it is indeed correct to assume that one could force the order of evaluation with parenthesis if it is all contained within the same expression.

However, the way I see it, this will only force the per-row evaluation order. Major problems can still arise when your expression relies on assignments that happened during the processing of some row other than the current row. Since the semantics of the SELECT statement is to deliver a set of rows, there is now way to force the order of row processing before constructing the resultset.

A simple example: 

SET @a:=0, @b:=0;
SELECT IF(@a:=@b+1, @b:=@a, NULL) 
FROM sakila.film_text;

In this case, no columns are accessed and it really does not matter in what order the rows are processed. Most likely, they are scanned in the order that is convenient for the underlying storage engine. So this is safe.

Now let&#039;s add a column: 

SET @a:=0, @b:=0;
SELECT film_id, IF(@a:=@b+1, @b:=@a, NULL) 
FROM sakila.film_text;

In this case, there is a covering index for film_id, and this is likely to be used. So, rows are processed in order of the index on film_id. Still, this does not matter for the result.

Now we change it still some:

SET @a:=0, @b:=0;
SELECT film_id, IF(@a:=@b+1, @b:=@a, NULL) 
FROM sakila.film
ORDER BY title;

The problem is, are the variables evaluated before the ORDER BY, or at the same time as the ORDER BY? I see different plans being used, and I get different results as compared to the previous query (beyond row order) 

It gets worse when we add a where. We would like the evaluations to be done after applying the WHERE, but what guarantee do we have?

just my 2 cts.</description>
		<content:encoded><![CDATA[<p>Hi Shlomi, </p>
<p>I think you raise a few valid points. For instance I think that it is indeed correct to assume that one could force the order of evaluation with parenthesis if it is all contained within the same expression.</p>
<p>However, the way I see it, this will only force the per-row evaluation order. Major problems can still arise when your expression relies on assignments that happened during the processing of some row other than the current row. Since the semantics of the SELECT statement is to deliver a set of rows, there is now way to force the order of row processing before constructing the resultset.</p>
<p>A simple example: </p>
<p>SET @a:=0, @b:=0;<br />
SELECT IF(@a:=@b+1, @b:=@a, NULL)<br />
FROM sakila.film_text;</p>
<p>In this case, no columns are accessed and it really does not matter in what order the rows are processed. Most likely, they are scanned in the order that is convenient for the underlying storage engine. So this is safe.</p>
<p>Now let&#8217;s add a column: </p>
<p>SET @a:=0, @b:=0;<br />
SELECT film_id, IF(@a:=@b+1, @b:=@a, NULL)<br />
FROM sakila.film_text;</p>
<p>In this case, there is a covering index for film_id, and this is likely to be used. So, rows are processed in order of the index on film_id. Still, this does not matter for the result.</p>
<p>Now we change it still some:</p>
<p>SET @a:=0, @b:=0;<br />
SELECT film_id, IF(@a:=@b+1, @b:=@a, NULL)<br />
FROM sakila.film<br />
ORDER BY title;</p>
<p>The problem is, are the variables evaluated before the ORDER BY, or at the same time as the ORDER BY? I see different plans being used, and I get different results as compared to the previous query (beyond row order) </p>
<p>It gets worse when we add a where. We would like the evaluations to be done after applying the WHERE, but what guarantee do we have?</p>
<p>just my 2 cts.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
