Hi Radhika,
You're right, _stats doesn't return the average directly, but it returns
e.g. the following JSON information:
...
{sum: 33772.231, count: 1344, min: 25.064, max: 25.166, sumsqr:
848634.685281}
...
You can calculate the average in user software with sum/count.
I'm am pretty sure you will find that even with this minor requirement
(performing a single calculation on the user side), _stats will give you a
noticeable overall performance improvement. Even if you absolutely *must*
return an average in your reduce function, you have to take care of the
case when rereduce is run as well as track the total count, e.g.:
"reduce" : "function(keys, values, rereduce) {
if (!rereduce) {
var length = values.length;
return [sum(values) / length, length];
} else {
var length = sum(values.map(function(v){return v[1]}));
var avg = sum(values.map(function(v){
return v[0] * (v[1] / length) }));
return [avg, length];
}
}"
On Thu, May 22, 2014 at 3:39 PM, Ramanadham, Radhika <
[email protected]> wrote:
> Yes, the values are numeric.
>
> -----Original Message-----
> From: Simon Metson [mailto:[email protected]]
> Sent: Thursday, May 22, 2014 3:45 AM
> To: [email protected]
> Subject: Re: running views return nothing with couchdb1.5.1
>
> And make sure your values are numeric - you can't sum "20" but you can sum
> 20.
>
>
> On Thursday, 22 May 2014 at 02:21, Manokaran K wrote:
>
> > On Thu, May 22, 2014 at 6:27 AM, Ramanadham, Radhika
> > <[email protected]> wrote:
> > > Hi Mike,
> > >
> > > Do, for one of the views, I need to return an average.
> > >
> > > So, my reduce function had -
> > >
> > > "cpu": {
> > > "map": "function(doc) { if ((doc.type == 'performance_stats'))
> > > emit(doc.test_id, doc.CPU) }",
> > > "reduce": "function(keys, values) "
> > > "{ "
> > > "avg = Math.round(sum(values)/values.length);"
> > > "return(avg)"
> > > " }"
> > >
> > > Now, if I want to use stats, which give me sum and count, I tried
> this, but doesn't work. What am I doing wrong?
> > >
> > > "cpu": {
> > > "map": "function(doc) { if ((doc.type == 'performance_stats'))
> > > emit(doc.test_id, doc.CPU) }",
> > > "reduce": "function(keys, values) "
> > > "{ "
> > > "avg = Math.round(_sum/_count);"
> > > "return(avg)"
> > > " }"
> > > -Radhika
> > >
> >
> >
> > Try this:
> >
> > "reduce": "_stats"
> >
> > Cheers,
> > mano
> >
> >
>
>
>