SQL Server - ignoring always true condition

Mateusz

I need to add to the WHERE clause one condition, which is always true, but it must reference one of the columns, for example:

ID is the primary key (and therefore NOT NULL)

and I will execute a select:

SELECT *
FROM   Table 
WHERE  ID IS NOT NULL 

Will this condition be ignored or checked for every row?

Martin Smith

SQL Server can determine at compile time that this condition will always be true and avoid the need to check at runtime.

CREATE TABLE #T
(
ID INT CONSTRAINT PK_ID PRIMARY KEY NONCLUSTERED,
X INT CONSTRAINT UQ_X UNIQUE
)

SELECT X 
FROM #T 
WHERE ID IS NOT NULL;    

DROP TABLE #T

enter image description here

In the execution plan above the only index accessed is UQ_X and this doesn't even contain the ID column that would make such a runtime evaluation possible.

By contrast if ID is nullable (and replaced with a unique constraint rather than primary key as a PK wouldn't allow NULL) then the check would of course need to be made at run time and so the plan will need to retrieve the column and might look like one of the following.

Scans whole table with predicate pushed into scan

enter image description here

Attempt to use narrower index requires lookup to retrieve the column and evaluate the predicate

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Server - ignoring always true condition

From Dev

if condition always evaluating true

From Dev

Term for an if condition that is always true

From Dev

SQL Server : Insert into Where Condition is True

From Dev

My Method always return 0 ignoring the condition

From Dev

if condition always returns true (Java)

From Dev

Why is this PHP if condition always true?

From Dev

Why is this PHP if condition always true?

From Dev

awk condition always TRUE in a loop

From Dev

Bash Script if condition is always true

From Dev

Condition always true when reached in while loop

From Dev

Why does my condition always evaluate as true?

From Dev

Compiler: What if condition is always true / false

From Dev

Python - True part of if condition always executes

From Dev

C++ GUI Condition for if statement always true

From Dev

WIX Operating System Condition Always true

From Dev

Why a condition that contains a pointer always returns true?

From Dev

Why is my "if" condition (string comparison) always true?

From Dev

SQL - only execute 'and' if a condition is true

From Dev

SQL Filter rows if condition is true

From Dev

Search index in SQL Server ignoring special characters

From Dev

Search index in SQL Server ignoring special characters

From Dev

IN Condition Issue in SQL Server

From Dev

select with condition -Sql server

From Dev

SQL-Procedures: NOT EXISTS seems to be always true

From Dev

Intellij Idea hint: Condition is always false - can that be true here? (Java)

From Java

Boolean condition is always false when using `status == true`

From Java

Typescript error This condition will always return 'true' since the types have no overlap

From Dev

if condition in try block always turns out true even if it shouldn´t

Related Related

  1. 1

    SQL Server - ignoring always true condition

  2. 2

    if condition always evaluating true

  3. 3

    Term for an if condition that is always true

  4. 4

    SQL Server : Insert into Where Condition is True

  5. 5

    My Method always return 0 ignoring the condition

  6. 6

    if condition always returns true (Java)

  7. 7

    Why is this PHP if condition always true?

  8. 8

    Why is this PHP if condition always true?

  9. 9

    awk condition always TRUE in a loop

  10. 10

    Bash Script if condition is always true

  11. 11

    Condition always true when reached in while loop

  12. 12

    Why does my condition always evaluate as true?

  13. 13

    Compiler: What if condition is always true / false

  14. 14

    Python - True part of if condition always executes

  15. 15

    C++ GUI Condition for if statement always true

  16. 16

    WIX Operating System Condition Always true

  17. 17

    Why a condition that contains a pointer always returns true?

  18. 18

    Why is my "if" condition (string comparison) always true?

  19. 19

    SQL - only execute 'and' if a condition is true

  20. 20

    SQL Filter rows if condition is true

  21. 21

    Search index in SQL Server ignoring special characters

  22. 22

    Search index in SQL Server ignoring special characters

  23. 23

    IN Condition Issue in SQL Server

  24. 24

    select with condition -Sql server

  25. 25

    SQL-Procedures: NOT EXISTS seems to be always true

  26. 26

    Intellij Idea hint: Condition is always false - can that be true here? (Java)

  27. 27

    Boolean condition is always false when using `status == true`

  28. 28

    Typescript error This condition will always return 'true' since the types have no overlap

  29. 29

    if condition in try block always turns out true even if it shouldn´t

HotTag

Archive