Performance differences between LINQ To SQL and NHibernate


In my current project one of the actions I’ve taken is to have the project and team move away from Linq To Sql to NHibernate. There was a multitude of issues that was the basis for this move, some of the main reasons are outlined in my post:“Top 10 reasons to think twice about using Linq To Sql in your project” but there was also others, like the inability to tweak Linq To Sql to perform in different scenarios which often lead to stored procedures. NHibernate have a multitude of buttons to push and can tweak almost every aspect of the data access which gives us more options.

This post is not about the lack of optimizing options in LTS, nor the options in NHibernate. It’s just to illustrate a simple truth; NHibernate is more mature and have had time to optimize the core functionality like object creation (materialization). Compare these two images:

 

Linq To Sql Materialization 703ms -  DB access 7 ms

 

NHIbernate materialization 159 – DB access 7 ms

Unfortunately I can’t show the real model that we loaded but it was an aggregate with a couple of lists which where 3 levels deep.

Another thing to note here: Linq To Sql needed a stored procedure to load this (due to the fact that load spans work really really bad in depths deeper then 2 levels, read more here: LINQ To SQL: Support for Eager Loading, Really? ) NHibernate usages a couple of batched select queries that was built using NHibernate Criteria and the Future feature.

Look at the database execution times, both are 7ms. So can we please kill the “stored procedures are faster then ORM generated queries”-debate now? Each of these scenarios is run on the same database with the same indexes and both had a database that where equally warmed up before the runs.

, , , ,

  1. #1 by Paulo Quicoli on December 20, 2010 - 20:26

    Hi Patrik.

    Man, I’m facing something really hard, as you faced: replace Linq2SQL (from an existing software). Do you have some kind of “recipe”, “steps” to help me get my mind organized… any help or advice is welcomed :)

  2. #2 by Patrik Löwendahl on December 21, 2010 - 00:26

    I’ll see if I can put down some pointers in a blogpost. I’m on vaccation though and it’s christmas so don’t hold your breath :P

  3. #3 by GMz on December 25, 2010 - 10:20

    Did you use CompiledQuery of Linq?

  4. #4 by Patrik Löwendahl on December 25, 2010 - 18:45

    well, we where calling sprocs from Linq To Sql so no. When looking at the instrumentation it was the materialization that took a lot of time.

(will not be published)