nHibernate performance issue when loading large collections

后端 未结 3 1219
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 02:37

(just to make clear: my app isn\'t really about employees and departments. I just use these terms for example\'s sake).
Each department has an employees collection, whic

相关标签:
3条回答
  • 2020-12-12 02:55

    hmmmm. may be its too much, and actually - i'd expect an "lazy=extra" ISet to behave like this, but you can write your own "extra lazy" ISet. if you didn't encounter an extra lazy collection - its a collection that, for example, when you ask its count, it doesn't fetch everything but issues a count query. your extra lazy ISet could issue an exists query whenever you try to add something.

    If you implement this, your code would be clean and you could submit the code to nhibernate core.

    you should however think about add range, and be careful not to issue N queries

    good luck.

    0 讨论(0)
  • 2020-12-12 03:18

    There is no reason for you to load all the empoyees to memory. you should write a query using HQL/Critiria API/Linq to NHibernate to check if the employee already existing in the DB. for example:

    var existingEmpoyee = session.Query<Employee>()
                                 .Where(e => e.Equals(newEmployee))
                                 .FirstOrDefault();
    if(existingEmployee != null)
       // Insert new employee to DB
    
    0 讨论(0)
  • 2020-12-12 03:18

    I would use a StatelessSession and batch optimization.

    The session will keep track of all the loaded objects, and if we load a lot of data, it will eventually blow out with an out of memory exception. Luckily, NHibernate has a ready made solution for this, the stateless session. The code now looks like this:

    using (IStatelessSession s = sessionFactory.OpenStatelessSession())
    {
        var books = new ActionableList<Book>(book => Console.WriteLine(book.Name));
        s.CreateQuery("from Book")
            .List(books);
    
    }
    

    The stateless session, unlike the normal NHibernate session, doesn’t keep track of loaded objects, so the code here and the data reader code are essentially the same thing.

    For batch optimization and more: NHibernate performance tricks.

    0 讨论(0)
提交回复
热议问题