Question

Simple question this one...

Is this;

http://host/database/docid

Faster than this;

http://host/database/_design/category/_view/specific/?key=docid

I expect that an indexed view would be faster, but I can't be sure, and a detailed answer would be nice.

Was it helpful?

Solution

Without diving into technical details, let's try to describe the whole process:

For URL:

http://host/database/docid
  1. Handle the request
  2. Pick database name, look if file handle is open or locate and open the related file
  3. Walk thought Btree index looking for the specified docid. This is O(logn) operation
  4. Read the document
  5. Return result to the client

Now for the view:

http://host/database/_design/category/_view/specific/?key=docid
  1. Handle the request
  2. Pick database name, look if file handle is open or locate and open the related file
  3. Walk thought Btree index looking for specified design document id
  4. Locate the index file. His name is based is hash from ddoc's views + language + options fields.
  5. Open the file or use already opened file handler
  6. Locate index for specified view function
  7. Walk thought Btree index looking for the specified key 6.5 If you want for full doc content - add include_docs=true query parameter and that's will cost you additional I/O operation
  8. Return result to the client

As you see, requesting views is a bit more complex than requesting by document id.

Here is dummy benchmark using wrk by doc id:

Running 1m test @ http://host/database/docid
  8 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    48.35ms    7.14ms 102.98ms   83.13%
    Req/Sec   250.38     24.81   323.00     74.06%
  Latency Distribution
     50%   45.99ms
     75%   50.98ms
     90%   57.95ms
     99%   74.01ms
  119880 requests in 1.00m, 103.71MB read
Requests/sec:   1997.91
Transfer/sec:      1.73MB

and using view:

Running 1m test @ http://host/database/_design/category/_view/specific/?key=docid
  8 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    61.06ms   13.68ms 178.97ms   79.74%
    Req/Sec   200.38     28.80   263.00     71.60%
  Latency Distribution
     50%   57.05ms
     75%   66.98ms
     90%   77.01ms
     99%  112.99ms
  96071 requests in 1.00m, 28.69MB read
Requests/sec:   1601.06
Transfer/sec:    489.55KB

and view with include_docs=true:

Running 1m test @ http://host/database/_design/category/_view/specific/?key=docid&include_docs=true
  8 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    82.95ms   13.26ms 200.98ms   77.46%
    Req/Sec   145.91     15.11   192.00     73.28%
  Latency Distribution
     50%   80.77ms
     75%   88.97ms
     90%   98.97ms
     99%  124.98ms
  69988 requests in 1.00m, 69.89MB read
Requests/sec:   1166.27
Transfer/sec:      1.16MB

However, views aren't for getting documents by their id. They are secondary indexes for your database and allows to locate the data in more different ways. They supports reduce operation, grouping, which is extremely useful for statistics.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top