Does wrapping nullable columns in ISNULL cause table scans?

前端 未结 1 1905
孤城傲影
孤城傲影 2020-12-06 05:29

Code analysis rule SR0007 for Visual Studio 2010 database projects states that:

You should explicitly indicate how to handle NULL values in comparison

相关标签:
1条回答
  • 2020-12-06 06:24

    Yes it causes table scans. (though seems to get optimised out if the column isn't actually nullable)

    The SR0007 rule is extremely poor blanket advice as it renders the predicate unsargable and means any indexes on the column will be useless. Even if there is no index on the column it might still make cardinality estimates inaccurate affecting other parts of the plan.

    The categorization of it in the Microsoft.Performance category is quite amusing as it seems to have been written by someone with no understanding of query performance.

    It claims the rationale is

    If your code compares two NULL values or a NULL value with any other value, your code will return an unknown result.

    Whilst the expression itself does evaluate to unknown your code returns a completely deterministic result once you understand that any =, <>, >, < etc comparison with NULL evaluate as Unknown and that the WHERE clause only returns rows where the expression evaluates to true.

    It is possible that they mean if ANSI_NULLS is off but the example they give in the documentation of WHERE ISNULL([c2],0) > 2; vs WHERE [c2] > 2; would not be affected by this setting anyway. This setting

    affects a comparison only if one of the operands of the comparison is either a variable that is NULL or a literal NULL.

    Execution plans showing scans vs seek or below

    CREATE TABLE #foo
      (
         x INT NULL UNIQUE
      )
    
    INSERT INTO #foo
    SELECT ROW_NUMBER() OVER (ORDER BY @@SPID)
    FROM   sys.all_columns
    
    SELECT *
    FROM   #foo
    WHERE  ISNULL(x, 10) = 10
    
    SELECT *
    FROM   #foo
    WHERE  x = 10
    
    SELECT *
    FROM   #foo
    WHERE  x = 10
            OR x IS NULL 
    

    enter image description here

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