apache / couchdb

Seamless multi-master syncing database with an intuitive HTTP/JSON API, designed for reliability
https://couchdb.apache.org/
Apache License 2.0
6.17k stars 1.03k forks source link

Mango: do not warn about missing index when only _id is being selected against #2124

Open oliverdain opened 5 years ago

oliverdain commented 5 years ago

Description

If I run the following query it clearly is using the _id index/btree (you can tell by how fast it runs and explain shows it's using _all_docs with the correct start_key and end_key):

{
   "selector": {
      "$and": [
         {
            "_id": {
               "$gt": "bar"
            }
         },
         {
            "_id": {
               "$lt": "foo"
            }
         }
      ]
   }
}

But when I run it I get "warning": "no matching index found, create an index to optimize query time". "use_index": "_all_docs" did not make it go away.

Steps to Reproduce

Hit the _find endpoint with that selector and request execution_stats.

Expected Behaviour

No warning.

Your Environment

wohali commented 4 years ago

Hi @oliverdain ,

That'll be because the _all_docs index is what we check to see if no matching index was found. :smile:

It sounds like you'd need a special case for this Mango query where we suppress the warning when only the _id field is being used.

Out of curiosity, why use the Mango API here instead of just using the /{db}/_all_docs endpoint?

oliverdain commented 4 years ago

Hi @wohali . Thanks for the explanation. I've simplified the real query here to make the bug report simpler but the real query used _id and some other fields to further limit the results I get. Note that with Mango this is legal and if I do that with an index other than _all_docs I do not get a warning. For example, a query like this is, I think, quite reasonable:

{
   "selector": {
      "$and": [
         {
            "_id": {
               "$gt": "bar"
            }
         },
         {
            "_id": {
               "$lt": "foo"
            }
         },
        {
          "other_field": "some value"
        }
      ]
   }
}

Assuming there aren't too many docs with _id between bar and foo this is a very useful way to use Mango and the performance is quite good.

I think the desired behavior isn't "suppress the warning when only the _id field is being used" but rather something like "suppress the warning when the _id field has significantly reduced the set of documents that must be examined" or whatever the equivalent rule is for a normal Mango index.

wohali commented 4 years ago

Hey @oliverdain , thanks for writing back.

Technically, what you're saying still requires a main index if you don't have a direct index that includes both _id and other_field. What I think would be the best compromise here is that the user can safely ignore the warning - but IMO the warning should still be generated. Adding more logic in that part of the code could slow down all requests, something we'd really prefer not to do.

To me, this feels like a documentation callout - if you'd like to open a PR in https://github.com/apache/couchdb-documentation and explain that the warning can be safely ignored in this scenario, I'd be happy to merge that.