T-SQL 1=1 性能命中 [英] T-SQL 1=1 Performance Hit

查看:32
本文介绍了T-SQL 1=1 性能命中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的 SQL 查询,我通常对 SELECT 语句执行以下操作:

For my SQL queries, I usually do the following for SELECT statements:

SELECT ...
FROM table t
WHERE 1=1
  AND t.[column1] = @param1
  AND t.[column2] = @param2

如果我需要添加/删除/注释任何 WHERE 子句,这会很容易,因为我不必关心第一行.

This will make it easy if I need to add / remove / comment any WHERE clauses, since I don't have to care about the first line.

使用此模式时是否会影响性能?

Is there any performance hit when using this pattern?

附加信息:

sheepsimulator 和所有其他没有使用过的示例.

Example for sheepsimulator and all other who didn't get the usage.

假设上面的查询,我需要将@param1 更改为不包含在查询中:

Suppose the above query, I need to change @param1 to be not included into the query:

1=1:

...
WHERE 1=1 <-- no change
  --AND t.[column1] = @param1 <-- changed
  AND t.[column2] = @param2 <-- no change
...

如果没有 1=1:

...
WHERE <-- no change
  --t.[column1] = @param1 <-- changed
  {AND removed} t.[column2] = @param2 <-- changed
...

推荐答案

很可能如果您使用分析器并查看,您最终会发现优化器最终会经常忽略这一点,因此在宏伟的计划,可能不会有太多的性能增益或损失.

It is likely that if you use the profiler and look, you will end up seeing that the optimizer will end up ignoring that more often than not, so in the grand scheme of things, there probably won't be much in the way of performance gain or losses.

这篇关于T-SQL 1=1 性能命中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆