dongtang1910 2013-08-07 09:20
浏览 34
已采纳

在MySQL中明智的SUM VS COLUMN明智的SUM

I have a tableA this contains the following structure

enter image description here

I modified this structure into tableB like below to reduce number of rows and the category is fixed length

enter image description here

Assume I have 21 lakh data in tableA after modified into new structure tableB contains 70k rows only

In some case I want to SUM all the values into the table,

QUERY1: SELECT SUM(val) AS total FROM tableA;

vs

QUERY2: SELECT SUM(cate1+cate2+cate3) AS total FROM tableB;

QUERY1 is executing faster while comparing to QUERY2.

tableB contains less rows while comparing to tableA

As of my expectation QUERY2 is faster but QUERY1 is the fastest one.

Help me to understand why the performance is reduced in QUERY2?

  • 写回答

2条回答 默认 最新

  • dongqiang5541 2013-08-07 09:45
    关注

    MySQL is optimized to speed up relational operations. There is not so much effort at speeding up the other kinds of operations MySQL can perform. Cate1+Cate2+Cate3 is a perfectly legitimate operation, but there's nothing particularly relational about it.

    Table1 is actually simpler in terms of the relational model of data than Table2, even though Table1 has more rows. It's worth noting in passing that Table1 conforms to first normal form but Table2 does not. Those three columns are really a repeating group even though it's been made to look like they are not.

    So First Normal form is good for you in terms of performance (most of the time).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿
  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址