I am using RavenDB version 888.

My client application inserts hundreds of thousands of documents into RavenDB. It works fine. After the insertion, my app will query some data out of the static index I predefined. I don't want stale results so my app will query periodically and wait until the index is up-to-date.

Unfortunately, today I find my app hang (more accurately, keep querying RavenDB again and again) because the server always tells it that the index is still stale. It is a bit strange because last insertion finished long ago - theoratically the server should have finished indexing.

I looked into the management studio and checked my simpliest index which does some counting on one type of my doc collection. Interesting that the count given by the index is up-to-date (same as the number I saw in 'Collections' tab of the management studio) but the status is 'stale'. And its last update shows '6 hours ago'. Altogether, half of my indexes is stale like this but another half is fresh according to the studio.

I have no idea why RavenDB leaves them stale and what RavenDB is doing now. It does not have a high CPU usage. How can I debug the scenario? 

UPDATE:

I think I spotted one thing which might be helpful to find the root cause. After comparing my non-stale indexes with always-stale indexes, it seems that the reduce result matters: stale indexes have large Value property in reduce results while up-to-date indexes have small Value property.

```
public class ReduceResult
{
    public string ID { get; set; }
    public string Key { get; set; }
    public long Value { get; set; } //This field seems to matter
}

```

Here is one of my index definition:

```
public class InternalPageCountIndex : AbstractIndexCreationTask<InternalPage, ReduceResult>
{
    public InternalPageCountIndex()
    {
        Map = posts => from post in posts
                       select new
                       {
                           Key = post.BatchID,
                           Value = 1
                       };

        Reduce = results => from result in results
                            group result by result.Key
                                into g
                                select new
                                {
                                    Key = g.Key,
                                    Value = g.Sum(c => c.Value)
                                };
    }
}

```

Btw, the server log also looks interesting. This afternoon the server thought there is no job to do:

*2012-04-07 16:36:44.6725,Raven.Database.Tasks.ReduceTask,Debug,Indexed 65 reduce keys in 00:00:03.5535907 with 493666 results for index SNRTotalByteSizeIndex,
2012-04-07 17:35:21.1888,Raven.Database.Indexing.WorkContext,Debug,"No work was found, workerWorkCounter: 5, for: ReducingExecuter, will wait for additional work",
2012-04-07 17:35:21.1888,Raven.Database.Indexing.WorkContext,Debug,"No work was found, workerWorkCounter: 5, for: IndexingExecuter, will wait for additional work",
2012-04-07 18:35:39.4759,Raven.Database.Indexing.WorkContext,Debug,"No work was found, workerWorkCounter: 5, for: ReducingExecuter, will wait for additional work",
2012-04-07 18:35:39.4759,Raven.Database.Indexing.WorkContext,Debug,"No work was found, workerWorkCounter: 5, for: IndexingExecuter, will wait for additional work",
2012-04-07 19:35:56.5994,Raven.Database.Indexing.WorkContext,Debug,"No work was found, workerWorkCounter: 5, for: ReducingExecuter, will wait for additional work",
2012-04-07 19:35:56.5994,Raven.Database.Indexing.WorkContext,Debug,"No work was found, workerWorkCounter: 5, for: IndexingExecuter, will wait for additional work",
2012-04-07 20:36:12.3345,Raven.Database.Indexing.WorkContext,Debug,"No work was found, workerWorkCounter: 5, for: ReducingExecuter, will wait for additional work",
2012-04-07 20:36:12.3345,Raven.Database.Indexing.WorkContext,Debug,"No work was found, workerWorkCounter: 5, for: IndexingExecuter, will wait for additional work",*

But when I query RavenDB to see how many stale index there are through Management Studio tonight, the server starts to do map/reduce! Yes, no insertion in between afternoon and tonight, but the server find something to do with the index after the studio query...

*2012-04-07 21:23:16.9357,Raven.Database.Tasks.ReduceTask,Debug,Read 1 reduce keys in 00:03:05.6481176 with 505406 results for index InternalPageCountIndex,*

2012-04-07 21:23:19.5103,Raven.Database.Indexing.Index.Indexing,Debug,"Indexing on batches/1 result in index PageCountMissingDescriptionIndex gave document: \_\_reduce\_key I-: batches/1 Key IS: batches/1 Value IS: 505406 Value\_Range IS: 505406",

*2012-04-07 21:23:19.6797,Raven.Database.Indexing.Index.Indexing,Debug,Reduce resulted in 1 entries for InternalPageCountIndex for reduce keys: batches/1,
2012-04-07 21:23:19.6797,Raven.Database.Tasks.ReduceTask,Debug,Indexed 1 reduce keys in 00:00:02.7426449 with 505406 results for index InternalPageCountIndex,*

And according to the studio query, the server still tells me half of my index is stale :(

![stale indexes](https://i.stack.imgur.com/RQ7jm.png)