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:

(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!)

This article is part of the GWB Archives. Original Author: Billy McCafferty

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.