How to Load the RadGrid when fetching a huge data from the Table of the database using Nhibernate

StackOverflow https://stackoverflow.com/questions/9193626

  •  27-04-2021
  •  | 
  •  

Question

I like to reduce the loading time when running the Default.aspx in the browser because it is fetching a huge data from the Table in the database.

When I use criteria.SetMaxResults(200) the grid load only 200 datas but I should load all the data's in the RadGrid from the table in the database .When I use criteria.SetFetchSize(200) it takes also a huge time to Load can anyone help me to find a solution for this.

Here is the code

    ICriteria criteria = session.CreateCriteria(typeof(Ttable1));
    criteria.SetMaxResults(200);      
    IList result = criteria.List();
    if (result.Count > 0)
    {

        grid1.DataSource = result;
        grid1.DataBind();
    }
    else
    {
        grid1.DataSource = new string[] { };
        grid1.DataBind();
    }

Here the Mapping is

  <class name="Ttable1" table="Ttable1" lazy="false"  mutable="true">
  <cache usage="read-only"/>
  <id name="ID" column="ID" >
  </id> 
  <property name="CustNumber" column="CustNumber" type="String" />
  <property name="CustName" column="CustName" type="String"/>
  <property name="PNo" column="PNor" type="String"/>
  <property name="ONo" column="ONo" type="String"/>
  <property name="Ln" column="Lns" type="String"/>
  <property name="Comments" column="Comments" type="String"/>
  <property name="size" column="size" type="String"/>
  <property name="Qty" column="Qty" type="String"/> 
  </class>
  </hibernate-mapping>

Here is the class using System; using System.Collections.Generic; using System.Linq; using System.Text;

   namespace Business.entities
      {
   public  class Ttable1
        {
    public virtual string displayobj { get; set; }
    public virtual Ttable1 TLQDataObj { get; set; }

    public virtual int?  ID  { get; set; }
    public virtual string CustNumber { get; set; }
    public virtual string CustName  { get; set; }
    public virtual string PNo  { get; set; }
   public virtual string ONo  { get; set; }
 public virtual string Ln { get; set; }
 public virtual string Comments  { get; set; }
  public virtual string size  { get; set; }
  public virtual string Qty  { get; set; }

   }
}
Was it helpful?

Solution

Are you show all data in grid with paging? if yes you can create custom paging for your grid by load 10 rows for every page you entered you can first retrieve paged data by using query.SetFirstResult(firstrowinpage); and query.SetMaxResults(pageSize); (HQL) , and set Radgrid property grid.AllowCustomPaging = true; to allow custom paging and set VirtualItemCount to know grid how number of rows to allow paging.

grdItems.VirtualItemCount =numberofrows();   

OTHER TIPS

It is impossible to tell what is wrong from what you have given us. We don't know your mappings, how many columns does the table have, nothing.

Problems could be caused either by:

  • N+1 selects because all your associations are eager
  • You have some column in the database with huge data (maybe some attachments or something)

You should also try to paginate your results.

You must debug NHibernate queries and see what is going on. You can do that either by using show_sql, by logging with some logger, or with some profiler like NHProf or even a SQL profiler.

You will probably see many queries executed because there is no lazy load enabled.

EDIT:

If you have some columns in the table that you don't want to load unless you access them, you can set their property mapping with lazy=true, or you can map these columns with components and set these components as lazy=true. This way when you access these columns for the first time, additional queries will be performed.

If you need to fetch data for display purposes you should create a light object from the table data. In any case, if fetching speed is your only concern you could set the object as mutable=false and set the cache <cache usage="read-only"/>.

You could do this in code also:

ICriteria crit =  session.CreateCriteria(typeof(Ttable1));
crit.SetCacheable(true);
crit.SetCacheMode(CacheMode.Get);
crit.SetMaxResults(200);      
IList result = crit.List();

Mapping -

<class name="Ttable1" table="Ttable1" lazy="false"  mutable="false">
   <cache usage="read-only"/>
    <id name="ID">
       <column name="ID"/>
       <generator class="identity"/>
    </id>
    <property name="CustNumber" column="CustNumber" type="String" />
    <property name="CustName" column="CustName" type="String"/>
    <property name="PNo" column="PNor" type="String"/>
    <property name="ONo" column="ONo" type="String"/>
    <property name="Ln" column="Lns" type="String"/>
    <property name="Comments" column="Comments" type="String"/>
    <property name="size" column="size" type="String"/>
    <property name="Qty" column="Qty" type="String"/> 
  </class>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top