首页 文章

SQL Server使用ISNULL where子句优化了大型查询

提问于
浏览
3

我有以下大型选择查询,在08:15返回150万行 . 我需要优化查询,选择约290列,我不能减少列数,以提高速度 .

select Column1 ... Column290
from dob.table1
where (ISNULL(Column50, 1) = 1)

我已经读过 ISNULL 如果在 WHERE 子句中使用了性能成本,因为优化器不使用索引而是求扫描,对吗?

我试图弄清楚如何重写

WHERE (ISNULL(Column50, 1) = 1)

我尝试使用cte并设置

IP_Column50 = case when IP_Column50 is null then else IP_Column50 end

并将我的查询重写为

select * 
from cte 
where IP_Column50 = 1

但是CTE需要更长时间 .

我读到了这种方法

如果是这样,考虑创建一个带有结果的计算列,如果是isnull(col1,0)并索引计算列并在where子句中使用它

但我不确定如何实现这一点 . 优化此查询可以获得任何帮助 .

谢谢

3 回答

  • 1

    您也可以在单个查询中执行此操作 . 像这样 .

    select Column1 ... Column290
    from dob.table1
    where Column50 = 1
    OR Column50 IS NULL
    

    这有可能成为问题,因为这是一种捕获所有查询 . 如果您有多个标准需要检查,请查看此文章 .

    http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/

  • 1

    使用UNION

    select Column1 ... Column290 from dob.table1 where Column50 is null
    union
    select Column1 ... Column290 from dob.table1 where Column50 = 1
    
  • 2

    你可以做到

    select Column1 ... Column290
    from dob.table1
    where Column50 IS NULL OR (Column50 IS NOT NULL AND Column50 = 1)
    

    正如您所说的那样_12828275_,似乎在Where子句中使用ISNULL的效率低于使用IS NULL的效率 .

相关问题