RavenDB - query problem - deprecated results / indexes

During a RavenDB request, I notice that it does not receive the expected results immediately. Maybe this is due to indexing, I do not know.

For instance:

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 do this, then ACount and BCount get their values ​​right the first time they start). However, CCount and DCount do not get their values ​​up to three or four runs. They show a 0 (zero) value in the first few runs.

Why is this happening for the two lower and not for the two upper queries? If this is due to outdated results (or indexes), then how can I modify my queries to get accurate results every time I run it the first time. Thank you for your help.

+6
source share
3 answers

If you did not specify an index for the movie request, Raven will create a Dynamic Index . If you reuse the query, the index will be automatically saved. Otherwise, Raven will drop it, and this may explain why you get 0 results during the first few runs.

You can also instruct Raven to wait for the indexing process to ensure that you always get the most accurate results (although this may not be a good idea, as it will slow down your queries) using WaitForNonStaleResults :

 session.Query<Movies>() .Customize(x => x.WaitForNonStaleResults()) .Where(x => x.Status == "Released") .Count(); 
+14
source

The need to set WaitForNonStaleResults in each request seems like a huge "smell of code" (as far as I usually hate this term, it seems quite appropriate here).

The only real solution I have found so far:

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

Performance suffers accordingly, but I think slower performance is far less sinful than unreliable, if not overt, inaccurate results.

+1
source

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

  • Set cut-off point .

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

    This will allow you to get the latest results up to this point (or until the last record). And you can put a cap on it (for example, 10 seconds) if you want to sacrifice the freshness of the results to get an answer faster.

  • Explicitly WaitForNonStaleResultsAsOfNow(TimeSpan.FromSeconds(10)) Results Without Obsolescence WaitForNonStaleResultsAsOfNow(TimeSpan.FromSeconds(10))

    Again, specifying a timeout will be good practice.

  • Setting query conditions to apply the same rule to all queries

    store.Conventions.DefaultQueryingConsistency = ConsistencyOptions.AlwaysWaitForNonStaleResultsAsOfLastWrite .

0
source

Source: https://habr.com/ru/post/904993/


All Articles