问题
CouchDB's map functions emit key/value pairs:
function(doc) {
emit(doc.date, 1);
}
Potentially, there could be many key/value pairs with the same key. Setting group=true while querying a view groups key/value pairs with the same key into the same reduce:
function(keys, values, rereduce) {
return sum(values);
}
Does this mean that with group=true (or for any group_level > 0), there will be exactly one reduce per key?
Or does the grouping only guarantee that all reduces will have homogeneous keys, and that there could still be one or more rereduces?
I am working with a reduce function that is not commutative, but which will not have a large number of records per key. I was hoping that I would be able to set group=true and then control the order of operation within a single reduce. If there will be rereduces, then that plan does not make sense.
回答1:
group=true roughly means "Hey, Couch! Group this map in the way there all keys will be distinct, but don't miss any case of them!" and actually equals to group_level=999 (see docs).
While you may not guess with proper group_level and strip some key items (if key is an array it makes sense), group takes care of this for you and rereduce wouldn't be applied.
Also, your reduce function could be replaced with the built-in _sum - it's implemented in Erlang and is much faster.
来源:https://stackoverflow.com/questions/13338799/does-couchdbs-group-true-prevent-rereduce