Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dummy where clauses effects on performance

What is the effect of having WHERE 1=1 if you write this request in a script (pseudocode) :

sql = "SELECT f1,f2,f3 FROM t
       WHERE 1=1" ++ restOfTheClause

Where restOfTheClause can be a list of field-names/operators/values concatenated with a AND :

restOfTheClause = [('f4','>',5), ('f5','IN(1,2,3)'), ('f10','=',1)].map(writeWherePart).join(' AND ')

Writing WHERE 1=1 allows to write simpler code because you don't have to check if your restOfTheClause is empty or not, etc.

What is the effect on performance with this clause ? What is the difference between having WHERE 1=1 and having no where clause at all ?

I work on Oracle since a couple of months and i found i have to put WHERE 1=1 but other RDBMS accept WHERE true. As = is an operator, is there any performance differences between oracle and other RDBMS in using dummy clauses ?

Thank you

like image 285
lud Avatar asked Dec 12 '13 09:12

lud


2 Answers

SQL Server and oracle are sophisticated enough to completely delete this clause. It is deleted as a simplification step. It has no perf impact whatsoever. I don't know about other RDBMS'es.

like image 142
usr Avatar answered Sep 19 '22 01:09

usr


The optimiser is smart enough to realize that 1=1 needs to be computed only once, so the performance impact is negligible.

like image 28
Klas Lindbäck Avatar answered Sep 17 '22 01:09

Klas Lindbäck