I have a CouchDB database with a view whose values are paired numbers of the form [x, y]. For documents with the same key, I need to (simultaneously) calculate the minimum x and maximum y. The database in which I work contains about 50,000 documents. Building a look takes several hours, which seems somewhat excessive. (The keys themselves are three length arrays.) I show the map and reduce the functions below, but the main question is: how can I speed up this process?
Note that inline functions will not work, because the values must be numbers, not a length or two arrays. It is possible that I could make two different representations (one for min (x) and one for max (y)), but it is not clear to me how to combine them to get both results at the same time.
My current display function looks mostly like
function(doc) {
emit ([doc.a, doc.b, doc.c], [doc.x, doc.y])
}
and my reduction function looks like
function(keys, values) {
var x = null;
var y = null;
for (i = 0; i < values.length; i++) {
if (values[i][0] == null) break;
if (values[i][1] == null) break;
if (x == null) x = values[i][0];
if (y == null) y = values[i][1];
if (values[i][0] < x) x = values[i][0];
if (values[i][1] > y) y = values[i][1];
}
emit([x, y]);
}
source
share