Blog Stats
  • Posts - 18
  • Articles - 0
  • Comments - 16
  • Trackbacks - 79

 

Giving NHibernate a serious performance boost

When NHibernate prepares SQL queries for SQL Server, it passes them to sp_execsql.  Upon first call, the query speed is lackluster as it parses and executes the SQL; on subsequent calls with the same query syntax, the speed gets a bit faster.  But even on subsequent calls, the response time is much slower than that of a stored procedure.  The problem is, by default, NHibernate does not fully qualify table names within its queries sent to SQL Server.  To leverage sp_execsql to its fullest, requests must include fully qualified table names.

You can fully qualify NHibernate queries within your app/web.config by including the following attribute in the nhibernate settings:

<add key=“hibernate.default_schema“ value=“your_DB_name.dbo“ />

(Obviously, “dbo“ should also be replaced as necessary.)

How big of a difference does this make?  We're realizing an order of magnitude difference in the execution speed of queries called subsequent times.  A test query originally costing 255 reads went down to 12 reads.  On more data intensive pages, this can result in an overall performance gain of 33% or more.

(A big thanks out to Pete Weissbrod for researching this!)


Feedback

# re: Giving NHibernate a serious performance boost

Gravatar can you explain how you were testing the number of reads? i would like to test in my app and share the results with you. 4/1/2006 7:03 AM | sean

# re: Giving NHibernate a serious performance boost

Gravatar From Pete Weissbrod, another developer I work with who specializes in performance... Perfmon (using the sql counters) and SQL profiler are great tools for
identifying bottlenecks in the data layer. Perfmon's sql:lock counter serves
as a birds eye view for finding potential scalability issues. SQL profiler
allows you to examine speed costs on a query-by-query basis.
For effective performance tests, get the application and the db in an
ISOLATED environment (i.e. your local laptop if possible). Run the debugger
on a transaction of interest in one window. In another window, run sql
profiler, watching the local db. Break before queries. The profiler will
display to you the actual sql that nhibernate is sending to the dbms, along
with the time in ms for execution, and the number of reads necessary to
complete.
The number of reads refers to the number of times the dbms had to "traverse"
its data hierarchy to get to the correct data. In sql server, data is stored
in a b*-tree (multiple indexing), where each step down the tree is a "read"
(index seek). If searching on a non-indexed value, the dbms is forced to do
a naïve "scan" thereby increasing the number of reads. Take one of the
expensive queries from profiler and use query analyzer to see the "plan";
this will tell you if you should consider adding a new index to avoid
"scanning". Optimizing the query is sometimes a matter of coaxing the
optimizer to run the query in the way you envision. 4/3/2006 8:28 AM | Billy

# re: Giving NHibernate a serious performance boost

Gravatar Was this against sql server 2005!? 7/31/2006 7:48 AM | chad

# re: Giving NHibernate a serious performance boost

Gravatar SQL 2000 actually. I haven't actually done anything with NHibernate/SQL 2005. I assume you may have some comments on the subject?

Billy 8/1/2006 8:50 AM | Billy

# re: Giving NHibernate a serious performance boost

Gravatar I tried this and my site became 33% slower..

Are you sure this is the right way?

I added this in nhibernate.config file in properties section. 9/20/2006 10:56 PM | Prashant

# re: Giving NHibernate a serious performance boost

Gravatar Would this setting affect MySQL as well ?

If so, what would be the value of the setting? Just the database name, since there is no dbo thing in mysql...

Thanks 12/28/2006 2:07 PM | Martin Bittner

# re: Giving NHibernate a serious performance boost

Gravatar Although I can't attest to the performance gain for MySQL, I'd recommend adding it in, nonetheless. (And please let me know if you end up doing any benchmarking to confirm a performance difference!)
1/3/2007 2:14 PM | Billy

# NHibernate ja sp_execsql

Gravatar Billy McCafferty jagab oma blogis head n&#245;u NHibernate-i j&#245;udluse parandamiseks andmebaasidega, kus koormus on v&#245;rreldes tavaliste rahulike veebikeste ja intranetidega suhteliselt &#228;&#228;rmuslik. See trikk ei puutugi ehk otseselt niiv&#228;ga NHibernate-i k... 5/22/2007 5:13 PM | DT blogi

Post a comment





 

Please add 4 and 8 and type the answer here:

 

 

Copyright © Billy McCafferty