开发者

Mapping speed of NHibernate is really slow - how can I improve it?

开发者 https://www.devze.com 2023-04-01 20:39 出处:网络
I have a simple query that I\'ve run using NHibernate, and plain (my)SQL: SELECT Col1, Col2, Col3, Col4 FROM Data LIMIT 100000;

I have a simple query that I've run using NHibernate, and plain (my)SQL:

SELECT Col1, Col2, Col3, Col4 FROM Data LIMIT 100000;

The query time is around 250ms.

When NHibernate maps the results to a list of entities, it takes around 4.5 seconds. When I do the mapping using datasets/datarows, and row["Col1"] syntax, it takes around 1.5 seconds.

The only d开发者_如何转开发ifference I can see between the two methods is the mapping logic. I have nhibernate diagnostics and caching disabled, and I'm using a stateless session.

My query looks like this:

        using (var session = OpenStatelessSession())
        {
            return session.QueryOver<Data>()
                .Take(100000)
                .List();
        }

And my mapping looks like this:

        Id(x => x.Col1);
        Map(x => x.Col2);
        Map(x => x.Col3);
        Map(x => x.Col4);

How can I improve nhibernate's mapping performance? Perhaps there's something I'm missing?


If you do really have an application that needs to read 4 columns of data, 100,000 rows at a time and performance is critical then don't use NHibernate. However, if that does not resemble the typical database usage of your application then it doesn't really tell you too much. (It's a little like comparing the quarter mile speeds of a motorcycle, a pickup truck and a lorry.)

While there is overhead associated with having an ORM, in real world usage you gain more than you lose. Caching (first and second level), lazy loading, query batching, etc. all deliver performance improvements you simply don't get when you're working closer to the metal.

There have been a number of efforts to benchmark NHibernate with other ORMs and data access strategies (such as this comparison of NHibernate and EF). They are usually controversial and, in my experience using NHibernate for five years, they have little relevance.

Even if you do find areas where you need to tweak performance, it is generally a relatively easy and inexpensive issue to address, when compared to the myriad benefits and cost savings an ORM can bring to application development.

0

精彩评论

暂无评论...
验证码 换一张
取 消