dongzhuo6137 2014-03-24 00:20
浏览 13
已采纳

哪种方式在mysql上存储统计信息更有效?

I have an statistics database for my website visitors and i need to find which is the most efficient way to store the data:

I came across to 2 options.

Option Number 1:

+-----------------------------+
+ date        | country       +
+-----------------------------+
+  2014-03-20 | US            +
+  2014-03-20 | US            +
+  2014-03-20 | NL            +                 
+  2014-03-21 | US            +
+-----------------------------+

And then get the visitors data with Select count(*) from table

Option number 2:

+-----------------------------+
+ date        | country | visits+
+-----------------------------+
+  2014-03-20 |     US | 2    +
+  2014-03-20 |     NL | 1    +
+  2014-03-21 |     US | 1    +
+-----------------------------+

And then get the visitors data with Select sum(visits) from table

Which do you think is the best option? or is there a better option yet? Please have in mind this in case it has a huge amount of traffic.

Thank you very much in advance.

  • 写回答

1条回答 默认 最新

  • duanrong5167 2014-03-24 00:25
    关注

    Obviously, the more efficient way would be to use the second option with the count. Multiple rows are rolled up into a single row, just for the expense of a 4-byte integer. In general, having a table where multiple the rows are identical is inefficient.

    So, for querying purposes the second choice is going to be better.

    In terms of creating and maintaining the table, the first choice is more efficient. Inserting a new record is a less expensive operation than updating an existing row. This probably isn't a big deal, but there might be situations where this make a difference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题