> I am having problem using the operator OR. when I test for condition A
> OR B in a query, the query becomes extensively slow or even fails,
> even though a test on condition A or B alone gives result right away.

I learned recently that you can often use a UNION to solve problems like this.

It's true that even with indexes (if you aren't using indexes already, you should try that first) OR queries can be slow.

But you can often rewrite a query like this:

SELECT Row FROM MyTable WHERE MyColumn = 'value1' or MyColumn = 'value2';

as:

(SELECT Row FROM MyTable WHERE MyColumn = 'value1') UNION (SELECT Row FROM MyTable WHERE MyColumn = 'value2')

...and get very fast, indexed results that are otherwise identical.

It's also worth noting that you can take an ORDER BY on the end of UNION to order both sets as one. Like so:

SELECT Row FROM MyTable WHERE MyColumn = 'value1' or MyColumn = 'value2' ORDER BY Row;

becomes:

(SELECT Row FROM MyTable WHERE MyColumn = 'value1') UNION (SELECT Row FROM MyTable WHERE MyColumn = 'value2') ORDER BY Row;


I recently was able to speed up several queries in my application using these UNIONs in place of ORs.


Good Luck.

-bill


-- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]



Reply via email to