如何在nhibernate中使用ToFuture?

如何在nhibernate中使用ToFuture?,nhibernate,hql,eager-loading,Nhibernate,Hql,Eager Loading,假设我有这些疑问 // Linq var allRecords = session.Query<Table1P>().FetchMany(x => x.Table2).FetchMany(x => x.Table3).FetchMany(x => x.Table4).FetchMany(x => x.Table6).ToList(); // Hql string hqlQuery = @"from Table1 tbl1

假设我有这些疑问

// Linq 
    var allRecords = session.Query<Table1P>().FetchMany(x => x.Table2).FetchMany(x => x.Table3).FetchMany(x => x.Table4).FetchMany(x => x.Table6).ToList();

// Hql
string hqlQuery = @"from Table1 tbl1
                                left join fetch tbl1.Table2 tbl2
                                left join fetch tbl1.Table3
                                left join fetch tbl1.Table4
                                left join fetch tbl1.Table6
                                left join fetch tbl2.Table5s";
//Linq
var allRecords=session.Query().FetchMany(x=>x.Table2).FetchMany(x=>x.Table3.FetchMany(x=>x.Table4.FetchMany)(x=>x.Table6.ToList();
//Hql
表1 tbl1中的字符串hqlQuery=@
左连接获取tbl1.Table2 tbl2
左连接获取tbl1.3
左连接获取tbl1.4
左连接获取tbl1.6
左连接获取tbl2.Table5s”;
在上述情况下,我需要执行所有这些连接来停止延迟加载并执行快速加载。现在,这将在nhibernate探查器中生成一个关于连接过多的警报

因此,停止此警报的一种方法是断开连接。并执行多个查询。这就是我想做的。然而,与此同时,我一直在阅读有关未来功能的文章,我认为这将允许我分解查询(停止太多的连接),但仍然可以一次性执行它

编辑

我一直在尝试未来,当试图限制返回的数据量时,我发现速度变慢了(如果我尝试返回所有数据,那么速度会更快,因为它不会崩溃)

var allRecords=session.Query().FetchMany(x=>x.Table2).FetchMany(x=>x.Table3).FetchMany(x=>x.Table4).FetchMany(x=>x.Table6).Take(3000).ToList();
//结果(2.731秒),行数3000,持续时间8毫秒/2013毫秒
选择TOP(3000/*@p0*/)表1p0.Id作为Id1\u 0,
表2x1\u.Id为Id2\u 1\u,
表3x2_u2;Id为Id3_2_2;,
表4x3\u.Id为Id4\u 3\u,
表6x4.Id为Id6\u 4,
表1p0.F1为F2\u 1\u 0,
表1p0.F2为F3\u 1\u 0,
表1p0.F3为F4\u 1\u 0,
表2X1.F1为F2_2_1_,
表2X1.F2为F3_2_1_,
表2x1.F3为F4\u 2\u 1,
表2x1\u。表1\u id为表5\u 2\u 1\u,
表2x1\u。表1\u id为表5\u 0\u,
表2X1.Id为Id0,
表3x2.F1为F2\u 3\u 2,
表3x2.F2为F3\u 3\u 2,
表3x2.F3为F4\u 3\u 2,
表3x2.表1\u id为表5\u 3\u 2\u,
表3x2.表1\u id为表5\u 1\u,
表3x2.Id为Id1,
表4x3.F1为F2\u 4\u 3,
表4X3.F2为F3\U 4\U 3,
表4x3.F3为F4\u 4\u 3,
表4x3\u。表1\u id为表5\u 4\u 3\u,
表4x3.表1\u id为表5\u 2\u,
表4x3.Id为Id2,
表6X4.F1为F2\U 6\U 4,
表6X4.F2为F3\U 6\U 4,
表6X4.F3为F4\U 6\U 4,
表6x4。表1作为表5,6,4,
表6x4.表1\u id为表5\u 3\u,
表6x4作为Id3的Id__
从表1到表1p0_
左外连接[表2]表2X1_
在table1p0.Id=table2x1.Table1\u Id上
左外连接[表3]表3x2_
在table1p0.Id=table3x2.Table1\u Id上
左外连接[表4]表4x3_
在表1p0.Id=表4x3\u.Table1\u Id上
左外连接[表6]表6X4_
在表1p0.Id=表6x4.Table1\u Id上
利用未来

// result (4.808 seconds), row count 6944 / 75/ 374, duration 1ms / 4022 ms

                // will load the Entities with Table2 fetched
                var allRecords = session.Query<Table1P>()
                    .FetchMany(x => x.Table2)
                    .ToFuture()
                    .Take(3000);

                // will load the Entities with Table3 fetched
                session.Query<Table1P>()
                    .FetchMany(x => x.Table3)
                    .ToFuture();

                // will load the Entities with Table4 fetched
                session.Query<Table1P>()
                    .FetchMany(x => x.Table4)
                    .ToFuture();

                // will load the Entities with Table6 fetched
                session.Query<Table1P>()
                    .FetchMany(x => x.Table6)
                    .ToFuture();

                // will trigger all queries at once and the session cache will make sure,
                // that the results of all queries fill the same Entities
                allRecords.ToList();


select table1p0_.Id        as Id1_0_,
       table2x1_.Id        as Id2_1_,
       table1p0_.F1        as F2_1_0_,
       table1p0_.F2        as F3_1_0_,
       table1p0_.F3        as F4_1_0_,
       table2x1_.F1        as F2_2_1_,
       table2x1_.F2        as F3_2_1_,
       table2x1_.F3        as F4_2_1_,
       table2x1_.Table1_id as Table5_2_1_,
       table2x1_.Table1_id as Table5_0__,
       table2x1_.Id        as Id0__
from   Table1 table1p0_
       left outer join [Table2] table2x1_
         on table1p0_.Id = table2x1_.Table1_id;



select table1p0_.Id        as Id1_0_,
       table3x1_.Id        as Id3_1_,
       table1p0_.F1        as F2_1_0_,
       table1p0_.F2        as F3_1_0_,
       table1p0_.F3        as F4_1_0_,
       table3x1_.F1        as F2_3_1_,
       table3x1_.F2        as F3_3_1_,
       table3x1_.F3        as F4_3_1_,
       table3x1_.Table1_id as Table5_3_1_,
       table3x1_.Table1_id as Table5_0__,
       table3x1_.Id        as Id0__
from   Table1 table1p0_
       left outer join [Table3] table3x1_
         on table1p0_.Id = table3x1_.Table1_id;



select table1p0_.Id        as Id1_0_,
       table4x1_.Id        as Id4_1_,
       table1p0_.F1        as F2_1_0_,
       table1p0_.F2        as F3_1_0_,
       table1p0_.F3        as F4_1_0_,
       table4x1_.F1        as F2_4_1_,
       table4x1_.F2        as F3_4_1_,
       table4x1_.F3        as F4_4_1_,
       table4x1_.Table1_id as Table5_4_1_,
       table4x1_.Table1_id as Table5_0__,
       table4x1_.Id        as Id0__
from   Table1 table1p0_
       left outer join [Table4] table4x1_
         on table1p0_.Id = table4x1_.Table1_id;



select table1p0_.Id        as Id1_0_,
       table6x1_.Id        as Id6_1_,
       table1p0_.F1        as F2_1_0_,
       table1p0_.F2        as F3_1_0_,
       table1p0_.F3        as F4_1_0_,
       table6x1_.F1        as F2_6_1_,
       table6x1_.F2        as F3_6_1_,
       table6x1_.F3        as F4_6_1_,
       table6x1_.Table1_id as Table5_6_1_,
       table6x1_.Table1_id as Table5_0__,
       table6x1_.Id        as Id0__
from   Table1 table1p0_
       left outer join [Table6] table6x1_
         on table1p0_.Id = table6x1_.Table1_id;
//结果(4.808秒),行数6944/75/374,持续时间1ms/4022 ms
//将加载已获取Table2的实体
var allRecords=session.Query()
.FetchMany(x=>x.Table2)
.ToFuture()
.采取(3000);
//将加载已获取表3的实体
session.Query()
.FetchMany(x=>x.Table3)
.ToFuture();
//将加载已获取Table4的实体
session.Query()
.FetchMany(x=>x.Table4)
.ToFuture();
//将加载已获取表6的实体
session.Query()
.FetchMany(x=>x.Table6)
.ToFuture();
//将立即触发所有查询,会话缓存将确保,
//所有查询的结果填充相同的实体
allRecords.ToList();
选择table1p0\u0.Id作为Id1\u0\u0,
表2x1\u.Id为Id2\u 1\u,
表1p0.F1为F2\u 1\u 0,
表1p0.F2为F3\u 1\u 0,
表1p0.F3为F4\u 1\u 0,
表2X1.F1为F2_2_1_,
表2X1.F2为F3_2_1_,
表2x1.F3为F4\u 2\u 1,
表2x1\u。表1\u id为表5\u 2\u 1\u,
表2x1\u。表1\u id为表5\u 0\u,
表2x1_u2;Id作为Id0__
从表1到表1p0_
左外连接[表2]表2X1_
表1p0.Id=表2X1.Table1\u Id;
选择table1p0\u0.Id作为Id1\u0\u0,
表3x1\u.Id为Id3\u 1\u,
表1p0.F1为F2\u 1\u 0,
表1p0.F2为F3\u 1\u 0,
表1p0.F3为F4\u 1\u 0,
表3x1.F1为F2_3_1_,
表3X1.F2为F3\U 3\U 1,
表3x1.F3为F4\u 3\u 1,
表3X1。表1\u id为表5\u 3\u 1,
表3X1。表1\u id为表5\u 0\u,
表3x1.Id为Id0__
从表1到表1p0_
左外连接[表3]表3X1_
表1p0.Id=表3x1\u1\uID;
选择table1p0\u0.Id作为Id1\u0\u0,
表4x1\u.Id为Id4\u 1\u,
表1p0.F1为F2\u 1\u 0,
表1p0.F2为F3\u 1\u 0,
表1p0.F3为F4\u 1\u 0,
表4X1.F1为F2\U 4\U 1,
表4X1.F2为F3\U 4\U 1,
表4x1.F3为F4\u 4\u 1,
表4X1。表1\u id为表5\u 4\u 1,
表4x1\u。表1\u id a
// result (4.808 seconds), row count 6944 / 75/ 374, duration 1ms / 4022 ms

                // will load the Entities with Table2 fetched
                var allRecords = session.Query<Table1P>()
                    .FetchMany(x => x.Table2)
                    .ToFuture()
                    .Take(3000);

                // will load the Entities with Table3 fetched
                session.Query<Table1P>()
                    .FetchMany(x => x.Table3)
                    .ToFuture();

                // will load the Entities with Table4 fetched
                session.Query<Table1P>()
                    .FetchMany(x => x.Table4)
                    .ToFuture();

                // will load the Entities with Table6 fetched
                session.Query<Table1P>()
                    .FetchMany(x => x.Table6)
                    .ToFuture();

                // will trigger all queries at once and the session cache will make sure,
                // that the results of all queries fill the same Entities
                allRecords.ToList();


select table1p0_.Id        as Id1_0_,
       table2x1_.Id        as Id2_1_,
       table1p0_.F1        as F2_1_0_,
       table1p0_.F2        as F3_1_0_,
       table1p0_.F3        as F4_1_0_,
       table2x1_.F1        as F2_2_1_,
       table2x1_.F2        as F3_2_1_,
       table2x1_.F3        as F4_2_1_,
       table2x1_.Table1_id as Table5_2_1_,
       table2x1_.Table1_id as Table5_0__,
       table2x1_.Id        as Id0__
from   Table1 table1p0_
       left outer join [Table2] table2x1_
         on table1p0_.Id = table2x1_.Table1_id;



select table1p0_.Id        as Id1_0_,
       table3x1_.Id        as Id3_1_,
       table1p0_.F1        as F2_1_0_,
       table1p0_.F2        as F3_1_0_,
       table1p0_.F3        as F4_1_0_,
       table3x1_.F1        as F2_3_1_,
       table3x1_.F2        as F3_3_1_,
       table3x1_.F3        as F4_3_1_,
       table3x1_.Table1_id as Table5_3_1_,
       table3x1_.Table1_id as Table5_0__,
       table3x1_.Id        as Id0__
from   Table1 table1p0_
       left outer join [Table3] table3x1_
         on table1p0_.Id = table3x1_.Table1_id;



select table1p0_.Id        as Id1_0_,
       table4x1_.Id        as Id4_1_,
       table1p0_.F1        as F2_1_0_,
       table1p0_.F2        as F3_1_0_,
       table1p0_.F3        as F4_1_0_,
       table4x1_.F1        as F2_4_1_,
       table4x1_.F2        as F3_4_1_,
       table4x1_.F3        as F4_4_1_,
       table4x1_.Table1_id as Table5_4_1_,
       table4x1_.Table1_id as Table5_0__,
       table4x1_.Id        as Id0__
from   Table1 table1p0_
       left outer join [Table4] table4x1_
         on table1p0_.Id = table4x1_.Table1_id;



select table1p0_.Id        as Id1_0_,
       table6x1_.Id        as Id6_1_,
       table1p0_.F1        as F2_1_0_,
       table1p0_.F2        as F3_1_0_,
       table1p0_.F3        as F4_1_0_,
       table6x1_.F1        as F2_6_1_,
       table6x1_.F2        as F3_6_1_,
       table6x1_.F3        as F4_6_1_,
       table6x1_.Table1_id as Table5_6_1_,
       table6x1_.Table1_id as Table5_0__,
       table6x1_.Id        as Id0__
from   Table1 table1p0_
       left outer join [Table6] table6x1_
         on table1p0_.Id = table6x1_.Table1_id;
// will load the Entities with Table2 fetched
var allRecords = session.Query<Table1P>()
    .FetchMany(x => x.Table2)
    .ToFuture();

// will load the Entities with Table3 fetched
session.Query<Table1P>()
    .FetchMany(x => x.Table3)
    .ToFuture();

// will load the Entities with Table4 fetched
session.Query<Table1P>()
    .FetchMany(x => x.Table4)
    .ToFuture();

// will load the Entities with Table6 fetched
session.Query<Table1P>()
    .FetchMany(x => x.Table6)
    .ToFuture();

// will trigger all queries at once and the session cache will make sure,
// that the results of all queries fill the same Entities
allRecords.ToList();
       .ToFuture()
       .Take(3000);
       .Take(3000);
       .ToFuture()
// Hql
var allRecords = session.CreateQuery("from Table1 left join fetch tbl1.Table2")
    .ToFuture<Table1>();

...