Although a couple of questions were already posted in SO about the difference between Over Partition By
and Group By
, I did not find a definitive conclusion about which performs better.
I set up a simple scenario at SqlFiddle, where Over (Partition By)
seems to boast a better execution plan (I am not much familiar with them, however).
Is the amount of data in the tables supposed to change this? Does Over (Partition By)
then ultimately performs better?
partition by
andgroup by
, but they're still two different operations with different use cases. And why do you consider the second execution plan to be better? Both have to do the same clustered index scans, but the second spends less relative time doing that, so it's obviously longer overall (about twice as long, just because of that distinct sort).group by
clearly wins. Performance is only important when it actually makes a difference - it's a cost calculation like any other; there's a cost to writing performance-centric code (usually, losing maintainability) and a cost to writing maintenance-centric code (usually, losing performance). Unless there's a strong reason to prefer performance (usually only for a tiny part of the whole application), maintainability is the way to go.