Unix Technical Forum

Dynamic ORing

This is a discussion on Dynamic ORing within the MySQL General forum forums, part of the MySQL category; --> Hello all, I'm trying to do some processing on the front end to optimize my query on the backend. ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:48 AM
Dave G
 
Posts: n/a
Default Dynamic ORing

Hello all,

I'm trying to do some processing on the front end to optimize my query on
the backend. I would like to generate a list of id's for this query like
so:

SELECT REPLACE('3,4,5,6,7,8,9',',',' OR element_id=') INTO @tmp;

Then use it like:

mysql> select @tmp;
+---------------------------------------------------------------------------------------------------+
| @tmp
|
+---------------------------------------------------------------------------------------------------+
| 3 OR element_id=4 OR element_id=5 OR element_id=6 OR element_id=7 OR
element_id=8 OR element_id=9 |
+---------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from display__Element where element_id=@tmp;
+------------+--------------+------------+
| element_id | display_name | data_units |
+------------+--------------+------------+
| 3 | Sync | |
+------------+--------------+------------+
1 row in set (0.00 sec)

mysql> select * from display__Element where element_id=3 OR element_id=4
OR element_id=5 OR element_id=6 OR element_id=7 OR element_id=8 OR
element_id=9;
+------------+---------------+------------+
| element_id | display_name | data_units |
+------------+---------------+------------+
| 3 | Sync | |
| 4 | Graph Samples | V |
| 5 | First E | V |
| 7 | Graph Sample | V |
| 8 | Test Graph | V |
+------------+---------------+------------+
5 rows in set (0.00 sec)

mysql>

The problem is that when I try to use a variable that is a string with
OR's contained, it only uses the first one. Anybody know what is going on
here?

David Godsey

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:48 AM
Baron Schwartz
 
Posts: n/a
Default Re: Dynamic ORing

Hi Dave,

Dave G wrote:
> Hello all,
>
> I'm trying to do some processing on the front end to optimize my query on
> the backend. I would like to generate a list of id's for this query like
> so:
>
> SELECT REPLACE('3,4,5,6,7,8,9',',',' OR element_id=') INTO @tmp;
>
> Then use it like:
>
> mysql> select @tmp;
> +---------------------------------------------------------------------------------------------------+
> | @tmp
> |
> +---------------------------------------------------------------------------------------------------+
> | 3 OR element_id=4 OR element_id=5 OR element_id=6 OR element_id=7 OR
> element_id=8 OR element_id=9 |
> +---------------------------------------------------------------------------------------------------+
> 1 row in set (0.00 sec)
>
> mysql> select * from display__Element where element_id=@tmp;


MySQL sees that element_id is a number and coerces @tmp into a number. Check the
output of 'show warnings' immediately after this statement. Another way to see what's
happening is to run 'select 0 + @tmp'. MySQL is truncating after the first non-digit
characters.

What you want to do is use a prepared statement; prepare the statement as
concat("select...", @tmp) and then execute the result.

> +------------+--------------+------------+
> | element_id | display_name | data_units |
> +------------+--------------+------------+
> | 3 | Sync | |
> +------------+--------------+------------+
> 1 row in set (0.00 sec)
>
> mysql> select * from display__Element where element_id=3 OR element_id=4
> OR element_id=5 OR element_id=6 OR element_id=7 OR element_id=8 OR
> element_id=9;


A better approach is to use an IN() list, which MySQL might even be able to optimize
better:

WHERE element_id IN(3,4,5,6,7,8,9);

DANGER: do NOT use an IN() subquery, of the form

WHERE element_id IN(select element_id from some_other_table)

MySQL optimizes this kind of query very poorly.

Baron
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:48 AM
Dave G
 
Posts: n/a
Default Re: Dynamic ORing

Thank you Baron, I'll give that a shot, and thanks for the IN() list tip
.... much appreciated.

Dave

> Hi Dave,
>
> Dave G wrote:
>> Hello all,
>>
>> I'm trying to do some processing on the front end to optimize my query
>> on
>> the backend. I would like to generate a list of id's for this query
>> like
>> so:
>>
>> SELECT REPLACE('3,4,5,6,7,8,9',',',' OR element_id=') INTO @tmp;
>>
>> Then use it like:
>>
>> mysql> select @tmp;
>> +---------------------------------------------------------------------------------------------------+
>> | @tmp
>> |
>> +---------------------------------------------------------------------------------------------------+
>> | 3 OR element_id=4 OR element_id=5 OR element_id=6 OR element_id=7 OR
>> element_id=8 OR element_id=9 |
>> +---------------------------------------------------------------------------------------------------+
>> 1 row in set (0.00 sec)
>>
>> mysql> select * from display__Element where element_id=@tmp;

>
> MySQL sees that element_id is a number and coerces @tmp into a number.
> Check the
> output of 'show warnings' immediately after this statement. Another way
> to see what's
> happening is to run 'select 0 + @tmp'. MySQL is truncating after the
> first non-digit
> characters.
>
> What you want to do is use a prepared statement; prepare the statement as
> concat("select...", @tmp) and then execute the result.
>
>> +------------+--------------+------------+
>> | element_id | display_name | data_units |
>> +------------+--------------+------------+
>> | 3 | Sync | |
>> +------------+--------------+------------+
>> 1 row in set (0.00 sec)
>>
>> mysql> select * from display__Element where element_id=3 OR element_id=4
>> OR element_id=5 OR element_id=6 OR element_id=7 OR element_id=8 OR
>> element_id=9;

>
> A better approach is to use an IN() list, which MySQL might even be able
> to optimize
> better:
>
> WHERE element_id IN(3,4,5,6,7,8,9);
>
> DANGER: do NOT use an IN() subquery, of the form
>
> WHERE element_id IN(select element_id from some_other_table)
>
> MySQL optimizes this kind of query very poorly.
>
> Baron
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 06:48 AM
Dave G
 
Posts: n/a
Default Re: Dynamic ORing

Prepared Statements with the IN function worked beautifully. Performance
is slightly degraded, but I get the functionality I want and can live with
the slight performance hit.

Thanks again.

Dave

> Hi Dave,
>
> Dave G wrote:
>> Hello all,
>>
>> I'm trying to do some processing on the front end to optimize my query
>> on
>> the backend. I would like to generate a list of id's for this query
>> like
>> so:
>>
>> SELECT REPLACE('3,4,5,6,7,8,9',',',' OR element_id=') INTO @tmp;
>>
>> Then use it like:
>>
>> mysql> select @tmp;
>> +---------------------------------------------------------------------------------------------------+
>> | @tmp
>> |
>> +---------------------------------------------------------------------------------------------------+
>> | 3 OR element_id=4 OR element_id=5 OR element_id=6 OR element_id=7 OR
>> element_id=8 OR element_id=9 |
>> +---------------------------------------------------------------------------------------------------+
>> 1 row in set (0.00 sec)
>>
>> mysql> select * from display__Element where element_id=@tmp;

>
> MySQL sees that element_id is a number and coerces @tmp into a number.
> Check the
> output of 'show warnings' immediately after this statement. Another way
> to see what's
> happening is to run 'select 0 + @tmp'. MySQL is truncating after the
> first non-digit
> characters.
>
> What you want to do is use a prepared statement; prepare the statement as
> concat("select...", @tmp) and then execute the result.
>
>> +------------+--------------+------------+
>> | element_id | display_name | data_units |
>> +------------+--------------+------------+
>> | 3 | Sync | |
>> +------------+--------------+------------+
>> 1 row in set (0.00 sec)
>>
>> mysql> select * from display__Element where element_id=3 OR element_id=4
>> OR element_id=5 OR element_id=6 OR element_id=7 OR element_id=8 OR
>> element_id=9;

>
> A better approach is to use an IN() list, which MySQL might even be able
> to optimize
> better:
>
> WHERE element_id IN(3,4,5,6,7,8,9);
>
> DANGER: do NOT use an IN() subquery, of the form
>
> WHERE element_id IN(select element_id from some_other_table)
>
> MySQL optimizes this kind of query very poorly.
>
> Baron
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:36 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com