views:

40

answers:

3

How do you optimize ActiveRecord calls in your ASP.NET MVC 2 web applications ?

I'm sitting in front of my project and all is fine until I start to fill in data. Like a lot of projects I have a data structure similar to this:

A planet has many countries. A country has many states/provinces. A State has many cities. A city has many neighborhoods.

Below an example of Castle ActiveRecord/NHibernate persistent business object

[ActiveRecord]
public class Country {

    [Property]
    public String CountryName { get; set; }

    [HasMany(typeof(States))]
    public IList<State> States { get; set; }
}

Now suppose you want to do an innocent request like getting a list of all the countries on the planet

By default, ActiveRecord/Nhibernate will load the entire tree, until the very last dependency.

It can turn out to be A LOT of SQL calls.

Okay, we can solve that with lazy loading [ActiveRecord(lazy=true)] but then whenever you want to do something neat like below

String text = "This country of " + country.CountryName + " has the following states:";

foreach(State s in country.States)
{
    text += s.StateName + " ";
}

With the lazy load activerecord attribute every time it fetches for s.StateName it's another sql call.

That's way too many sql calls.

Some friends of mine suggested using ActiveRecordBase methods such as FindAll(criteria).

But it ends up being really ugly and hard to read with all those Expression.Eq and what not.

And then other people also suggested using something like HQL. HQL looks A LOT like SQL.

So bottom line, it seems like the most clean and optimized way is to write plain and simple SQL queries. It goes straight to the point.

SELECT * FROM Countries //No loading of an entire tree of dependencies

SELECT * FROM States WHERE CountryId = @selectedId   //1 call and you have all your states

Anyways, for now I'm using SQL queries and stored procedures for fetching data and ActiveRecord for saving/deleting objects.

Please correct me if I'm wrong... ?

Thanks Appreciated.

+1  A: 

That sounds really weird, sorry to say!

One big advantage of ORM's is that you can abstract your database away...! So, yes, HQL may feel, look and sound like SQL, but it has one big difference: it is DBMS independent. The same HQL will work for SQL Server, or Oracle, or MySQL...

You will also need some work for sharing your database connection between NHibernate and SQL Server. Feasible, but a little bit ugly.

I would go with HQL without even thinking about it. Sometimes you really need to get to a lower level and write some SQL queries, but the scenario you're describing is far from being in that case.

OTOH, on real world scenarios you'll probably need pagination for displaying so much data. And if that's the case, the lazy approach will be much better, even if it potentially generates much more SQL statements.

Just think about it: if a user queries a result set of 1,000,000 records, but he/she is seeing just the first 20 entries on a web page, I don't need to fetch ALL 1,000,000 records from the database. And that's exactly what lazy loading is meant for...

Edit: almost forgot NHibernate first and second level caching. Using this your application may avoid querying the DBMS layer. That alone may translate in a HUGE performance boost...

rsenna
Hmm, I understand your point. However, in my case, it will always be SQL SERVER. Also I already abstract all the queries inside data access objects. If for some reason I need to change database, the only thing I would need to rewrite would be the data access objects.Performance wise I'd still recommend to write your queries in SQL entirely. Especially in the scenario where your proprietary database won't change for the next 5 years.
Tchi Yuan
Heh, being there, done that. I can only say this is a decision you'll probably regret. There are ways of getting better performance from HHibernate (like NHibernate Profiler), and you WILL lose lazy loading by using plain SQL queries (which is a very performance un-wise decision). Remember, the Java world has already proven that you GAIN performance by using ORM's on real world scenarios. But be my guest... :)
rsenna
In my case, performance is of utmost importance...The scenario I presented in my original post is a simplified version of the complexity of my data structures.
Tchi Yuan
Well, if you are that much worried about performance, you could consider using a NoSQL approach, like RavenDB or MongoDB. A very "extreme" action, but with nice results. Please take a look at http://stackoverflow.com/questions/1777103/what-nosql-solutions-are-out-there-for-net
rsenna
Insightful thank you! I'll take a look at it but not planning to use it.
Tchi Yuan
+1  A: 

This is a well-known issue with ORMs. One way to solve this is to add the BatchSize nhibernate setting so that each trip to the db you bring back n records (States in this case) instead of one.

[HasMany(typeof(States), BatchSize = 20)]
public IList<State> States { get; set; }
Gabe Moothart
Insightful thank you!
Tchi Yuan
+3  A: 

The recommended way is using lazy mappings by default, then eagerly fetch what you need for each case.

If you don't use lazy by default, you fetch pretty much the whole database in each query, as you already noticed.

If you use lazy but don't fetch eagerly, you run into the SELECT N+1 issue you noticed in your foreach example.

Everything I mentioned so far is independent of the query API you use. You can do all of this using either HQL, Criteria, Linq or QueryOver. Some queries are easier to express in HQL, and some other times it's more convenient to use Criteria, Linq or QueryOver. But again, this is an orthogonal question.

Also, this isn't so different from a SQL query. In your example, if you wanted a country with its states you would have INNER JOINed to get all states beforehand instead of issuing individual SELECts for each state.

Mauricio Scheffer
Bull's eye! Thank you!
Tchi Yuan