Blog Stats
  • Posts - 18
  • Articles - 0
  • Comments - 583
  • Trackbacks - 69

 

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

# re: Giving NHibernate a serious performance boost

Gravatar Great article, thanks you for sharing 12/28/2009 5:34 AM | unibet

# re: Giving NHibernate a serious performance boost

Gravatar Thanks for sharing ! 1/18/2010 2:29 PM | giochi per ragazze

# re: Giving NHibernate a serious performance boost

Gravatar Thanks a lot my friends 3/9/2010 6:15 PM | psychologue à domicile

# re: Giving NHibernate a serious performance boost

Gravatar Nice, but crazy, thank you my friend 4/25/2010 10:34 AM | ACN

# re: Giving NHibernate a serious performance boost

Gravatar thanks buddy!: 6/3/2010 5:53 PM | paris sportif

# re: Giving NHibernate a serious performance boost

Gravatar that's good thanks!
8/21/2010 9:39 AM | jouer casino

# re: Giving NHibernate a serious performance boost

Gravatar i like your blog 8/21/2010 9:44 AM | machine à sous gratuit

# re: Giving NHibernate a serious performance boost

Gravatar I really loved reading your blog. It was very well authored and easy to understand. Unlike additional blogs I have read which are really not good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he enjoyed it as well! 12/5/2010 9:58 PM | coco chanel

# windows 7 key

Gravatar I find html too complicated, so its really nice of you to post this as I have been looking for html code for setting up a calendar.Office 2010 Key
12/9/2010 1:37 AM | Office 2010 Product Key

# re: Giving NHibernate a serious performance boost

Gravatar
Hello friends, I am new in this blogb. Hope I will enjoy here a lot... 12/12/2010 10:20 AM | jocuri cu hannah montana

# re: Giving NHibernate a serious performance boost

Gravatar NICE BLOG 1/1/2011 5:52 AM | Jocuri pentru copii

# re: Giving NHibernate a serious performance boost

Gravatar i like this blog is verry interesant.. 2/18/2011 5:54 AM | Jocuri de gatit

# re: Giving NHibernate a serious performance boost

Gravatar I liked this blog. Thanks.

Does default_schema with NHibernate 2.0 and Oracle 10g will give performance boost?

Thanks,
Rao 2/23/2011 12:05 PM | Rao

# re: Giving NHibernate a serious performance boost

Gravatar The number of reads refers to the number of times the GAMES 3/11/2011 2:00 PM | skill GAMES

# re: Giving NHibernate a serious performance boost

Gravatar I am new in this SITE. Hope I will enjoy 3/11/2011 2:02 PM | JOCURI LOGICE

# re: Giving NHibernate a serious performance boost

Gravatar Unlike additional blogs I have read which are really not good 3/11/2011 2:04 PM | JOCURI MASINI

# This is my first time i visit here. I discovered so numerous interesting stuff in your weblog especially its discussion. From th

Gravatar interesting post, pretty much covered it all for me, thanks. 3/30/2011 2:38 AM | Windows 7 Key

# re: Giving NHibernate a serious performance boost

Gravatar keep up the wonderful work , I read few articles on this web site and I believe that your web site is rattling interesting and holds lots of superb info 3/31/2011 7:07 PM | onyx cup series

# NoutatioutatiGiving NHibernate a serious performance boost

Gravatar Nice, but crazy, thank you my friend 4/3/2011 12:49 PM | Noutati

# re: Giving NHibernate a serious performance boost

Gravatar Really nice blog post ,I'll come back for new articles. 4/19/2011 10:51 AM | Jocuri cu masini

# re: Giving NHibernate a serious performance boost

Gravatar Interesting article 9/7/2011 5:34 AM | poker freeroll

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

 

 

Copyright © Billy McCafferty