Is there is any Performance issue while using ISNULL() in SQL Server?

后端 未结 8 1776
陌清茗
陌清茗 2020-12-15 06:51

I am using ISNULL in MS SQl server 2008, since my table is too huge, whether using of ISNULL may cause any thing to the performance ?.

Than

相关标签:
8条回答
  • 2020-12-15 07:25

    Yes there is a performance issue afaik, in SQL Server Studio 2012.

    The problem is quite glaring when I used ISNULL in combination with OVER. After optimizing (i.e. putting ISNULL in the sub-query I am using OVER on) run time reduced from (estimated) 25.2 hours down to 102 seconds.

    My guess is ISNULL is OK when you run it over an entire column (for example, in a plain-ol' SELECT). But when you run it with OVER it is called anew every time, thus dragging down performance.

    Not ready to drill down further. Simply putting it here for others' reference.

    0 讨论(0)
  • 2020-12-15 07:32

    ISNULL() in the select-clause has neglible influence on the performance. In the where-clause on the other hand it can have a very huge impact on performance, since it prevents the optimizer for using an index on that column.

    where isnull(col1, 0) = 0 -- unable to use index, because every 
                              -- row has to be evaluated
    
    where col1 = isnull(@myVar, 0) -- index will be used, since isnull(@myVar, 0) 
                                   -- returns the same static value for every row and 
                                   -- not every row has to be evaluated by the function.
    

    So, when using isnull() in a where-clause, evaluate if it prevents the query optimizer from using an index. If so, consider creating a computed column with the result if isnull(col1, 0) and index the computed column and use it in your where-clause.

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