我正在开发广告管理系统,最近,我们在高流量网站上推出了一些测试。 我将每次展示作为记录存储在数据库中,并带有时间戳,广告ID,区域ID和网站ID 。
我会选择日期范围之间的所有数据,并使用PHP来组织记录(按天等)。那效率很低,所以我做了两个查询
GROUP BY date(`impressions`.timestamp)
首先检索count
次展示,第二次查询获取相关标签列表。
我的问题是:
我需要查询不仅要获得整体展示次数,还要按地区和按天获得展示次数,而且我不能使用PHP来整理这一数据数据,因为它是数百万点。
SELECT `zones`.name as zoneName, `impressions`.*
from `impressions`, `zones`
WHERE `impressions`.website_id = "14"
AND `zones`.website_id="14"
AND `zones`.zone_id=`impressions`.zone_id
AND `impressions`.timestamp
BETWEEN "2012-10-08 00:00:00" AND "2012-10-15 23:59:59"
ORDER BY `impressions`.timestamp ASC
这可能会返回数百万条记录。然后我使用PHP将返回的数据排序到区域中。
例如,这是我的PHP排序后生成的数组。此数组表示他们在给定日期内收到的区域和展示次数。在处理大型数组时,使用哪种好的查询来尽量减少内存开销?
Array
(
[labels] => Array
(
[Oct 8th] => "Oct 8th"
[Oct 9th] => "Oct 9th"
[Oct 10th] => "Oct 10th"
)
[final] => Array
(
[0] => Array
(
[name] => Blog Skyscraper
[data] => Array
(
[0] => 449
[1] => 499
[2] => 558
)
)
[1] => Array
(
[name] => Latest News Right
[data] => Array
(
[0] => 805
[1] => 809
[2] => 760
)
)
[2] => Array
(
[name] => Photos Right
[data] => Array
(
[0] => 788
[1] => 786
[2] => 743
)
)
[3] => Array
(
[name] => Banner Zone
[data] => Array
(
[0] => 793
[1] => 796
[2] => 747
)
)
[4] => Array
(
[name] => Mini Right Bottom
[data] => Array
(
[0] => 784
[1] => 778
[2] => 742
)
)
[5] => Array
(
[name] => Mini Right Top
[data] => Array
(
[0] => 790
[1] => 787
[2] => 743
)
)
)
)