You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Piotr Zarzycki <pi...@gmail.com> on 2020/06/04 12:53:29 UTC

How to work with Indexes

Hello everyone,

We are building JS based application which storing data in CouchDb. It is a
greenfield application in case of front end and database structure. I would
use some examples at the beginning.

I have some selector which gets me data from db:

 const q = {
      selector: {
        historyId: document.historyId,
      },
      sort: [{ version: "desc" }],
      limit: 500,
    };


In the results I will get data along with warning:

"warning": "no matching index found, create an index to optimize query time"


I can easily get rid of that warning by adding using Fauxton index:

{
   "index": {
      "fields": [
         "historyId"
      ]
   },
   "name": "historyId-json-index",
   "type": "json"
}


My question is - How do you guys approach to working with indexes ? Should
I add it to each query which I'm doing ?

Thoughts about approaches would be much appreciated. :)

Thanks,
-- 

Piotr Zarzycki

Patreon: *https://www.patreon.com/piotrzarzycki
<https://www.patreon.com/piotrzarzycki>*

Re: How to work with Indexes

Posted by Piotr Zarzycki <pi...@gmail.com>.
Hi Garren,

Thanks for input. Very helpful. I will try to formalize and play a bit with
Explain feature to understand what actually it gives me.

Thanks,
Piotr

śr., 10 cze 2020 o 13:11 Garren Smith <ga...@apache.org> napisał(a):

> Hi Piotr,
>
> If you have the above two indexes. If you specify docType and Age. It will
> use the index that has docType and Age. If you only specify age it will use
> all_docs because you do not have a suitable index. If you select only
> doctype it will use the index with docType. The _explain endpoint will give
> you some ideas on how it works.
>
> What is nice about Mango is in the beginning you don't have to create any
> indexes. You can just use it. So while you are building an application
> using the Mango endpoints rather don't create indexes and rather figure out
> the type of queries you want to do. Once you are further along and you know
> exactly the type of queries you want to perform with it. Then create
> indexes.
>
> I hope that helps a bit.
>
> Cheers
> Garren
>
>
> On Wed, Jun 10, 2020 at 8:33 AM Piotr Zarzycki <pi...@gmail.com>
> wrote:
>
> > Hi ermouth,
> >
> > Many Thanks for your suggestion regarding Help feature. I will go with
> that
> > for sure!
> >
> > Piotr
> >
> > wt., 9 cze 2020 o 14:27 ermouth <er...@gmail.com> napisał(a):
> >
> > > > Just remember to ask the selector in the same order as they are
> indexed
> > >
> > > It doesn’t guarantee appropriate index will be used, however providing
> > > use_index param might help.
> > >
> > >
> >
> https://docs.couchdb.org/en/2.3.1/api/database/find.html?highlight=use_index
> > >
> > > ermouth
> > >
> > >
> > > вт, 9 июн. 2020 г. в 15:18, Andrea Brancatelli
> > > <ab...@schema31.it.invalid>:
> > >
> > > > As ermouth said the best approach is to use the explain feature of
> > > > Fauxton / Photon.
> > > >
> > > > Couch tries to guess what's the best index to use for any case so you
> > > > don't necessarily need to build complex indexes, if you ask for more
> > > > selectors than the indexed ones usually it picks an index and then
> scan
> > > > the documents to apply the subselectors.
> > > >
> > > > Yet I must admit I feel the logic behind it a bit entropic and
> > > > cumbersome.
> > > >
> > > > Just remember to ask the selector in the same order as they are
> > indexed,
> > > > even for partial indexes.
> > > >
> > > > Like a select for { fieldA, fieldB, fieldC } and an index for {
> fieldA,
> > > > fieldB } would work as you expect, but if the index was on { fieldA,
> > > > fieldC } it won't get used.
> > > >
> > > > On the contrary I don't think Couch is (yet?) able to to Union
> between
> > > > indexes, so if you have index { fieldA } index { fieldB } and ask for
> > > > select { fieldA, fieldB } only index { fieldA } will be used and then
> > > > all the documents will be processed to find the subset.
> > > >
> > > > That's my empiric deduction from some usage, but someone with the
> hands
> > > > on the code can probably correct me.
> > > >
> > > > ---
> > > >
> > > > Andrea Brancatelli
> > > >
> > > > On 2020-06-09 13:21, Piotr Zarzycki wrote:
> > > >
> > > > > Hi  Aurélien,
> > > > >
> > > > > It is not actually about any specific case. My questions are just
> > > helpers
> > > > > to truly understand what approach we should take in our database. I
> > > just
> > > > > have hope that I will get more understanding to get answer to some
> > > cases.
> > > > >
> > > > > Answer to your questions inline.
> > > > >
> > > > > wt., 9 cze 2020 o 13:04 Aurélien Bénel <au...@utt.fr>
> > > > napisał(a):
> > > > >
> > > > > Hi Piotr,
> > > > >
> > > > > Let's say that I have created two indexes for fields "docType" and
> > > "age":
> > > > > (...)
> > > > > My question is - How CouchDb will behave in case when I'm searching
> > my
> > > db
> > > > > using both fields when there are two indexes which contains the
> same
> > > > > fields?
> > > > > To answer your question (at least with js views, I don't use Mango
> > but
> > > I
> > > > > suppose it to be similar),
> > > > > we need more details:
> > > > >
> > > > > - Are both fields variable parameters ? Or is one of them a
> constant?
> > > >
> > > > I think both cases I would consider. However the first one would be
> > that
> > > > I'm searching using both fields as a variable parameters. I'm not
> sure
> > > > about constant question. I think non of those fields are constant - I
> > > > mean
> > > > from the database point of view docType won't change in the document
> > > > that's
> > > > for sure.
> > > >
> > > > > - Will both parameters be always set ? If not, which one might be
> > > unset?
> > > > >
> > > > > Let's assume that it always will, but like I said what if I'm
> looking
> > > > only
> > > >  and searching/querying using docType only ?
> > > >
> > > > > Regards,
> > > > >
> > > > > Aurélien
> > >
> >
> >
> > --
> >
> > Piotr Zarzycki
> >
> > Patreon: *https://www.patreon.com/piotrzarzycki
> > <https://www.patreon.com/piotrzarzycki>*
> >
>


-- 

Piotr Zarzycki

Patreon: *https://www.patreon.com/piotrzarzycki
<https://www.patreon.com/piotrzarzycki>*

Re: How to work with Indexes

Posted by Garren Smith <ga...@apache.org>.
Hi Piotr,

If you have the above two indexes. If you specify docType and Age. It will
use the index that has docType and Age. If you only specify age it will use
all_docs because you do not have a suitable index. If you select only
doctype it will use the index with docType. The _explain endpoint will give
you some ideas on how it works.

What is nice about Mango is in the beginning you don't have to create any
indexes. You can just use it. So while you are building an application
using the Mango endpoints rather don't create indexes and rather figure out
the type of queries you want to do. Once you are further along and you know
exactly the type of queries you want to perform with it. Then create
indexes.

I hope that helps a bit.

Cheers
Garren


On Wed, Jun 10, 2020 at 8:33 AM Piotr Zarzycki <pi...@gmail.com>
wrote:

> Hi ermouth,
>
> Many Thanks for your suggestion regarding Help feature. I will go with that
> for sure!
>
> Piotr
>
> wt., 9 cze 2020 o 14:27 ermouth <er...@gmail.com> napisał(a):
>
> > > Just remember to ask the selector in the same order as they are indexed
> >
> > It doesn’t guarantee appropriate index will be used, however providing
> > use_index param might help.
> >
> >
> https://docs.couchdb.org/en/2.3.1/api/database/find.html?highlight=use_index
> >
> > ermouth
> >
> >
> > вт, 9 июн. 2020 г. в 15:18, Andrea Brancatelli
> > <ab...@schema31.it.invalid>:
> >
> > > As ermouth said the best approach is to use the explain feature of
> > > Fauxton / Photon.
> > >
> > > Couch tries to guess what's the best index to use for any case so you
> > > don't necessarily need to build complex indexes, if you ask for more
> > > selectors than the indexed ones usually it picks an index and then scan
> > > the documents to apply the subselectors.
> > >
> > > Yet I must admit I feel the logic behind it a bit entropic and
> > > cumbersome.
> > >
> > > Just remember to ask the selector in the same order as they are
> indexed,
> > > even for partial indexes.
> > >
> > > Like a select for { fieldA, fieldB, fieldC } and an index for { fieldA,
> > > fieldB } would work as you expect, but if the index was on { fieldA,
> > > fieldC } it won't get used.
> > >
> > > On the contrary I don't think Couch is (yet?) able to to Union between
> > > indexes, so if you have index { fieldA } index { fieldB } and ask for
> > > select { fieldA, fieldB } only index { fieldA } will be used and then
> > > all the documents will be processed to find the subset.
> > >
> > > That's my empiric deduction from some usage, but someone with the hands
> > > on the code can probably correct me.
> > >
> > > ---
> > >
> > > Andrea Brancatelli
> > >
> > > On 2020-06-09 13:21, Piotr Zarzycki wrote:
> > >
> > > > Hi  Aurélien,
> > > >
> > > > It is not actually about any specific case. My questions are just
> > helpers
> > > > to truly understand what approach we should take in our database. I
> > just
> > > > have hope that I will get more understanding to get answer to some
> > cases.
> > > >
> > > > Answer to your questions inline.
> > > >
> > > > wt., 9 cze 2020 o 13:04 Aurélien Bénel <au...@utt.fr>
> > > napisał(a):
> > > >
> > > > Hi Piotr,
> > > >
> > > > Let's say that I have created two indexes for fields "docType" and
> > "age":
> > > > (...)
> > > > My question is - How CouchDb will behave in case when I'm searching
> my
> > db
> > > > using both fields when there are two indexes which contains the same
> > > > fields?
> > > > To answer your question (at least with js views, I don't use Mango
> but
> > I
> > > > suppose it to be similar),
> > > > we need more details:
> > > >
> > > > - Are both fields variable parameters ? Or is one of them a constant?
> > >
> > > I think both cases I would consider. However the first one would be
> that
> > > I'm searching using both fields as a variable parameters. I'm not sure
> > > about constant question. I think non of those fields are constant - I
> > > mean
> > > from the database point of view docType won't change in the document
> > > that's
> > > for sure.
> > >
> > > > - Will both parameters be always set ? If not, which one might be
> > unset?
> > > >
> > > > Let's assume that it always will, but like I said what if I'm looking
> > > only
> > >  and searching/querying using docType only ?
> > >
> > > > Regards,
> > > >
> > > > Aurélien
> >
>
>
> --
>
> Piotr Zarzycki
>
> Patreon: *https://www.patreon.com/piotrzarzycki
> <https://www.patreon.com/piotrzarzycki>*
>

Re: How to work with Indexes

Posted by Piotr Zarzycki <pi...@gmail.com>.
Hi ermouth,

Many Thanks for your suggestion regarding Help feature. I will go with that
for sure!

Piotr

wt., 9 cze 2020 o 14:27 ermouth <er...@gmail.com> napisał(a):

> > Just remember to ask the selector in the same order as they are indexed
>
> It doesn’t guarantee appropriate index will be used, however providing
> use_index param might help.
>
> https://docs.couchdb.org/en/2.3.1/api/database/find.html?highlight=use_index
>
> ermouth
>
>
> вт, 9 июн. 2020 г. в 15:18, Andrea Brancatelli
> <ab...@schema31.it.invalid>:
>
> > As ermouth said the best approach is to use the explain feature of
> > Fauxton / Photon.
> >
> > Couch tries to guess what's the best index to use for any case so you
> > don't necessarily need to build complex indexes, if you ask for more
> > selectors than the indexed ones usually it picks an index and then scan
> > the documents to apply the subselectors.
> >
> > Yet I must admit I feel the logic behind it a bit entropic and
> > cumbersome.
> >
> > Just remember to ask the selector in the same order as they are indexed,
> > even for partial indexes.
> >
> > Like a select for { fieldA, fieldB, fieldC } and an index for { fieldA,
> > fieldB } would work as you expect, but if the index was on { fieldA,
> > fieldC } it won't get used.
> >
> > On the contrary I don't think Couch is (yet?) able to to Union between
> > indexes, so if you have index { fieldA } index { fieldB } and ask for
> > select { fieldA, fieldB } only index { fieldA } will be used and then
> > all the documents will be processed to find the subset.
> >
> > That's my empiric deduction from some usage, but someone with the hands
> > on the code can probably correct me.
> >
> > ---
> >
> > Andrea Brancatelli
> >
> > On 2020-06-09 13:21, Piotr Zarzycki wrote:
> >
> > > Hi  Aurélien,
> > >
> > > It is not actually about any specific case. My questions are just
> helpers
> > > to truly understand what approach we should take in our database. I
> just
> > > have hope that I will get more understanding to get answer to some
> cases.
> > >
> > > Answer to your questions inline.
> > >
> > > wt., 9 cze 2020 o 13:04 Aurélien Bénel <au...@utt.fr>
> > napisał(a):
> > >
> > > Hi Piotr,
> > >
> > > Let's say that I have created two indexes for fields "docType" and
> "age":
> > > (...)
> > > My question is - How CouchDb will behave in case when I'm searching my
> db
> > > using both fields when there are two indexes which contains the same
> > > fields?
> > > To answer your question (at least with js views, I don't use Mango but
> I
> > > suppose it to be similar),
> > > we need more details:
> > >
> > > - Are both fields variable parameters ? Or is one of them a constant?
> >
> > I think both cases I would consider. However the first one would be that
> > I'm searching using both fields as a variable parameters. I'm not sure
> > about constant question. I think non of those fields are constant - I
> > mean
> > from the database point of view docType won't change in the document
> > that's
> > for sure.
> >
> > > - Will both parameters be always set ? If not, which one might be
> unset?
> > >
> > > Let's assume that it always will, but like I said what if I'm looking
> > only
> >  and searching/querying using docType only ?
> >
> > > Regards,
> > >
> > > Aurélien
>


-- 

Piotr Zarzycki

Patreon: *https://www.patreon.com/piotrzarzycki
<https://www.patreon.com/piotrzarzycki>*

Re: How to work with Indexes

Posted by ermouth <er...@gmail.com>.
> Just remember to ask the selector in the same order as they are indexed

It doesn’t guarantee appropriate index will be used, however providing
use_index param might help.
https://docs.couchdb.org/en/2.3.1/api/database/find.html?highlight=use_index

ermouth


вт, 9 июн. 2020 г. в 15:18, Andrea Brancatelli
<ab...@schema31.it.invalid>:

> As ermouth said the best approach is to use the explain feature of
> Fauxton / Photon.
>
> Couch tries to guess what's the best index to use for any case so you
> don't necessarily need to build complex indexes, if you ask for more
> selectors than the indexed ones usually it picks an index and then scan
> the documents to apply the subselectors.
>
> Yet I must admit I feel the logic behind it a bit entropic and
> cumbersome.
>
> Just remember to ask the selector in the same order as they are indexed,
> even for partial indexes.
>
> Like a select for { fieldA, fieldB, fieldC } and an index for { fieldA,
> fieldB } would work as you expect, but if the index was on { fieldA,
> fieldC } it won't get used.
>
> On the contrary I don't think Couch is (yet?) able to to Union between
> indexes, so if you have index { fieldA } index { fieldB } and ask for
> select { fieldA, fieldB } only index { fieldA } will be used and then
> all the documents will be processed to find the subset.
>
> That's my empiric deduction from some usage, but someone with the hands
> on the code can probably correct me.
>
> ---
>
> Andrea Brancatelli
>
> On 2020-06-09 13:21, Piotr Zarzycki wrote:
>
> > Hi  Aurélien,
> >
> > It is not actually about any specific case. My questions are just helpers
> > to truly understand what approach we should take in our database. I just
> > have hope that I will get more understanding to get answer to some cases.
> >
> > Answer to your questions inline.
> >
> > wt., 9 cze 2020 o 13:04 Aurélien Bénel <au...@utt.fr>
> napisał(a):
> >
> > Hi Piotr,
> >
> > Let's say that I have created two indexes for fields "docType" and "age":
> > (...)
> > My question is - How CouchDb will behave in case when I'm searching my db
> > using both fields when there are two indexes which contains the same
> > fields?
> > To answer your question (at least with js views, I don't use Mango but I
> > suppose it to be similar),
> > we need more details:
> >
> > - Are both fields variable parameters ? Or is one of them a constant?
>
> I think both cases I would consider. However the first one would be that
> I'm searching using both fields as a variable parameters. I'm not sure
> about constant question. I think non of those fields are constant - I
> mean
> from the database point of view docType won't change in the document
> that's
> for sure.
>
> > - Will both parameters be always set ? If not, which one might be unset?
> >
> > Let's assume that it always will, but like I said what if I'm looking
> only
>  and searching/querying using docType only ?
>
> > Regards,
> >
> > Aurélien

Re: How to work with Indexes

Posted by Piotr Zarzycki <pi...@gmail.com>.
Hi Andrea,

Even that empiric usage is really helpful and helps formalize some plan on
that. I hope someone with CouchDb code knowledge will answer and shed even
more light on that.

Thank you so much,
Piotr

wt., 9 cze 2020 o 14:18 Andrea Brancatelli <ab...@schema31.it>
napisał(a):

> As ermouth said the best approach is to use the explain feature of Fauxton
> / Photon.
> Couch tries to guess what's the best index to use for any case so you
> don't necessarily need to build complex indexes, if you ask for more
> selectors than the indexed ones usually it picks an index and then scan the
> documents to apply the subselectors.
>
> Yet I must admit I feel the logic behind it a bit entropic and cumbersome.
>
> Just remember to ask the selector in the same order as they are indexed,
> even for partial indexes.
>
> Like a select for { fieldA, fieldB, fieldC } and an index for { fieldA,
> fieldB } would work as you expect, but if the index was on { fieldA, fieldC
> } it won't get used.
>
> On the contrary I don't think Couch is (yet?) able to to Union between
> indexes, so if you have index { fieldA } index { fieldB } and ask for
> select { fieldA, fieldB } only index { fieldA } will be used and then all
> the documents will be processed to find the subset.
>
> That's my empiric deduction from some usage, but someone with the hands on
> the code can probably correct me.
>
>
> ---
>
> *Andrea Brancatelli
>
> *
>
> On 2020-06-09 13:21, Piotr Zarzycki wrote:
>
> Hi  Aurélien,
>
> It is not actually about any specific case. My questions are just helpers
> to truly understand what approach we should take in our database. I just
> have hope that I will get more understanding to get answer to some cases.
>
> Answer to your questions inline.
>
> wt., 9 cze 2020 o 13:04 Aurélien Bénel <au...@utt.fr> napisał(a):
>
> Hi Piotr,
>
> Let's say that I have created two indexes for fields "docType" and "age":
> (...)
> My question is - How CouchDb will behave in case when I'm searching my db
> using both fields when there are two indexes which contains the same
> fields?
>
>
> To answer your question (at least with js views, I don't use Mango but I
> suppose it to be similar),
> we need more details:
>
> - Are both fields variable parameters ? Or is one of them a constant?
>
>
> I think both cases I would consider. However the first one would be that
> I'm searching using both fields as a variable parameters. I'm not sure
> about constant question. I think non of those fields are constant - I mean
> from the database point of view docType won't change in the document that's
> for sure.
>
>
> - Will both parameters be always set ? If not, which one might be unset?
>
> Let's assume that it always will, but like I said what if I'm looking only
>
> and searching/querying using docType only ?
>
>
>
> Regards,
>
> Aurélien
>
>
>

-- 

Piotr Zarzycki

Patreon: *https://www.patreon.com/piotrzarzycki
<https://www.patreon.com/piotrzarzycki>*

Re: How to work with Indexes

Posted by Andrea Brancatelli <ab...@schema31.it.INVALID>.
As ermouth said the best approach is to use the explain feature of
Fauxton / Photon.

Couch tries to guess what's the best index to use for any case so you
don't necessarily need to build complex indexes, if you ask for more
selectors than the indexed ones usually it picks an index and then scan
the documents to apply the subselectors. 

Yet I must admit I feel the logic behind it a bit entropic and
cumbersome. 

Just remember to ask the selector in the same order as they are indexed,
even for partial indexes. 

Like a select for { fieldA, fieldB, fieldC } and an index for { fieldA,
fieldB } would work as you expect, but if the index was on { fieldA,
fieldC } it won't get used. 

On the contrary I don't think Couch is (yet?) able to to Union between
indexes, so if you have index { fieldA } index { fieldB } and ask for
select { fieldA, fieldB } only index { fieldA } will be used and then
all the documents will be processed to find the subset. 

That's my empiric deduction from some usage, but someone with the hands
on the code can probably correct me. 

---

Andrea Brancatelli

On 2020-06-09 13:21, Piotr Zarzycki wrote:

> Hi  Aurélien,
> 
> It is not actually about any specific case. My questions are just helpers
> to truly understand what approach we should take in our database. I just
> have hope that I will get more understanding to get answer to some cases.
> 
> Answer to your questions inline.
> 
> wt., 9 cze 2020 o 13:04 Aurélien Bénel <au...@utt.fr> napisał(a):
> 
> Hi Piotr,
> 
> Let's say that I have created two indexes for fields "docType" and "age":
> (...)
> My question is - How CouchDb will behave in case when I'm searching my db
> using both fields when there are two indexes which contains the same
> fields? 
> To answer your question (at least with js views, I don't use Mango but I
> suppose it to be similar),
> we need more details:
> 
> - Are both fields variable parameters ? Or is one of them a constant?

I think both cases I would consider. However the first one would be that
I'm searching using both fields as a variable parameters. I'm not sure
about constant question. I think non of those fields are constant - I
mean
from the database point of view docType won't change in the document
that's
for sure.

> - Will both parameters be always set ? If not, which one might be unset?
> 
> Let's assume that it always will, but like I said what if I'm looking only
 and searching/querying using docType only ?

> Regards,
> 
> Aurélien

Re: How to work with Indexes

Posted by Piotr Zarzycki <pi...@gmail.com>.
Hi  Aurélien,

It is not actually about any specific case. My questions are just helpers
to truly understand what approach we should take in our database. I just
have hope that I will get more understanding to get answer to some cases.

Answer to your questions inline.

wt., 9 cze 2020 o 13:04 Aurélien Bénel <au...@utt.fr> napisał(a):

> Hi Piotr,
>
> > Let's say that I have created two indexes for fields "docType" and "age":
> > (...)
> > My question is - How CouchDb will behave in case when I'm searching my db
> > using both fields when there are two indexes which contains the same
> > fields?
>
> To answer your question (at least with js views, I don't use Mango but I
> suppose it to be similar),
> we need more details:
>
> - Are both fields variable parameters ? Or is one of them a constant?
>

I think both cases I would consider. However the first one would be that
I'm searching using both fields as a variable parameters. I'm not sure
about constant question. I think non of those fields are constant - I mean
from the database point of view docType won't change in the document that's
for sure.


> - Will both parameters be always set ? If not, which one might be unset?
>
> Let's assume that it always will, but like I said what if I'm looking only
and searching/querying using docType only ?


>
> Regards,
>
> Aurélien



-- 

Piotr Zarzycki

Patreon: *https://www.patreon.com/piotrzarzycki
<https://www.patreon.com/piotrzarzycki>*

Re: How to work with Indexes

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

> Let's say that I have created two indexes for fields "docType" and "age":
> (...)
> My question is - How CouchDb will behave in case when I'm searching my db
> using both fields when there are two indexes which contains the same
> fields?

To answer your question (at least with js views, I don't use Mango but I suppose it to be similar), 
we need more details:

- Are both fields variable parameters ? Or is one of them a constant?
- Will both parameters be always set ? If not, which one might be unset? 


Regards,

Aurélien

Re: How to work with Indexes

Posted by ermouth <er...@gmail.com>.
> How CouchDb will behave in case when I'm searching my db
> using both fields when there are two indexes which contains the same

Consider using Explain button, it exists both in Fauxton and Photon and
located in Mango qry panel. Very likely you will discover that CouchDB by
default does not use any of indexes.

ermouth

Re: How to work with Indexes

Posted by Piotr Zarzycki <pi...@gmail.com>.
I have found following article [1], which describes how indexes are working
- I hope it is still true. In the light of what I'm reading there I have a
question:

Let's say that I have created two indexes for fields "docType" and "age":

{
   "index": {
      "fields": [
         "docType"
      ]
   },
   "name": "foo-json-index",
   "type": "json"
},
{
   "index": {
      "fields": [
         "docType",
         "age "
      ]
   },
   "name": "foo-json-index",
   "type": "json"
}

My question is - How CouchDb will behave in case when I'm searching my db
using both fields when there are two indexes which contains the same
fields?

Let's say I have: doceType == "form" && age == 32

[1] https://dx13.co.uk/articles/2019/11/08/couchdb-mango-indexes/

Thanks,
Piotr

pt., 5 cze 2020 o 11:34 Piotr Zarzycki <pi...@gmail.com>
napisał(a):

> Hi Adam,
>
> Thank you for your response. I would like to go a bit deeper into that
> idea. I'm definitely not an expert on CouchDb - I would say very beginner.
> :)
> If I will have let's say 100 different type of requests in my app and I
> will create indexes for all of them. Rather if I will have one JSON object
> in CouchDb and I will create in Fauxton for each property Index, cause I
> each of my requests related to that object will need that or I will just
> feel that is needed  - Can it be some bad implication of doing that ?
>
> Thanks,
> Piotr
>
>
> czw., 4 cze 2020 o 16:06 Adam Kocoloski <ko...@apache.org> napisał(a):
>
>> Hi Piotr,
>>
>> To first order, yes, I’d try to have every query an application issues be
>> satisfied by an index.
>>
>> If you’ve got some queries that you run infrequently in the background
>> those may not warrant an index, as the resources required to keep the index
>> up-to-date would be greater than the cost of just scanning the entire
>> database.
>>
>> Cheers, Adam
>>
>> > On Jun 4, 2020, at 8:53 AM, Piotr Zarzycki <pi...@gmail.com>
>> wrote:
>> >
>> > Hello everyone,
>> >
>> > We are building JS based application which storing data in CouchDb. It
>> is a
>> > greenfield application in case of front end and database structure. I
>> would
>> > use some examples at the beginning.
>> >
>> > I have some selector which gets me data from db:
>> >
>> > const q = {
>> >      selector: {
>> >        historyId: document.historyId,
>> >      },
>> >      sort: [{ version: "desc" }],
>> >      limit: 500,
>> >    };
>> >
>> >
>> > In the results I will get data along with warning:
>> >
>> > "warning": "no matching index found, create an index to optimize query
>> time"
>> >
>> >
>> > I can easily get rid of that warning by adding using Fauxton index:
>> >
>> > {
>> >   "index": {
>> >      "fields": [
>> >         "historyId"
>> >      ]
>> >   },
>> >   "name": "historyId-json-index",
>> >   "type": "json"
>> > }
>> >
>> >
>> > My question is - How do you guys approach to working with indexes ?
>> Should
>> > I add it to each query which I'm doing ?
>> >
>> > Thoughts about approaches would be much appreciated. :)
>> >
>> > Thanks,
>> > --
>> >
>> > Piotr Zarzycki
>> >
>> > Patreon: *https://www.patreon.com/piotrzarzycki
>> > <https://www.patreon.com/piotrzarzycki>*
>>
>>
>
> --
>
> Piotr Zarzycki
>
> Patreon: *https://www.patreon.com/piotrzarzycki
> <https://www.patreon.com/piotrzarzycki>*
>


-- 

Piotr Zarzycki

Patreon: *https://www.patreon.com/piotrzarzycki
<https://www.patreon.com/piotrzarzycki>*

Re: How to work with Indexes

Posted by Piotr Zarzycki <pi...@gmail.com>.
Hi Adam,

Thank you for your response. I would like to go a bit deeper into that
idea. I'm definitely not an expert on CouchDb - I would say very beginner.
:)
If I will have let's say 100 different type of requests in my app and I
will create indexes for all of them. Rather if I will have one JSON object
in CouchDb and I will create in Fauxton for each property Index, cause I
each of my requests related to that object will need that or I will just
feel that is needed  - Can it be some bad implication of doing that ?

Thanks,
Piotr


czw., 4 cze 2020 o 16:06 Adam Kocoloski <ko...@apache.org> napisał(a):

> Hi Piotr,
>
> To first order, yes, I’d try to have every query an application issues be
> satisfied by an index.
>
> If you’ve got some queries that you run infrequently in the background
> those may not warrant an index, as the resources required to keep the index
> up-to-date would be greater than the cost of just scanning the entire
> database.
>
> Cheers, Adam
>
> > On Jun 4, 2020, at 8:53 AM, Piotr Zarzycki <pi...@gmail.com>
> wrote:
> >
> > Hello everyone,
> >
> > We are building JS based application which storing data in CouchDb. It
> is a
> > greenfield application in case of front end and database structure. I
> would
> > use some examples at the beginning.
> >
> > I have some selector which gets me data from db:
> >
> > const q = {
> >      selector: {
> >        historyId: document.historyId,
> >      },
> >      sort: [{ version: "desc" }],
> >      limit: 500,
> >    };
> >
> >
> > In the results I will get data along with warning:
> >
> > "warning": "no matching index found, create an index to optimize query
> time"
> >
> >
> > I can easily get rid of that warning by adding using Fauxton index:
> >
> > {
> >   "index": {
> >      "fields": [
> >         "historyId"
> >      ]
> >   },
> >   "name": "historyId-json-index",
> >   "type": "json"
> > }
> >
> >
> > My question is - How do you guys approach to working with indexes ?
> Should
> > I add it to each query which I'm doing ?
> >
> > Thoughts about approaches would be much appreciated. :)
> >
> > Thanks,
> > --
> >
> > Piotr Zarzycki
> >
> > Patreon: *https://www.patreon.com/piotrzarzycki
> > <https://www.patreon.com/piotrzarzycki>*
>
>

-- 

Piotr Zarzycki

Patreon: *https://www.patreon.com/piotrzarzycki
<https://www.patreon.com/piotrzarzycki>*

Re: How to work with Indexes

Posted by Adam Kocoloski <ko...@apache.org>.
Hi Piotr,

To first order, yes, I’d try to have every query an application issues be satisfied by an index.

If you’ve got some queries that you run infrequently in the background those may not warrant an index, as the resources required to keep the index up-to-date would be greater than the cost of just scanning the entire database.

Cheers, Adam

> On Jun 4, 2020, at 8:53 AM, Piotr Zarzycki <pi...@gmail.com> wrote:
> 
> Hello everyone,
> 
> We are building JS based application which storing data in CouchDb. It is a
> greenfield application in case of front end and database structure. I would
> use some examples at the beginning.
> 
> I have some selector which gets me data from db:
> 
> const q = {
>      selector: {
>        historyId: document.historyId,
>      },
>      sort: [{ version: "desc" }],
>      limit: 500,
>    };
> 
> 
> In the results I will get data along with warning:
> 
> "warning": "no matching index found, create an index to optimize query time"
> 
> 
> I can easily get rid of that warning by adding using Fauxton index:
> 
> {
>   "index": {
>      "fields": [
>         "historyId"
>      ]
>   },
>   "name": "historyId-json-index",
>   "type": "json"
> }
> 
> 
> My question is - How do you guys approach to working with indexes ? Should
> I add it to each query which I'm doing ?
> 
> Thoughts about approaches would be much appreciated. :)
> 
> Thanks,
> -- 
> 
> Piotr Zarzycki
> 
> Patreon: *https://www.patreon.com/piotrzarzycki
> <https://www.patreon.com/piotrzarzycki>*