Comments on: Advanced MySQL user variable techniques http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/ Stay curious! Fri, 10 May 2013 18:25:19 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Xaprb http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/#comment-19983 Xaprb Thu, 12 Apr 2012 14:38:58 +0000 http://www.xaprb.com/blog/?p=280#comment-19983 Glad to hear it!

]]>
By: Gruff Davies http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/#comment-19982 Gruff Davies Thu, 12 Apr 2012 12:55:14 +0000 http://www.xaprb.com/blog/?p=280#comment-19982 Thanks SO much for posting this and your other posts on user-variables for limiting result sets. The fact that you’ve exposed your methods has been invaluable to me in solving a really tricky problem (a moving average in a timeline report).

In the end, I had to use the select-from-a-select method to get the variables to calculate correctly as I’m doing a cross product with a calendar table which messes the evaluation time up completely. but at least I now have a working query!

]]>
By: Ian http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/#comment-19847 Ian Sun, 22 Jan 2012 15:54:05 +0000 http://www.xaprb.com/blog/?p=280#comment-19847 This example is incorrect:

set @num=0, @type := ”;
select * from fruits
where (@num := if(type = @type, @num + 1, 1))
and (@type := type)
and (@num <= 2);
Empty set (0.00 sec)

select @num, @type;
+——+——–+
| @num | @type |
+——+——–+
| 9 | cherry |
+——+——–+

On MySQL 5.1.41-3ubuntu12.10 the result is:

select @num, @type;
+——+——–+
| @num | @type |
+——+——–+
| 2 | cherry |
+——+——–+

I don't know how the heck you got 9 there.
Your article is excellent though.

]]>
By: Cabeza http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/#comment-19661 Cabeza Wed, 28 Sep 2011 07:17:53 +0000 http://www.xaprb.com/blog/?p=280#comment-19661 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.

]]>
By: Cabeza http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/#comment-19658 Cabeza Tue, 27 Sep 2011 13:15:52 +0000 http://www.xaprb.com/blog/?p=280#comment-19658 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 < 300 recs.

Here is the original version:

set @version = '100952';
select j.wo, t.wo from
(
select distinct wo from t1.details where version = @version and reg_id ‘foo’
) 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 = ’100952′ and reg_id ‘foo’
) t
right join table2.wo2 j on locate(j.wo,t.wo);
This takes < 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!

]]>