Bound FetchMany in Linq to NHibernate

*爱你&永不变心* 提交于 2019-12-11 01:55:28

问题


I am using FetchMany for some of my queries and the NHibernate profiler gives me the following error:

WARN:
firstResult/maxResults specified with collection fetch; applying in memory!

I guess this is because the fetch is unbound. Is there a solution to this?


回答1:


This problem arises because using FetchMany will bring the whole result set to memory and then Take the specified subset (something inefficient and potentially dangerous).

Apparently there is no way to limit the subset before fetching when using FetchMany.

The solution is to use either a JoinQueryOver or a JoinAlias (differences of the two have been discussed in other SO questions)

So insted of doing

Session.QueryOver<Product>().FetchMany(p=>p.Images).Take(5)

which produces the warning in the question, we do

Image image = null;
Session.QueryOver<Product>().Left.JoinQueryOver(x => x.Images, () => image).Take(5)

which produces a SELECT TOP (5) query



来源:https://stackoverflow.com/questions/5704311/bound-fetchmany-in-linq-to-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!