Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RavenDB - querying issue - Stale results/indexes

Tags:

nosql

ravendb

While querying RavenDB I am noticing that it does not get the expected results immediately. May be it has to do with indexing, I dont know.

For example :

   int ACount = session.Query<Patron>()
                   .Count();

   int BCount = session.Query<Theaters>()
                   .Count();

   int CCount = session.Query<Movies>()
                   .Where(x => x.Status == "Released")                                              
                   .Count();

   int DCount = session.Query<Promotions>()
                    .Count();

When I execute this then ACount and BCount get their values immediately on the first run). However CCount and DCount do not get their values until after three or four runs. They show 0 (zero) value in the first few runs.

Why does this happen for bottom two and not top two queries? If its because of stale results (or indexes) then how can I modify my queries to get the accurate results every time, when I run it first time. Thank you for help.

like image 772
ZVenue Avatar asked Jan 03 '12 15:01

ZVenue


3 Answers

If you haven't defined an index for the Movies query, Raven will create a Dynamic Index. If you use the query repeatedly the index will be automatically persisted. Otherwise Raven will discard it and that may explain why you're getting 0 results during the first few runs.

You can also instruct Raven to wait for the indexing process to ensure that you'll always get the most accurate results (even though this might not be a good idea as it will slow your queries) by using the WaitForNonStaleResults instruction:

session.Query<Movies>()
.Customize(x => x.WaitForNonStaleResults())
.Where(x => x.Status == "Released")                                              
.Count();
like image 73
Carlos Mendes Avatar answered Nov 02 '22 12:11

Carlos Mendes


Needing to put WaitForNonStaleResults in each query feels like a massive "code smell" (as much as I normally hate the term, it seems completely appropriate here).

The only real solution I've found so far is:

var store = new DocumentStore(); // do whatever
store.DatabaseCommands.DisableAllCaching();

Performance suffers accordingly, but I think slower performance is far less of a sin than unreliable if not outright inaccurate results.

like image 44
nathanchere Avatar answered Nov 02 '22 11:11

nathanchere


You have the following options according to the official documentation (the most preferable first):

  1. Setting cut-off point.

    WaitForNonStaleResultsAsOfLastWrite(TimeSpan.FromSeconds(10)) or WaitForNonStaleResultsAsOfNow()

    This will make sure that you get the latest results up to that point in time (or till the last write). And you can put a cap on it (e.g. 10s), if you want to sacrifice freshness of the results to receiving the response faster.

  2. Explicitly waiting for non-stale results WaitForNonStaleResultsAsOfNow(TimeSpan.FromSeconds(10))

    Again, specifying a time-out would be a good practice.

  3. Setting querying conventions to apply the same rule to all requests

    store.Conventions.DefaultQueryingConsistency = ConsistencyOptions.AlwaysWaitForNonStaleResultsAsOfLastWrite.

like image 33
Alex Klaus Avatar answered Nov 02 '22 11:11

Alex Klaus