2

我遇到了 NHibernate 3.3 和 Firebird 2.5.1 的性能问题。我使用 ASP.NET MVC 和本地(!)Firebird 数据库创建了一个非常简单的示例。但是下面的代码执行大约需要 1 秒?!?

        var startTickCountWrite = Environment.TickCount;

        IRepository<Project> repository = facade.ProjectRepository(null);
        for (int i = 1; i <= 250; ++i)
        {
            var myProject = new Project { ProjectId = i };
            repository.Insert(myProject);
        }
        repository.Commit();

        var endTickCountWrite = Environment.TickCount;

如果我将 commit() 放在 for 循环中,大约需要 5 秒!

Repository 和外观背后并没有什么特别之处。我只是将项目转发给 ISession.Insert。

Project 只有 ID 和 ProjectID 作为属性。

谁能告诉我出了什么问题?

谢谢, 安德烈亚斯

4

1 回答 1

1

250 objects in 1 second does not sound shockingly slow. For evaluating performance it would also be good to provide some comparison - how long does it take to execute the same amount of SQL directly against Firebird?

NHibernate can in some circumstances batch INSERT statements, but I don't know if this works on Firebird. http://nhibernate.info/doc/nh/en/index.html#performance-batch-updates The choice of identity generator can also have an effect. Some generators force NHibernate to execute the INSERT statement immediately, which will prevent the use of batching.

From a design point of view you might want to change some things: IRepository.Insert() is typically named Add(), since it mimics a collection interface (and Insert on collections, if present, usually take an index parameter which is not relevant here of course). Also, Commit() seems out-of-place on the repository, since you will typically have multiple repository instances involved, that share the same transaction and session.

For accurate time measurements, you can use Stopwatch from System.Diagnostics, so you don't need to convert the value yourself.

于 2012-09-06T21:03:01.913 回答