List:General Discussion« Previous MessageNext Message »
From:Martin Ramsch Date:August 16 1999 3:52pm
Subject:Re: AND statements
View as plain text  
On Mon, 1999-08-16 16:36:51 +0100, Clinton wrote:
> So does MySQL use "lazy evaluation" or not (ie , if the left hand
> side of the OR statement is true, then ignore the right hand side)
> 
> Because if it does, this would mean that Paul can prepare 1
> statement which works for all conditions rather than preparing a new
> statement for each query, or prepreparing potentially hundreds of
> combinations.

Sorry, I still can't answer this.

But why does it matter?  Even with lazy evaluation the complete SQL
statement would have to have proper syntax, so it's not possible to
write something like "(1=1) OR crap", hoping that MySQL won't complain
about the crap.

So the question is not about working or not working, "just" about
efficiency!
And that's something you easily can test yourself!

| mysql> SELECT BENCHMARK(1000000, 1=1 AND SIN(0.5)>0.5);
| [...]
| 1 row in set (3.62 sec)
| 
| mysql> SELECT BENCHMARK(1000000, 1=0 AND SIN(0.5)>0.5);
| [...]
| 1 row in set (1.18 sec)
| 
| mysql> SELECT BENCHMARK(1000000, 1=0 OR SIN(0.5)>0.5);
| [...]
| 1 row in set (3.74 sec)
| 
| mysql> SELECT BENCHMARK(1000000, 1=1 OR SIN(0.5)>0.5);
| [...]
| 1 row in set (1.21 sec)

So, yes!  MySQL seems to do lazy evaluation.
But I recommend that you do your own testing, too.

Regards,
  Martin
-- 
Martin Ramsch <m.ramsch@stripped> <URL: http://home.pages.de/~ramsch/ >
PGP KeyID=0xE8EF4F75 FiPr=52 44 5E F3 B0 B1 38 26  E4 EC 80 58 7B 31 3A D7
Thread
AND statementsPaul16 Aug
  • Re: AND statementsMartin Ramsch16 Aug
  • Re: AND statementsRuben I Safir16 Aug
RE: AND statementsPaul16 Aug
RE: AND statementsClinton16 Aug
  • Re: AND statementsMartin Ramsch16 Aug
RE: AND statementsPaul16 Aug
  • RE: AND statementsMichael Widenius21 Aug