views:

308

answers:

3

My question is what can I do to determine the cause of the slowness, or what can I do to speed it up without knowing the exact cause.

I am running a simple query and it appears that the mapping back to the entities is taking taking forever. The result set is 350, which is not much data in my opinion.

IRepository repo = ObjectFactory.GetInstance<IRepository>();
var q =  repo.Query<Order>(item => item.Ordereddate > DateTime.Now.AddDays(-40));

foreach (var order in q)
{
    Console.WriteLine(order.TransactionNumber);
}

The profiler is telling me it is executing the query 7ms / 35257ms, I am assuming that the former is the actual response from the db and the latter is the time it takes NH to do it's magic.

35 seconds is too long. This is a simple mapping, one table, nested components, using fluent interface to do mappings. I just start up a simple console app and run the one query, the slowness is measured after the SessionFactory is initialized, there should only be one session, and I am not using a transaction.

Thanks

+2  A: 

If your database query is returning quickly, then the problem is somewhere in the code itself. It's unlikely to be in NHibernate. Your comments imply that you are using custom user types, and it may very well be that there's something particularly inefficient in there. I am guessing you are using NHProf as your profiler? Perhaps you should use a straight .NET profiler and look at the performance of the code itself, including these custom types? Something like Ants, dotTrace or AQTime.

David M
Correct on the profiler. I guess that is a good suggestion, i haven't used this as I just assumed it was me configuring NHibernate incorrectly since I am pretty new to it.
Rob A
Unlikely - it's something slow in the code. Try a profiler like this against it. dotTrace has a 10 day trial and is fairly easy to use.
David M
Well, i used the default profiler in visual studio and timing resulted in 197ms / 543ms. ran the app outside of visual studio and it was just as fast. Any ideas on why it is so slow inside vs?
Rob A
Is it churning out lots of debug SQL statements to the debug window? Try setting showSql to false when you configure the session factory.
David M
Or, do you have a slow logger like a database logger configured using log4net for debug output? This, combined with showSql=true, would slow your code down a fair bit as well.
David M
A: 

It is likely that you are doing something in your user types that takes a lot of time

Ayende Rahien
A: 

Try disabling log4net. If it speeds things up and you still want to use it for other purposes, disable only nhibernate.

Terlisimo