[英]Classic average calculus using PHP versus Map and Reduce MongoDB
I need to calculate the averages for a chart. 我需要计算图表的平均值。
I have 15k rows in the database, my index is the time. 我的数据库中有15,000行,我的索引是时间。
I did it in two different way: 我以两种不同的方式做到了:
1) I repeat on the interval of time (for each interval) : - raw data request between the dates - average calculation in PHP for this interval 1)我在时间间隔上重复(每个时间间隔):-日期之间的原始数据请求-此间隔的PHP平均计算
2) Map and Reduce: for each interval the reduce function is counting the data, then in the finalize function I make the average. 2)映射和归约:对每个间隔,归约函数都会对数据进行计数,然后在finalize函数中求平均值。
m = function() {
var k = new Date(this.date);
k.setSeconds(0);
k.setMilliseconds(0);
emit(
k, {
note: this.note
}
);
}
r = function(key, values) {
var reduced = {
note:0,
count:0,
noteAvg:0,
};
values.forEach(function(val) {
reduced.note += val.note;
reduced.count += val.count;
});
return reduced;
}
f = function(key, reduced) {
reduced.noteAvg = reduced.note / reduced.count;
return reduced;
}
$data_graph = $this->db->command(array(
"mapreduce" => "notes",
"map" => $map,
"reduce" => $reduce,
"finalize" => $finalize,
"query" => $req,
"out" => array("inline"=>1)
));
The second solution is a lot of time slower than the first. 第二种解决方案比第一种解决方案要慢很多时间。 Why?
为什么? Should I try to use more data to compare?
我应该尝试使用更多数据进行比较吗?
I tried on MongoLab (free version) and with my local mongo server and nothing change. 我尝试了MongoLab(免费版),并使用了本地mongo服务器,没有任何变化。
Thanks :) 谢谢 :)
It sounds like you're mapping all data and filtering it out with reduce rather than restricting the query to the same subset your PHP query is getting. 听起来您正在映射所有数据并使用reduce过滤掉它,而不是将查询限制为您的PHP查询所获得的同一子集。
If you are not already doing so, add a {query:{}} parameter to your mapreduce call as documented here. 如果您尚未这样做,请按照此处所述在您的mapreduce调用中添加一个{query:{}}参数。
This will only pass the subset of documents satisfying the query to the map/reduce operation. 这只会将满足查询条件的文档子集传递给map / reduce操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.