简体   繁体   中英

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)

SELECT SUM(val)/2 FROM my_table

will compute the SUM() and then the division only once

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.

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,则这两种方法都是正确的;如果除以3或另一个数,则第一种方法是正确的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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