You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Dan Santner <da...@me.com> on 2017/02/22 00:33:44 UTC

view problem when using array field items to map the view

we have docs that look like this:

_id:id,
seats:[#,#,#,#,#,#….]

and then we are doing a view that maps for each seat in the array.
function(doc) {
   if (doc.type == 'account') {
       doc.seats.map(function(seat) {
           delete doc.seats
           emit(seat.userID, doc))
       }
   }
}

when we get our seat count up to like 1k elements the view is a disaster to rebuild.  It pegs the cpu for like a minute.

so we changed this to just emit the id of the doc rather than the doc

function(doc) {
   if (doc.type == 'account') {
       doc.seats.map(function(seat) {
           delete doc.seats
           emit(seat.userID, doc._id))
       }
   }
}

and it rebuilds super fast again.

Would love to know if anyone has hit this before and can explain why it behaves this way???

Re: view problem when using array field items to map the view

Posted by Aurélien Bénel <au...@utt.fr>.
Hi Dan,

> when we get our seat count up to like 1k elements the view is a disaster to rebuild.  It pegs the cpu for like a minute. so we changed this to just emit the id of the doc rather than the doc (…)
> and it rebuilds super fast again.

As a general rule, consider your index as an expensive resource:
don’t put lengthy keys or values if  you don’t really need them. 

As a specific rule, when you need the doc you emitted the key from,
don’t send *any* value (it is already send in the rows as `_id`).

    emit(seat.userID);

Then call the view with `include_docs=true`.


See: http://docs.couchdb.org/en/2.0.0/couchapp/views/collation.html#examples


Regards,

Aurélien