Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 08 | Comments: 18

filter by tags archive

NHibernate Mutli Query Support

time to read 7 min | 1330 words

Anything with Multi in it sounds exciting, except the mutli penalties mode.

Developers are a suckers for performance. Give a developer the promise for a 0.5% performance gain, and they will jump through all number of hoops and go into any number of contractions just to be able to see the end of the rainbow.

One of the questions that I was asked fairly frequently is "how do I get a paged view of the data, including the total count of the rows?" When I try to explain that this is not possible to do this in SQL, they produce the following snippet:

SELECT * FROM Customers;


I then have to explain that while this is a clever idea, this is not possible with NHibernate. And then I hit a case where I really needed NHibernate to send several queries to the database and get the results of all of them. This wasn't quite the case above (I may blog about this later) but it required the same abilities from NHibernate, so I set out to build the Mutli Query for NHibernate.

After a long journey, I finally commited the changes this morning. The new feature is this:

IList results = s.CreateMultiQuery()

       .Add("from Item i where i.Id > :id")

       .Add("select count(*) from Item i where i.Id > :id")

       .SetInt32("id", 50)


You specify several queries, and execute them as one, what is actually is happening is that NHibernate will concatenate the queries, read all the result sets, and understand them correctly. It returns a list of lists, one per each executed query, so the above query results are:

IList items = (IList)results[0];

long count = (long)((IList)results[1])[0];

But you don't have to just pass the query string, you can also pass the query itself, which means that you can configure it in interesting ways, like this example of paging and counting, and if we are at it, let us add caching into the mix:

IList results = s.CreateMultiQuery()

       .Add(s.CreateQuery("from Item i where i.Id > :id")


       .Add(s.CreateQuery("select count(*) from Item i where i.Id > :id") )



Have fun...


Comment preview

Comments have been closed on this topic.


  1. Concurrent max value - 9 hours from now
  2. Production postmortem: The case of the memory eater and high load - 3 days from now
  3. Production postmortem: The case of the lying configuration file - 4 days from now
  4. Production postmortem: The industry at large - 5 days from now
  5. The insidious cost of allocations - 6 days from now

And 5 more posts are pending...

There are posts all the way to Sep 10, 2015


  1. Find the bug (5):
    20 Apr 2011 - Why do I get a Null Reference Exception?
  2. Production postmortem (10):
    14 Aug 2015 - The case of the man in the middle
  3. What is new in RavenDB 3.5 (7):
    12 Aug 2015 - Monitoring support
  4. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats