Conditional counting: Performance differences in using SUM() vs COUNT()?

前端 未结 4 1830
隐瞒了意图╮
隐瞒了意图╮ 2021-01-18 10:38

Just as a very simple example, let\'s say I have table test with sample data like so:

a     |     b      
-------------
1     |    18
1     |            


        
4条回答
  •  佛祖请我去吃肉
    2021-01-18 11:14

    Personally, I would use

    select a, count(b)
      from test
     where b < 50
     group by a
    

    Clear, concise and according to this SQL fiddle a tiny bit quicker than the others (needs less data according to the execution plan, though with a table that small you won't notice a difference):

提交回复
热议问题