简体   繁体   English

MySQL:如何优化字段 SUM 的结果?

[英]MySQL: How to optimize result of SUM of fields?

I have this query:我有这个查询:

SELECT name, SUM(count_1 + count_2 + count_3 + count_4 + count_5 + count_6) AS Total
FROM my_table

Is there a way to add these values count_1 + count_2 + count_3 + count_4 + count_5 + count_6 and so on.. more efficiently?有没有办法添加这些值count_1 + count_2 + count_3 + count_4 + count_5 + count_6等等..更有效? MySQL keeps crashing for me when I add huge numbers of fields.当我添加大量字段时,MySQL 不断崩溃。

Regardless of whether the db design is right or wrong if you use an aggregation function you should use group by如果您使用聚合 function ,则无论数据库设计是对还是错,您都应该使用 group by

SELECT name, SUM(count_1 + count_2 + count_3 + count_4 + count_5 + count_6) AS Total
FROM my_table
GROUP BY name

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

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