sql查询性能使用有点奇怪的where子句

时间:2021-08-25 10:17:04

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

我有一个有趣的问题,那里的sql大师(或者可能在哪里?)。

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.

当我需要在运行时构造一个查询时,我养成了这个习惯。为了避免多次'if'检查,我只是去写'where true'。然后其余部分是形式中的条款的另一部分,当然是“真实......”并继续进行有用的过滤。

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.

顺便说一句,我特别感兴趣的SQL引擎是MySql。

3 个解决方案

#1


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.

我们有sql-server,但我们使用这种“不编程ifs”(如果时间很少)1 = 1作为表达式,并且没有任何影响,我们可以放心。

#2


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

我认为MySQL查询优化器摆脱了真实和真实的东西。

#3


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.

不应该。我在SQL Server中已经多次这样做了,它对执行计划没有影响。但是,我不会将其留在生产代码中。

#1


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.

我们有sql-server,但我们使用这种“不编程ifs”(如果时间很少)1 = 1作为表达式,并且没有任何影响,我们可以放心。

#2


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

我认为MySQL查询优化器摆脱了真实和真实的东西。

#3


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.

不应该。我在SQL Server中已经多次这样做了,它对执行计划没有影响。但是,我不会将其留在生产代码中。