title: How to use ToFuture in nhibernate?
body: <p>Say I have these queries</p>
  
  <pre><code>// Linq 
      var allRecords = session.Query&lt;Table1P&gt;().FetchMany(x =&gt; x.Table2).FetchMany(x =&gt; x.Table3).FetchMany(x =&gt; x.Table4).FetchMany(x =&gt; 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";
  </code></pre>
  
  <p>In the cases above I need to do all these joins to stop lazy loading and do eager loading. Now this would produce an alert in the nhibernate profiler about too many joins.</p>
  
  <p><a href="http://nhprof.com/Learn/Alerts/TooManyJoins" rel="nofollow">http://nhprof.com/Learn/Alerts/TooManyJoins</a></p>
  
  <p>So one way to stop this alert is to break up the joins. and do multiple queries. This is what I am trying to do. However at the same time I been reading about the future feature that I think would allow me to break up the query(to stop too many joins) but still execute it in one go.</p>
  
  <p><em><strong>Edit</em></strong></p>
  
  <p>I been trying ToFuture and finding it slower when trying to limit the amount of data to come back(if I try to get everything back then it is faster as it does not crash).</p>
  
  <pre><code>  var allRecords = session.Query&lt;Table1P&gt;().FetchMany(x =&gt; x.Table2).FetchMany(x =&gt; x.Table3).FetchMany(x =&gt; x.Table4).FetchMany(x =&gt; x.Table6).Take(3000).ToList();
  
  // result (2.731 seconds), row count 3000, duration 8 ms/ 2013 ms
  
  select TOP (3000 /* @p0 */) table1p0_.Id        as Id1_0_,
                   table2x1_.Id        as Id2_1_,
                   table3x2_.Id        as Id3_2_,
                   table4x3_.Id        as Id4_3_,
                   table6x4_.Id        as Id6_4_,
                   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__,
                   table3x2_.F1        as F2_3_2_,
                   table3x2_.F2        as F3_3_2_,
                   table3x2_.F3        as F4_3_2_,
                   table3x2_.Table1_id as Table5_3_2_,
                   table3x2_.Table1_id as Table5_1__,
                   table3x2_.Id        as Id1__,
                   table4x3_.F1        as F2_4_3_,
                   table4x3_.F2        as F3_4_3_,
                   table4x3_.F3        as F4_4_3_,
                   table4x3_.Table1_id as Table5_4_3_,
                   table4x3_.Table1_id as Table5_2__,
                   table4x3_.Id        as Id2__,
                   table6x4_.F1        as F2_6_4_,
                   table6x4_.F2        as F3_6_4_,
                   table6x4_.F3        as F4_6_4_,
                   table6x4_.Table1_id as Table5_6_4_,
                   table6x4_.Table1_id as Table5_3__,
                   table6x4_.Id        as Id3__
  from   Table1 table1p0_
         left outer join [Table2] table2x1_
           on table1p0_.Id = table2x1_.Table1_id
         left outer join [Table3] table3x2_
           on table1p0_.Id = table3x2_.Table1_id
         left outer join [Table4] table4x3_
           on table1p0_.Id = table4x3_.Table1_id
         left outer join [Table6] table6x4_
           on table1p0_.Id = table6x4_.Table1_id
  </code></pre>
  
  <p>Using ToFuture</p>
  
  <pre><code>// result (4.808 seconds), row count 6944 / 75/ 374, duration 1ms / 4022 ms
  
                  // will load the Entities with Table2 fetched
                  var allRecords = session.Query&lt;Table1P&gt;()
                      .FetchMany(x =&gt; x.Table2)
                      .ToFuture()
                      .Take(3000);
  
                  // will load the Entities with Table3 fetched
                  session.Query&lt;Table1P&gt;()
                      .FetchMany(x =&gt; x.Table3)
                      .ToFuture();
  
                  // will load the Entities with Table4 fetched
                  session.Query&lt;Table1P&gt;()
                      .FetchMany(x =&gt; x.Table4)
                      .ToFuture();
  
                  // will load the Entities with Table6 fetched
                  session.Query&lt;Table1P&gt;()
                      .FetchMany(x =&gt; 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;
  </code></pre>
  
  <p>Queries and times taken from nhibernate profiler. Test was run by running it once with one of the sets of code, recording the results, stopping the .net development server and then running the other set of code.</p>
  
  <p>Table1 - 6200 rows
  Table2 - 744 rows
  Table3 - 75 rows
  Table4 - 374 rows
  Table6 - 300 rows</p>
  
  <p>Pretty much Table 1 has a one to many to all the other tables. </p>