对某列做sum 求和时, 0值和 null 哪个运算更快  ,
如果null值快就把0换成null, 如果0快就把null换成0,   
有谁做过这个测试? 

解决方案 »

  1.   

    顶一楼SUM 忽略 NULL
    最好把该字段 默认值 为0  不要NULL
      

  2.   

    sum 忽略null的 不过这个忽略也是需要sqlserver自动判定的 这样null不参与运算
    如果改成0 则都参与了运算 不知道哪个处理更好点 啊
      

  3.   

    NULL值在做算术运算时容易出问题,我一般把它转换为0
    至于效率问题,关注下.........
      

  4.   

    一般的话数据值不存在就应该用NULL,值为0就记录为0
    他们两个没什么可比性
      

  5.   


    drop table tb
    create table tb(a int, b int, c int)
    insert into tb values(1,2,3) 
    insert into tb values(2,2,1) 
    insert into tb values(3,3,2) 
    insert into tb values(4,3,3) 
    insert into tb values(5,2,4) 
    insert into tb values(6,2,5) 
    insert into tb values(7,4,null) 
    select b,max(c) as c from tb group by b
      

  6.   

    我通过测试,发现sum()忽略null的含义就是视其为0,而不是视其记录不存在,例子如下:
    有人说,sum()将null值的记录视为不存在,我想不是这样的:
    drop   table   tb 
    create   table   tb(a   int,   b   int,   c   int) 
    insert   into   tb   values(1,2,3)   
    insert   into   tb   values(2,2,1)   
    insert   into   tb   values(3,3,2)   
    insert   into   tb   values(4,3,3)   
    insert   into   tb   values(5,2,4)   
    insert   into   tb   values(6,2,5)   
    insert   into   tb   values(7,4,null)   
    insert   into   tb   values(8,4,60) 
    select b,count(1) as 记录数,max(c) as c 
    from  tb   
    group by b /*
    b           记录数         c           
    ----------- ----------- ----------- 
              2           4           5 
              3           2           3 
              4           2          60 (所影响的行数为 3 行)警告: 聚合或其它 SET 操作消除了空值。
      

  7.   

    当值为null时,聚合函数不会把他当做0的,而是视为不存在。
    这一点如果单看sum()函数看不出来。可以视为0,但是如果是avg(),max()函数,就能区别了,create table tb(
    a int,
        b int,
        c int
    )
     
    insert into tb values(2,2,1)   
    insert into tb values(3,3,2)   
    insert into tb values(4,3,3)   
    insert into tb values(5,2,4)   
    insert into tb values(6,2,5)   
    insert into tb values(7,4,null)   
    insert into tb values(8,4,-60) 
    select b,avg(c) as c平均 
    from  tb   
    group by b /*
    b           c平均         
    ----------- ----------- 
              2           3 
              3           2 
              4         -60 (所影响的行数为 3 行)
    */
      

  8.   

    下面是为0时的情况:
    drop table tb create table tb(
    a int,
        b int,
        c int
    )
     
    insert into tb values(2,2,1)   
    insert into tb values(3,3,2)   
    insert into tb values(4,3,3)   
    insert into tb values(5,2,4)   
    insert into tb values(6,2,5)   
    insert into tb values(7,4,0)   
    insert into tb values(8,4,-60) 
    select b,avg(c) as c平均 
    from  tb   
    group by b /*
    b           c平均         
    ----------- ----------- 
              2           3 
              3           2 
              4         -30 (所影响的行数为 3 行)
    */
      

  9.   

    可见,null与0,是有本质区别的。
      

  10.   

    --统计平均值是要注意的,其它的不会,包括取记录数
    create   table   tb(a   int,   b   int,   c   int) 
    insert   into   tb   values(1,2,3)   
    insert   into   tb   values(2,2,1)   
    insert   into   tb   values(3,3,2)   
    insert   into   tb   values(4,3,3)   
    insert   into   tb   values(5,2,4)   
    insert   into   tb   values(6,4,5)   
    insert   into   tb   values(7,4,null)   
    select   b,avg(c),count(*)   as   c   from   tb   group   by   b 
    drop table tb
    b                       c           
    ----------- ----------- ----------- 
    2           2           3
    3           2           2
    4           5           2(所影响的行数为 3 行)警告: 聚合或其它 SET 操作消除了空值。