我需要存储我的网站的统计信息。
我需要这些
1: I can query how many hits in day, week month, last month, etc based on a category.
2: Then I can sort stats according to countries, day, pages etc.
我知道这些方法可以做到
表all_stats
| category | page_url | country | date |.......
| x | index.php | US | timestamp |.......
| y | index2.php | UK | timestamp |.......
.
.
然后我可以查询为
SELECT * FROM all_stats WHERE category=x AND date<=today AND date>=last week
This works fine but problem with this is
as database increase query takes a lot of time to execute.
要解决查询速度,请从其他表格中存储基于国家/地区的每日统计信息,其中包含 256 个国家/地区和岛屿等
表 daily_stats
| category | page_url | US | UK |.......| date |
| x | index.php | 1000 | 1500 |.......| 1st |
| y | index2.php | 1500 | 2000 |.......| 2nd |
.
.
然后我可以像
SELECT
SUM(US) AS us
SUM(Uk) AS uk
.
. //all 256 countries and island etc
.
.
WHERE category=x AND date<=today AND date>=last week
But this will be a big table with many columns
我看到的另一个优点是
创建表smart_stats
| category | page_url | country_array .......| date |
| x | index.php | US=>1000, UK=1500 .......| 1st |
| y | index2.php | US=>1500, UK=2000 .......| 2nd |
.
.
This can be smart approach if I can add all country stats in some way
请建议一种从表格smart_stats country_array进行查询的方法,我可以以我喜欢的任何方式进行排序
或者建议你认为最好的方法
谢谢
您的all_stats
结构可能没问题。 此查询:
SELECT *
FROM all_stats
WHERE category = x AND date <= today AND date >= last week
可以从指数中受益。 我实际上会推荐两个:(category, date)
和(date)
。 第一个应该大大加快查询速度。 第二个处理不查找特定类别的查询。
如果您通常希望同时使用所有国家/地区,您可能会发现调整国家/地区会有所帮助。 它实际上减少了表格的绝对大小以及行数 - 如果每天代表所有或大多数国家/地区。 在查询包含数百列的表以及为新国家/地区维护表时会出现此问题(MySQL对表中的列数有限制,因此适用于国家/地区的内容可能不适用于省/州/市级别)。
您可以添加以下索引:
ALTER TABLE `all_stats`
ADD INDEX `categoryDate` (`category` ASC, `date` ASC) COMMENT '';