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

前端 未结 4 1776
隐瞒了意图╮
隐瞒了意图╮ 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 10:56

    Performance?

    Oh, the difference, if any, would be marginal, I'm sure. It would be nothing for me to worry about.

    How many other DBMSs they would work in?

    I've no doubt both would work in any major SQL product at least, so, again, this wouldn't be a matter of concern, not to me anyway.

    Clarity of statement?

    Certainly COUNT expresses it clearer that you want to count things, not to add up some arbitrary values. With SUM, you would realise the actual intention only upon reaching the THEN 1 part after skimming through the condition.

    Also, if I use COUNT I can omit the ELSE NULL part, because that's what is implied when ELSE is absent. If I omit ELSE 0 in the SUM expression, I may end up with a NULL result instead of the probably expected 0.

    On the other hand, there may be quite opposite situations where it would be more convenient to return NULL instead of 0 as a result of counting. So, if I used COUNT, I would have to do something like NULLIF(COUNT(CASE ...), 0), while with SUM(CASE ...) it would be just enough to leave out the ELSE clause. But even in that case I might still prefer the somewhat longer clarity to the slightly more obscure brevity (other things being equal).

    0 讨论(0)
  • 2021-01-18 10:57

    Whats wrong with a where clause:

    select a, count(b)
    from test
    where b < 50
    group by a
    
    0 讨论(0)
  • 2021-01-18 10:58

    With COUNT, you count the elements, using SUM you add numbers (positive, negative or zero) for a result that can be negative.

    0 讨论(0)
  • 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):

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