My problem is that NHibernate gets exponentially slow when fetching records from the database. I had a request to basically pull all the data from a very large database to be used in a report.
I figured, well since I can’t get all the records in one shot because the recordset is so large, i thought try breaking it up. Basically I’m iterating through ranges of an index, ie. records id x to y, then y+1 to z, and so forth.
Each result set is about 10megs. The first 20 or so pulls takes less than a minute each, then on the next pull, it takes 10minutes, then 30minutes, and 1hr. I stopped the program there, didn’t want to wait till the next pull will come. I ran the program again starting from the index where I left off, again, the first 20 or so pulls are really quick, then for some odd reason there is a major slowdown.
Any help would be greatly appreciated.
NHibernate is doing a lot of work for you, and that might be slowing it down. It keeps objects you’ve pulled from in the session, which is basically a first level cache. When you run queries, it will check that set of objects to see if they need to be flushed back to the database (maybe you’ve changed them so that they now qualify as the result of the query!). So one strong possibility is that your query is secretly walking through a huge number of objects trying to figure out what it should write to the database before it ever sends your query to the database.
If this is the case, you can try a periodic session.Clear() or session.Evict(object) which removes objects from the cache, so that NH won’t try to make the database consistent with them. You can always reattach objects later if you want NH to “pay attention” to them.
Edit: or use a stateless session. Here’s an example.