直到最近这个查询才对我有用。现在,我的 RavenDB 中有 135 个 InstallationSummary 文档。它不是按开始时间获取最新的文档,而是大部分工作,但最后几个、最新的文档并未从此查询中显示。是我查询错误吗?是否有我应该注意的使用 RavenDB 进行 OrderByDescending 和 Take 的不同方法?正确查询的文档数量是否有限制?
注意:我已经对此进行了调试,并且查询确实返回了我们在网格中看到的内容。查询运行时间与 UI 中显示的时间之间没有进行任何转换。
IEnumerable<InstallationSummary> installationSummaries =
QueryAndCacheEtags(session => session.Advanced.LuceneQuery<InstallationSummary>()
.Include(x => x.ApplicationServerId)
.Include(x => x.ApplicationWithOverrideVariableGroup.ApplicationId)
.Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroupId)
.OrderByDescending(summary => summary.InstallationStart)
.Take(numberToRetrieve)).Cast<InstallationSummary>().ToList();
此网格应显示更多行,其开始时间大于 1/19/2012 6:33:51 PM:
编辑:我从查询中删除了 Take(numberToRetrieve),并且我只获得了总共 160 个 InstallationSummary 文档中的 128 个。我可以在 RavenDB Studio 中看到所有 160 个,但查询中只返回 128 个。 128... 128... 2 的幂... 我是否达到了某个极限?
好吧,看起来我确实达到了 128 的限制:
http://www.blogcoward.com/archive/2010/05/21/RavenDB-and-a-brief-design-philosophy-discussion-with-Ayende.aspx
http://codeofrob.com/archive/2010/ 05/12/ravendb-basic-usage-considerations.aspx
但为什么呢?我在那里有一个 Take() 方法。我该如何获取 50 个最新文档?
作为一个黑客,下面的查询至少会显示最新的。这并不完全是我想要的,因为我想要最近的 50 个,无论日期如何。只要自开始日期以来不超过 50 个,这至少会显示最新的项目。
using Raven.Client.Linq;
DateTime startDate = new DateTime(2012, 1, 18);
IEnumerable<InstallationSummary> installationSummaries =
QueryAndCacheEtags(session => session.Query<InstallationSummary>()
.Include(x => x.ApplicationServerId)
.Include(x => x.ApplicationWithOverrideVariableGroup.ApplicationId)
.Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroupId)
.Where(x => x.InstallationStart > startDate)
.OrderByDescending(summary => summary.InstallationStart)
.Take(numberToRetrieve)
).Cast<InstallationSummary>().ToList();
我必须从 LuceneQuery 改为仅查询,并且必须添加Where 子句。
This query was working for me until recently. I now have 135 InstallationSummary documents in my RavenDB. Instead of getting the most recent by start time, it's mostly working, but the last couple, most recent documents aren't showing up from this query. Am I querying incorrectly? Is there a different way to do OrderByDescending and Take with RavenDB that I should be aware of? Is there a document number limit to what I can query correctly?
Note: I have debugged this, and the query indeed returns what we see in the grid. There is no transformation done between the time the query is run and what is shown in the UI.
IEnumerable<InstallationSummary> installationSummaries =
QueryAndCacheEtags(session => session.Advanced.LuceneQuery<InstallationSummary>()
.Include(x => x.ApplicationServerId)
.Include(x => x.ApplicationWithOverrideVariableGroup.ApplicationId)
.Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroupId)
.OrderByDescending(summary => summary.InstallationStart)
.Take(numberToRetrieve)).Cast<InstallationSummary>().ToList();
This grid should show a few more rows in it with start times greater than 1/19/2012 6:33:51 PM:
Edit: I removed Take(numberToRetrieve) from the query, and I'm only getting 128 of the total 160 InstallationSummary documents. I can see all 160 in RavenDB Studio, but only 128 return from the query. 128... 128... power of 2... Did I hit some limit?
Okay, it looks like I did hit the limit of 128:
http://www.blogcoward.com/archive/2010/05/21/RavenDB-and-a-brief-design-philosophy-discussion-with-Ayende.aspx
http://codeofrob.com/archive/2010/05/12/ravendb-basic-usage-considerations.aspx
But why? I have a Take() method in there. How am I supposed to get the 50 most recent documents?
As a bit of a hack, the query below will at least show the most recent. It isn't exactly what I want, because I want the most recent 50, regardless of date. As long as there aren't more than 50 since the start date, this will at least show the most recent items.
using Raven.Client.Linq;
DateTime startDate = new DateTime(2012, 1, 18);
IEnumerable<InstallationSummary> installationSummaries =
QueryAndCacheEtags(session => session.Query<InstallationSummary>()
.Include(x => x.ApplicationServerId)
.Include(x => x.ApplicationWithOverrideVariableGroup.ApplicationId)
.Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroupId)
.Where(x => x.InstallationStart > startDate)
.OrderByDescending(summary => summary.InstallationStart)
.Take(numberToRetrieve)
).Cast<InstallationSummary>().ToList();
I had to go from a LuceneQuery to just Query and I had to add the Where clause.
发布评论
评论(2)
终于解决了真正的问题。
QueryAndCacheEtags(..) 函数的签名是
Func
,而不是Expression>
。并且它返回IEnumerable
而不是IQueryable
这会将语句从
IQueryable
转换为IEnumerable
那时。这意味着RavenDB服务器只处理查询的第一部分,没有过滤或排序。其余的语句然后在内存中应用到您返回的 128 个项目。这就是为什么您没有看到正确订购或过滤的项目。
此处和此处
一般来说,您不必担心之间的差异
Func
和Expression>
,编译器会为您处理它。但是,如果您将自己的函数调用引入到 LINQ 语句中,则确实需要正确处理。Finally worked out the real issue.
The signature of the QueryAndCacheEtags(..) function is
Func<T>
, notExpression<Func<T>>
. And it returnsIEnumerable<T>
notIQueryable<T>
This converts the statement from
IQueryable<T>
toIEnumerable<T>
at that point. This means that the RavenDB server only processes the first part of the query, which has no filtering or ordering.The rest of the statements and then applied in-memory to the 128 items you get back. Hence why you aren't seeing the item ordered or filtered properly.
There's a bit more info here and here
Generally you don't have to worry about the difference between
Func<T>
andExpression<Func<T>>
, the compiler handles it for you. But if you introduce your own function call into a LINQ statement, you do need to get it right.RavenDB 默认使用最终一致性,因此索引可以是 stale,除非您明确指定否则。
将下面的行(或其变体之一)添加到您的查询中:
RavenDB uses eventual consistency by default, so indexes can be stale unless you explicitly specify otherwise.
Add the line below (or one of it's variants) to your query to this: