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?
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
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments