Click here to monitor SSC

Laila Lotfi

.NET tools Brand Manager & Simple-Talk Editor

How Slow is 'Slow'?

Published Thursday, May 21, 2009 7:05 PM

One of the most interesting throw-away lines at the recent TechEd show was that Entity Framework was about four times slower than LINQ for SQL, because Entity Framework was a generic solution that could be used with a variety of data stores, and wasn't optimized for SQL Server.

This set us thinking about what the basis on which such statements are made.

LINQ-to-SQL generated a lot of interest from users but "isn't getting the love" at Microsoft because it was always seen as a stop-gap. By contrast, Entity Framework is the apple of the Microsoft eye, but has never received much affection from the users. What most programmers wanted was a simple abstraction layer to allow to them to focus on the 'application domain' and not have to immerse themselves too deeply in the arcane workings of relational databases. The project to provide this simple layer has been fumbled, mainly because there isn't a clear solution for all common application architectures.

The trouble is that abstraction layers come at a cost: they make applications run slower, but how much slower? The message from Microsoft and the MVPs about the architecture of data-driven applications seems confused, possibly because it isn't easy to quantify this performance cost.

All attempts at abstraction, including LINQ, seem to result in a solution that is considerably slower than that which can be achieved through intelligent use of SQLDataReader, or a simple interface based on SQLClient and stored procedures. However, performance comparisons between the various architectures seem to be either anecdotal or dependant on too many variables. Comparisons between LINQ and EF, for example, vary wildly according to whether LINQ queries are pre-compiled, and whether the SQL Query plan is cached and reused. Brian Dawson's benchmarks of Entity Framework against SQLClient and LINQ solutions, which are the most widely-known, illustrate the range of factors that can change the results of timings. For example, he was able to show a 28% performance gain in EFs performance by merely pre-generating the views!

It is extraordinarily difficult to apply a benchmark to the various ORM solutions in a fair and consistent way, but that doesn't mean that the community of users shouldn't be doing it. I'd like to see a production-size application being used, with the full range of timing criteria being decided 'up-front'. In other words, we need to decide before we start on what timings are fair measures of performance, and then stick to that definition. After all, you don't decide where the finishing post is after the horse-race is over, do you?

I'd be fascinated to know what you think about this. What are your experiences of nHibernate, Entity Framework and LINQ to SQL? Do you think that the obvious benefit of a full abstraction layer is really worth the performance cost of all the additional overhead?

Cheers,

Laila

by Laila

Comments

 

damodamodamo said:

Around 2005 I used NHibernate for a reasonably large logistics modeling application.

From memory, the app had about 100 classes with a 'non trival' inheritance hierarchy. In a normal user session, the app would instantiate up to about 10k objects at any given time.

NH promised a lot... and wasn't bad. The thing is though, as soon as the complexity level of the app moved past "college level" (how's that for a measurable benchmark?), NH struggled. I found that, amongst other things, it lacked flexibility in it's lazy loading. What's more, whilst I can't remember the exact details anymore (but they'll be in a forum archive somewhere) I recall finding serious architectural issues in the way that objects were banked / cached. I got into some fairly detailed discussions (ok, arguments) with developers about this where they'd usually take the line, "you don't need to do what you're doing".

The thing is - I most definitely did need to do what I was doing! I recall that I eventually made my point... and all the threads went silent.

In summary: If you're building an app to email your kid's baseball team's newsletter, NH is great. If you're building anything of significance or complexity, build your own object banks and populate them with straight SQL. In a recent project of equal size, I did just that. Using OO features prudently and making use of the awesome collections etc in .net (and now generics!) it was much easier than I thought - and much much easier than using NH!


Just my 2c worth.



DS


May 25, 2009 10:51 PM
 

SzymonPobiega said:

In my opinion the question of what is more performant: Linq2SQL, EDM, NHibernate or any other DAL framework is irrevelant in most cases. The question is which of these is more suitable in the project and allows more robust architecture to be developed.

I think most problems with NHibernate are caused by improper attitude to modeling domain of a problem being solved. Eplicit boudaries around aggregates nad contexts help alot.

It is a fact that Linq2SQL is faster. But is is also a fact that Linq2SQL requires one-to-one table to class mapping which can cause performance problems. Imagine a non-trivial application which requires complex domain model. You can either map it to tables one-to-one and have non-optimal relational schema or use NHibernate or EDM and use complex mapping and have more optimal relational schema. So my opitnion is that Linq2SQL can prohibit performance optimization in a non-trivial app.

The difference between EDM and NHibernate is a more subtle one. It is a matter of promoting a programmig model. NHibernate has POCO suppurt built in. It has no built-in tools for reverse engineering database schema to objects and _has_ a tool do to the oposite: generate the schema from mapping.
EDM, on the other hand, promotes 'database first' approach (simply by having a tool) and an 'anemic model' approch (by exposing all members to the public, by default). Yes, one can build a rich domain model in code with EDM, but is is simply easier to do it the other (evil;-) ) way. And, on the other hand, it is possible to reverse engeneer database to stuct-like claasses in NHibernate.

NHibernate have, in my opinion, a better 'by default' approach.
May 26, 2009 4:21 AM
 

levy.david said:

My name is David Levy, I am the Development manager at Digiknow, we are building a template solution for projects using Entity Framework. We are fond of it and checking all the new features it has and how to take advantage of each them.
We have got to the conclusion that one of the tricks is defining exactly the scope for the Data Context.
I'd like to share with you guys what we are putting together when done so that you can go ahead and review it  on your own and provide insights and thoughts.
Glad to see people are re-searching on which is the best way to go.
We've used NHibernate and it is not bad but honestly If I am using Microsoft I'd rather go with Microsoft products for development.
I'll keep you guys posted.
May 26, 2009 8:45 AM
 

BOB W said:

I agree with damodamodamo in that ORMs don't scale to complex data models or high-performant requirements.

So you're going to need to stick close to SQL for anything serious and avoid ORMs (especially bloatware like the Entity Framework).

LinqToSql is still useful in this context in that you can easily and succinctly build a query that returns you table-mapped objects. These objects should not live long, nor should they live outside your DAL. In other words the advantage of LinqToSql is the Linq syntax and convenience.

Where LinqToSql is slow or generates non-optimal SQL you can hand it the exact query you want and still benefit from the Linq syntax and easy generation of table-mapped objects.

ORMs and massive middleware like the Entity Framework seem appealing to architects and PMs, but they bog down quickly and are a pitfall to avoid.
May 26, 2009 9:09 AM
 

Tony Davis said:

What is wrong with benchmarking software? Not much, you’d have thought. A while back, Laila Lotfi...
September 3, 2009 8:21 AM
You need to sign in to comment on this blog

About Laila

I'm working for the .NET Tools division at Red Gate. These days, I'm spending most of my time working on SmartAssembly (.NET obfuscator, error reporting and feature usage reporting tool), and I'm currently looking to speak to anyone who has used the product for its error reporting technology. My email address: editor@red-gate.com
<May 2009>
SuMoTuWeThFrSa
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456
How to Kill a Company in One Step or Save it in Three
 The majority of companies that suffer a major data loss subsequently go out of business. David Wesley... Read more...

Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across, and start getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...