Heya Thilo,

startkey and endkey are applied first, so you are guaranteed to get all results 
within your specified range, but you are not guaranteed to get it in the sorted 
order defined by the second link you sent.

Imagine a sharded database. When making a view query, CouchDB makes an internal 
query with the same options (including startkey/endkey) to each shard. To 
ensure a sorted result (the default), the results from each shard get streamed 
to the node that handles the query, and that node does an in-flight merge of 
the incoming results.

The sorted=false option just returns all the result rows as they come in from 
the shards.

The potential performance gain is that some shards might reply faster than 
others, but to ensure an in-order result, those shard-results have to wait 
before they can be streamed to the client because a row from a slower shard 
might have logical precedence.

If you can sort on the client, or if the order doesn’t matter (just the 
startkey/endkey selection), then sorted=false can give you some potential 
speedup on a sharded database. You won’t go faster than your slowest shard, 
however, you might just get other shards’ results sooner.

Best
Jan
—

> On 9. Apr 2020, at 17:40, Thilo from Cobot <th...@cobot.me> wrote:
> 
> Hi there,
> 
> I was looking into speeding up queries to the database. I found under
> https://docs.couchdb.org/en/stable/api/ddoc/views.html#db-design-design-doc-view-view-name
> that I can disable sorting of results with sorted=false. I wonder what 
> happens if I combine that with the startkey/endkey parameters.
> 
> What I understood from 
> https://docs.couchdb.org/en/stable/api/ddoc/views.html#api-ddoc-view-sorting
> sorting is applied before filtering. I assume the result is undetermined, it 
> might include some docs that are between start and endkey but not all or 
> none, so its not wise to combine sorted=false with any filters. Is this 
> correct?
> 
> Thanks for any insights.
> 
> Cheers
> Thilo
> 
> --
> more time for your coworkers: https://cobot.me

Reply via email to