tags:

views:

227

answers:

4

Hi, I've got a funny question for you, sql gurus out there (or maybe inhere?).

I got this habit when I need to construct a query at runtime. In order to avoid multiple 'if' checks I just go and write 'where true '. Then the rest is another part of the clause in the form, of course of ' and true ...' and continues with useful filtering.

My question is: do those 'where true and true' affect query performance? Right, best answer is to make some tests, but out of your experience can what can you say about this approach?

By the way, the sql engine that I am particularly interested is MySql.

+2  A: 

we have sql-server, but we use for this kind of "not programming ifs" (if there is little time) 1=1 as expression, and there was no perf-impact we could meassure.

karlis
+1  A: 

I think the MySQL query optimizer gets rid of where true and true stuff.

Karsten
A: 

Shouldn't. I've done this many times in SQL Server and it has no effect on the execution plan. However, I would not leave this in production code.

Cade Roux
A: 

Thanks a bunch! Great crowd. I may stop by again soon :).