简体   繁体   English

SUM(val / 2)或SUM(val)/ 2中哪个更好?

[英]Which of SUM(val/2) or SUM(val)/2 is better?

I presume that 我认为

SELECT SUM(val/2) FROM my_table

will compute the division N times (once per row) 将计算除法N次(每行一次)

SELECT SUM(val)/2 FROM my_table

will compute the SUM() and then the division only once 将计算SUM(),然后除法一次

Is that right? 那正确吗?
For any aggregate function? 对于任何聚合函数?

The second is better. 第二个更好。 Performance-wise the number of operations done is lesser than in the first case. 在性能方面,完成的操作数量少于第一种情况。 Just one division is done, while in the first N divisions are done. 仅完成一个分区,而在前N个分区中完成。

Secondly, the result of the second query is more "exact" than in the first, as just one approximation will be done when the sum is divided by 2. In the first query the approximated val/2 values are sum, and then the result approximated. 其次,第二个查询的结果比第一个查询更“精确”,因为当将总和除以2时将只执行一个近似。在第一个查询中,近似的val / 2值是和,然后是结果近似。

如果除以2,则这两种方法都是正确的;如果除以3或另一个数,则第一种方法是正确的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM