cloudant / python-cloudant

A Python library for Cloudant and CouchDB
Apache License 2.0
163 stars 55 forks source link

Querying with _id : does it use the index #319

Closed vvasuki closed 7 years ago

vvasuki commented 7 years ago

Does

    from cloudant.query import Query
    query = Query(self.db, selector={"_id": id})

automatically use the index? If not, perhaps it ought to? Otherwise, a warning could be provided in the Query documentation..

smithsz commented 7 years ago

You can use the use_index parameter to define a specific index for the query to run against. If you don't specify an index then the server will attempt to find the best index for you (see here for more details on how the best index is chosen).

For more info and some examples take a look at our query documentation.

Let me know if you have further questions.

vvasuki commented 7 years ago

Thanks! Just to double-check, the code I quoted will automatically use some (usually the default) index? (The question is specifically about a CouchDatabase, not cloudant.)

smithsz commented 7 years ago

Yep, it'll choose a "best index" by default. You can give Query a CouchDB database object and it won't complain. Here's an example I ran against CouchDB 2.0:

from cloudant.client import CouchDB
from cloudant.query import Query

# setup admin party CouchDB client
client = CouchDB(None, None, admin_party=True, connect=True, url='http://localhost:5984')

db = client['animaldb']
query = Query(db, selector={'diet': 'omnivore'})

for doc in query()['docs']:
    print doc['_id']

Results:

aardvark
badger
lemur
snipe

By querying the /_explain endpoint you can see the primary /_all_docs index is being used to service this request.

$ curl -sg localhost:5984/animaldb/_explain -H content-type:application/json -d '{"selector":{"diet":"omnivore"}}' | jq .index.name
"_all_docs"

For production, we advise you create a specific index to service these requests in order to optimise query time. See our query docs for more details on this.