You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Landry Soules <la...@gmail.com> on 2014/07/22 11:51:26 UTC

Which view is better optimized ?

Hello,

I have a simple users document containing fields such as gender, age and
last seen date.
I have to request these documents by age, or gender, or last seen date or a
mix of these criteria.
My question is : is it more efficient to create a single view containing
several emit, for example:
emit([doc.age, doc.gender, doc.lastSeen], doc);
emit([doc.gender, doc.age, doc.lastSeen], doc);
or to create several views, each of them containing one emit statement ?

Thanks.

Re: Which view is better optimized ?

Posted by Matthieu Rakotojaona <ma...@gmail.com>.
Hello,

Excerpts from Landry Soules's message of 2014-07-22 11:51:26 +0200:
> emit([doc.age, doc.gender, doc.lastSeen], doc);
> emit([doc.gender, doc.age, doc.lastSeen], doc);

Unless you know what you're doing, you shouldn't emit the full doc as a
value. Views are optimized that values are copied, so it means that
_all_ your docs will be copied, once per view. The trick is that at
query time, there is a special parameter you can pass to the GET [0]:

    Query Parameters:
      [...]
      include_docs (boolean) – Include the associated document with each row. Default is false.
      [...]

so what you should emit is something like

    emit([doc.age, doc.gender, doc.lastSeen], null);
    emit([doc.gender, doc.age, doc.lastSeen], null);

and use "&include_docs=true" when you query the view, so that you
effectively get what you initially wanted to emit without wasting space.

Regarding your question, you fall in the field of multi-dimensional
queries, which Couchdb isn't suited for. The solution generally is to
use couchdb-lucene or even elasticsearch for something like that.

[0]
http://couchdb.readthedocs.org/en/latest/api/ddoc/views.html#get--db-_design-ddoc-_view-view

-- 
Matthieu Rakotojaona

Re: Which view is better optimized ?

Posted by Jens Alfke <je...@couchbase.com>.
On Jul 22, 2014, at 9:45 AM, Mike Marino <mm...@gmail.com> wrote:

> Multiple views also allows you to split them across design documents, which
> means they can be built independently and in parallel.

On the other hand, if the views are re-indexed independently, then each task is reading the documents out of the database. Whereas if the views are in the same design document, the documents only get read once and then passed to each view’s map function. I would have thought the latter would be faster, but it probably depends on details of the view engine that I don’t know about.

—Jens

Re: Which view is better optimized ?

Posted by Sebastian Rothbucher <se...@googlemail.com>.
Hi,

The smaller unit will save you lots of trouble, esp. when updating the code of large views

Cheers
   Sebastian

Von meinem iPhone gesendet

> Am 22.07.2014 um 19:02 schrieb Tito Ciuro <tc...@mac.com>:
> 
> Is it better then to define a view per design doc then? If I have say, 8 views for a given person design doc, would you place them in one single doc or break them into smaller units?
> 
> -- Tito
> 
> 
>> On Jul 22, 2014, at 9:45, Mike Marino <mm...@gmail.com> wrote:
>> 
>> Multiple views also allows you to split them across design documents, which
>> means they can be built independently and in parallel.
>> 
>> 
>>> On Tue, Jul 22, 2014 at 5:44 PM, Jens Alfke <je...@couchbase.com> wrote:
>>> 
>>> 
>>> On Jul 22, 2014, at 2:51 AM, Landry Soules <la...@gmail.com>
>>> wrote:
>>> 
>>>> My question is : is it more efficient to create a single view containing
>>>> several emit, for example:
>>>> emit([doc.age, doc.gender, doc.lastSeen], doc);
>>>> emit([doc.gender, doc.age, doc.lastSeen], doc);
>>>> or to create several views, each of them containing one emit statement ?
>>> 
>>> Creating several views is probably better, since each one will be slightly
>>> faster to search. Also, with one view you can easily get mixed up between
>>> different types of keys — for example, if you emit both doc.gender and
>>> doc.name as keys, and both are strings, then the “F” gender rows are
>>> going to appear in between the names that start with “E” and “F”, which
>>> sounds like a mess.
>>> 
>>> —Jens

Re: Which view is better optimized ?

Posted by Mike Marino <mm...@gmail.com>.
On Tue, Jul 22, 2014 at 7:02 PM, Tito Ciuro <tc...@mac.com> wrote:

> Is it better then to define a view per design doc then? If I have say, 8
> views for a given person design doc, would you place them in one single doc
> or break them into smaller units?
>

I think that probably heavily depends on your use case.  We have generally
split our views that are slightly slower to process into separate design
documents so that they may be run in parallel.  In the case of a query that
requires a (re)build (e.g. no stale=ok or update_after), then this also
means that we only have to wait for the one view to finish.

mgm

Re: Which view is better optimized ?

Posted by Tito Ciuro <tc...@mac.com>.
Is it better then to define a view per design doc then? If I have say, 8 views for a given person design doc, would you place them in one single doc or break them into smaller units?

-- Tito


> On Jul 22, 2014, at 9:45, Mike Marino <mm...@gmail.com> wrote:
> 
> Multiple views also allows you to split them across design documents, which
> means they can be built independently and in parallel.
> 
> 
>> On Tue, Jul 22, 2014 at 5:44 PM, Jens Alfke <je...@couchbase.com> wrote:
>> 
>> 
>> On Jul 22, 2014, at 2:51 AM, Landry Soules <la...@gmail.com>
>> wrote:
>> 
>>> My question is : is it more efficient to create a single view containing
>>> several emit, for example:
>>> emit([doc.age, doc.gender, doc.lastSeen], doc);
>>> emit([doc.gender, doc.age, doc.lastSeen], doc);
>>> or to create several views, each of them containing one emit statement ?
>> 
>> Creating several views is probably better, since each one will be slightly
>> faster to search. Also, with one view you can easily get mixed up between
>> different types of keys — for example, if you emit both doc.gender and
>> doc.name as keys, and both are strings, then the “F” gender rows are
>> going to appear in between the names that start with “E” and “F”, which
>> sounds like a mess.
>> 
>> —Jens

Re: Which view is better optimized ?

Posted by Mike Marino <mm...@gmail.com>.
Multiple views also allows you to split them across design documents, which
means they can be built independently and in parallel.


On Tue, Jul 22, 2014 at 5:44 PM, Jens Alfke <je...@couchbase.com> wrote:

>
> On Jul 22, 2014, at 2:51 AM, Landry Soules <la...@gmail.com>
> wrote:
>
> > My question is : is it more efficient to create a single view containing
> > several emit, for example:
> > emit([doc.age, doc.gender, doc.lastSeen], doc);
> > emit([doc.gender, doc.age, doc.lastSeen], doc);
> > or to create several views, each of them containing one emit statement ?
>
> Creating several views is probably better, since each one will be slightly
> faster to search. Also, with one view you can easily get mixed up between
> different types of keys — for example, if you emit both doc.gender and
> doc.name as keys, and both are strings, then the “F” gender rows are
> going to appear in between the names that start with “E” and “F”, which
> sounds like a mess.
>
> —Jens

Re: Which view is better optimized ?

Posted by Jens Alfke <je...@couchbase.com>.
On Jul 22, 2014, at 2:51 AM, Landry Soules <la...@gmail.com> wrote:

> My question is : is it more efficient to create a single view containing
> several emit, for example:
> emit([doc.age, doc.gender, doc.lastSeen], doc);
> emit([doc.gender, doc.age, doc.lastSeen], doc);
> or to create several views, each of them containing one emit statement ?

Creating several views is probably better, since each one will be slightly faster to search. Also, with one view you can easily get mixed up between different types of keys — for example, if you emit both doc.gender and doc.name as keys, and both are strings, then the “F” gender rows are going to appear in between the names that start with “E” and “F”, which sounds like a mess.

—Jens

Re: Which view is better optimized ?

Posted by la...@gmail.com.
Thanks a lot guys for your very useful input. And as suggested Matthieu, I 
will probably have a look at lucene or elasticsearch.