简体   繁体   English

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

[英]Which way is more efficient to store stats on 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. 我遇到了2个选择。

Option Number 1: 选项编号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 然后Select count(*) from table获取访问者数据

Option number 2: 选项编号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 然后使用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. 提前非常感谢您。

Obviously, the more efficient way would be to use the second option with the count. 显然,更有效的方法是将第二个选项与count一起使用。 Multiple rows are rolled up into a single row, just for the expense of a 4-byte integer. 多行汇总为单行,仅以4字节整数为代价。 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. 这可能没什么大不了的,但是在某些情况下这会有所作为。

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

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