False queries and optimizer
Posted in 1993
Is there any way to slip fake queries past the online5 optimizer?
E.g.,
select * from foo where field1=42
works fine, uses the index on field1, while
select * from foo where 1=0 or field1=42
has the optimzer doing a sequential scan on all of foo...
(As for "why do I care", I had a simple piece of code to build a query,
e.g. [psuedocode]:
q = "select * from foo where 1=0"
if (thing1)
q = q + " or field1 = " + value1
if (thing2)
q = q + " or field2 = " + value2
...
etc. Without the leading "1=0" it gets painful to decide if each 'if' should
start with "or" or not. The same problem arises with 'and', so it's not
as simple as switching to a bunch of unions, and also in this specific case
it would be ugly to use unions, since all of that combinations is further
conjoined with some other logic (e.g., "select * from foo where field9=42
and ..otherstuff.. and (1=0 or....). But it did fail in the above simple
case, and also with "and 1=1".)
Does the optimizer not recognize simple optimizations? Ought it not "guess"
that this has probability 0?
--
Andrew Burt aburt@du.edu
"But if he was dying he wouldn't bother to carve "Aaaaargh", he'd just say it."