NHibernate分页性能(更好的选项) [英] NHibernate Paging performance (Better option)

查看:231
本文介绍了NHibernate分页性能(更好的选项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种简单情况下:

public class Person
{
     public int Id {get;set;}
     public int Name {get;set;}
}

  • 我需要生成具有分页和排序功能的网格
  • 我的数据库大约有10万个人
  • 哪种选项具有更好的性能:

    1),并且在利用NHibernate一级缓存后,第一次获取所有元素,例如:

    1) Get all elemtents in the first time and after take advantage of NHibernate First Level Cache, example:

    personRep.FindAll().OrderBy(s =>s.Name).AsPagination(pageNumber,pageSize);
    

    不好.:AsPagination是一种扩展方法...

    Obs.: AsPagination is a extension method...

    2)仅从数据库中获取实际页面,例如:

    2) Get only the actual page from database, example:

       public virtual IList<T> GetPaginedList(int __pageIndex, int __pageSize,out int __total)
        {            
            var _rowCount = Session.CreateCriteria(typeof(T))
                .SetProjection(Projections.RowCount()).FutureValue<Int32>();
    
            var _results = Session.CreateCriteria(typeof(T))
                .SetFirstResult(__pageIndex * __pageSize)
                .SetMaxResults(__pageSize)
                .Future<T>();
    
            __total = _rowCount.Value; 
            return _results;
        } 
    

    推荐答案

    第二个选项最合适.

    当您(用户)甚至没有使用"所有这些实例时,一次性检索所有实例是没有用的.

    It is useless to retrieve all instances in one go, when you (the user) perhaps doesn't even 'use' all those instances.

    如果"Person"类是具有大量关联的"Heavy"类,那么最好创建一个"PersonView"类,该类仅包含要在网格中显示的属性.

    If the 'Person' class is a 'heavy' class with lots of associations, it would be even better to create a 'PersonView' class, which contains only the properties you want to show in the Grid.

    您不必映射该PersonView类,只需简单地导入"它,以便NHibernate知道它的存在. 然后,在Person类上创建查询,并定义必须使用AliasToBean Transformer来将Person实例转换为PersonView实例.

    You don't have to map that PersonView class, you'll simply have to 'import' it, so that NHibernate knows of its existance. Then, you create a query on the Person class, and define that a AliasToBean Transformer has to be used to convert the Person instances to PersonView instances.

    这样做,NHibernate将能够生成一个查询,该查询仅从数据库中检索必要的列,并使用它填充PersonView实例.

    By doing so, NHibernate will be able to generate a query which only retrieves the necessary columns from the DB, and will populate PersonView instances with it.

    这篇关于NHibernate分页性能(更好的选项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆