How to resolve poor nHibernate collection initialization

后端 未结 3 1641
花落未央
花落未央 2021-01-07 05:20

nHibernate3; retrieving 4xxx records out of an EAV data schema. When nHibernate, or .NET, goes to initialize those collections for the first time, we\'re seeing a severe pen

3条回答
  •  盖世英雄少女心
    2021-01-07 05:52

    One option is to enable batch-size on your collections. I assume those are lazy, and with batch size enabled, it would try to fetch collections for multiple entities in a single roundtrip.

    It doesn't make a difference if you fetch 1 entity with one collection, but can make a huge difference if you select 1000 entities which all has one collection. Using a batch-size of 1000 would result in 2 queries instead of 1001.

    Tried to find some documentation, but only found this example:

    nhibernate alternates batch size

    Using join strategies in your case would result in gigantic resultsets so that is not a good option. A better option would be to use FetchMode.Select which would explicitly force your collections to be loaded in a subsequent roundtrip.

    Another thing that could improve performance is setting:

    Session.FlushMode = FlushMode.Never;
    

    Which disable automatic flushing of your scope. This is useful if all you actually do is reading data, not modifying it. However, you would see calls to IsDirty or any other check for dirty objects in your callstack.

提交回复
热议问题