T-SQL 1=1 Performance Hit

前端 未结 8 736
谎友^
谎友^ 2020-12-06 04:33

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] = @para         


        
相关标签:
8条回答
  • 2020-12-06 05:27

    No, SQL Server is smart enough to omit this condition from the execution plan since it's always TRUE.

    Same is true for Oracle, MySQL and PostgreSQL.

    0 讨论(0)
  • 2020-12-06 05:30

    No performance hit. Even if your WHERE clause is loaded with a large number of comparisons, this is tiny.

    Best case scenario is that it's a bit-for-bit comparison. Worse case is that the digits are evaluated as integers.

    0 讨论(0)
提交回复
热议问题