<?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: Advanced MySQL user variable techniques</title>
	<atom:link href="http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/</link>
	<description>Stay curious!</description>
	<lastBuildDate>Thu, 09 Feb 2012 09:56:43 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Ian</title>
		<link>http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/#comment-19847</link>
		<dc:creator>Ian</dc:creator>
		<pubDate>Sun, 22 Jan 2012 15:54:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=280#comment-19847</guid>
		<description>This example is incorrect:

set @num=0, @type := &#039;&#039;;
select * from fruits
where (@num := if(type = @type, @num + 1, 1))
      and (@type := type)
      and (@num &lt;= 2);
Empty set (0.00 sec)

select @num, @type;
+------+--------+
&#124; @num &#124; @type  &#124;
+------+--------+
&#124; 9    &#124; cherry &#124; 
+------+--------+

On MySQL 5.1.41-3ubuntu12.10 the result is:

select @num, @type;
+------+--------+
&#124; @num &#124; @type  &#124;
+------+--------+
&#124; 2    &#124; cherry &#124; 
+------+--------+

I don&#039;t know how the heck you got 9 there.
Your article is excellent though.</description>
		<content:encoded><![CDATA[<p>This example is incorrect:</p>
<p>set @num=0, @type := &#8221;;<br />
select * from fruits<br />
where (@num := if(type = @type, @num + 1, 1))<br />
      and (@type := type)<br />
      and (@num &lt;= 2);<br />
Empty set (0.00 sec)</p>
<p>select @num, @type;<br />
+&#8212;&#8212;+&#8212;&#8212;&#8211;+<br />
| @num | @type  |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8211;+<br />
| 9    | cherry |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8211;+</p>
<p>On MySQL 5.1.41-3ubuntu12.10 the result is:</p>
<p>select @num, @type;<br />
+&#8212;&#8212;+&#8212;&#8212;&#8211;+<br />
| @num | @type  |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8211;+<br />
| 2    | cherry |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8211;+</p>
<p>I don&#039;t know how the heck you got 9 there.<br />
Your article is excellent though.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cabeza</title>
		<link>http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/#comment-19661</link>
		<dc:creator>Cabeza</dc:creator>
		<pubDate>Wed, 28 Sep 2011 07:17:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=280#comment-19661</guid>
		<description>Stop the search! Whatever is happening, the culprit for the behaviour above is down to Workbench (v5.2.7). The use of user vars does not seem to have any significant effect when executing the queries via command line. Thanks anyway.</description>
		<content:encoded><![CDATA[<p>Stop the search! Whatever is happening, the culprit for the behaviour above is down to Workbench (v5.2.7). The use of user vars does not seem to have any significant effect when executing the queries via command line. Thanks anyway.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cabeza</title>
		<link>http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/#comment-19658</link>
		<dc:creator>Cabeza</dc:creator>
		<pubDate>Tue, 27 Sep 2011 13:15:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=280#comment-19658</guid>
		<description>I see the original post appears rather old, so maybe the thread is dead, but just in case...

I found your blog when searching for an odd side-effect of using user variables in (rather simple) queries -they get very slow.

As way of example, we join a couple of tables, one of them with about a million recs the other one &lt; 300 recs.

Here is the original version:

set @version = &#039;100952&#039;;
select j.wo, t.wo  from
(
    select distinct wo from t1.details where version = @version and reg_id &#039;foo&#039;
) t
right join table2.wo2 j on locate(j.wo,t.wo);

This takes about 12 secs, now, if I get rid of the set @version...

select j.wo, t.wo  from
(
    select distinct wo from t1.details where version = &#039;100952&#039; and reg_id &#039;foo&#039;
) t
right join table2.wo2 j on locate(j.wo,t.wo);
This takes &lt; 1 sec!  

These are InnoDBs on a 5.1 server on an XP SP3 machine 2GHz 2GB that pretty much only hosts the MySQL server. 

I can repeat these results at anytime. On more complex queries I get similar improvements when getting rid of the user vars. What is it with these things? 
Loved the your post btw. Thanks!</description>
		<content:encoded><![CDATA[<p>I see the original post appears rather old, so maybe the thread is dead, but just in case&#8230;</p>
<p>I found your blog when searching for an odd side-effect of using user variables in (rather simple) queries -they get very slow.</p>
<p>As way of example, we join a couple of tables, one of them with about a million recs the other one &lt; 300 recs.</p>
<p>Here is the original version:</p>
<p>set @version = &#039;100952&#039;;<br />
select j.wo, t.wo  from<br />
(<br />
    select distinct wo from t1.details where version = @version and reg_id &#8216;foo&#8217;<br />
) t<br />
right join table2.wo2 j on locate(j.wo,t.wo);</p>
<p>This takes about 12 secs, now, if I get rid of the set @version&#8230;</p>
<p>select j.wo, t.wo  from<br />
(<br />
    select distinct wo from t1.details where version = &#8217;100952&#8242; and reg_id &#8216;foo&#8217;<br />
) t<br />
right join table2.wo2 j on locate(j.wo,t.wo);<br />
This takes &lt; 1 sec!  </p>
<p>These are InnoDBs on a 5.1 server on an XP SP3 machine 2GHz 2GB that pretty much only hosts the MySQL server. </p>
<p>I can repeat these results at anytime. On more complex queries I get similar improvements when getting rid of the user vars. What is it with these things?<br />
Loved the your post btw. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Raynham</title>
		<link>http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/#comment-19507</link>
		<dc:creator>Mike Raynham</dc:creator>
		<pubDate>Sat, 23 Jul 2011 11:17:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=280#comment-19507</guid>
		<description>Thanks for a great article.  You can use your technique if you want to reset the variables in a single query, rather than creating two statements.  This might be useful for ensuring that the variables are reset each time the query is run, rather than having to remember to do two queries.

Instead of:

set @num := 0, @type := &#039;&#039;;
select ...

You can use a UNION to do both in a single query:

select
  null as type,
  null as variety,
  null as price,
  null as num
from dual where ((@num := 0) and (@type := &#039;&#039;))
union
select ...

The WHERE clause creates an &quot;Impossible WHERE&quot;, so it doesn&#039;t return any results, but it does reset the variables each time the query is run.

The alternative is to tag an extra SELECT after the FROM:

from fruits, (select @num := 0, @type := &#039;&#039;) vars

But I think this creates a subquery, which is what you are trying to avoid in the first place.</description>
		<content:encoded><![CDATA[<p>Thanks for a great article.  You can use your technique if you want to reset the variables in a single query, rather than creating two statements.  This might be useful for ensuring that the variables are reset each time the query is run, rather than having to remember to do two queries.</p>
<p>Instead of:</p>
<p>set @num := 0, @type := &#8221;;<br />
select &#8230;</p>
<p>You can use a UNION to do both in a single query:</p>
<p>select<br />
  null as type,<br />
  null as variety,<br />
  null as price,<br />
  null as num<br />
from dual where ((@num := 0) and (@type := &#8221;))<br />
union<br />
select &#8230;</p>
<p>The WHERE clause creates an &#8220;Impossible WHERE&#8221;, so it doesn&#8217;t return any results, but it does reset the variables each time the query is run.</p>
<p>The alternative is to tag an extra SELECT after the FROM:</p>
<p>from fruits, (select @num := 0, @type := &#8221;) vars</p>
<p>But I think this creates a subquery, which is what you are trying to avoid in the first place.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michel Kogan</title>
		<link>http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/#comment-19491</link>
		<dc:creator>Michel Kogan</dc:creator>
		<pubDate>Fri, 15 Jul 2011 20:37:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=280#comment-19491</guid>
		<description>Thanks dude, great post, it solves my problem :-)</description>
		<content:encoded><![CDATA[<p>Thanks dude, great post, it solves my problem :-)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

