对于以下文档
{
"_id" : ObjectId("511b7d1b3daee1b1446ecdfe"),
"l_linenumber" : 1,
"l_quantity" : 17,
"l_extendedprice" : 21168.23,
"l_discount" : 0.04,
"l_tax" : 0.02,
"l_returnflag" : "N",
"l_linestatus" : "O",
"l_shipdate" : ISODate("1996-03-13T03:00:00Z"),
"l_commitdate" : ISODate("1996-02-12T03:00:00Z"),
"l_receiptdate" : ISODate("1996-03-22T03:00:00Z"),
"l_shipinstruct" : "DELIVER IN PERSON",
"l_shipmode" : "TRUCK",
"l_comment" : "blithely regular ideas caj",
}
我尝试了两个类似的 map reduce 函数:首先
db.runCommand({
mapreduce: "lineitem",
query: {
l_shipdate: {'$gte': new Date("Jan 01, 1994")},
l_shipdate: {'$lt': new Date("Jan 01, 1995")},
l_discount: {'$gte':0.05},
l_discount: {'$lte':0.07},
l_quantity: {'$lt':24}
},
map : function Map() {
var revenue = this.l_extendedprice * this.l_discount;
emit("REVENUE", revenue);
},
reduce : function(key, values) {
return Array.sum(values);
},
out: 'query006'
});
第二
db.runCommand({
mapreduce: "lineitem",
map : function Map() {
var dataInicial = new Date("Jan 1, 1994");
var dataFinal = new Date("Jan 1, 1995");
if( this.l_discount>=0.05 && this.l_discount<=0.07 && this.l_quantity<24 && this.l_shipdate>=dataInicial && this.l_shipdate<dataFinal) {
var produto = this.l_extendedprice * this.l_discount;
emit("revenue", produto);
}
},
reduce : function(key, values) {
return Array.sum(values);
},
out: 'query006'
});
对我来说,这两个函数都是相等的,并且可能返回相同的结果。但只有第二个返回正确答案。
这些函数是我在 TPC-H 基准测试中转换 SQL 查询存在的尝试。查询显示在这里:
select
sum(l_extendedprice*l_discount) as revenue
from
lineitem
where
l_shipdate >= date '1994-01-01'
and l_shipdate < date '1994-01-01' + interval '1' year
and l_discount between 0.06 - 0.01 and 0.06 + 0.01
and l_quantity < 24;
为什么当我在第一个函数中使用查询语句时,结果大于正确答案?功能真的一样吗?