2012-07-26 21 views
4

我有一個50k數據塊。我使用NHibernate檢索所有(檢索全部是必要的)。但是,由於連接5-7個表所創建的大數據集NHibernate需要大約一分鐘。 緩慢抓取的主要原因可能是連接NHibernate爲每個表中的每行創建查詢的表。我明白這是必要的,因爲NHibernate需要將每一行映射到一個對象,但這個開銷必須被刪除。使用NHibernate性能問題獲取大塊數據

有什麼辦法可以在BLOCK中獲取數據,然後使用NHibernate創建對象。 我包括我的映射文件以及代碼 -

的App.config

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
     <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> 
    </configSections> 
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 

     <bytecode-provider type="lcg"/> 
     <reflection-optimizer use="true"/> 
     <session-factory> 
      <property name="connection.provider" > 
       NHibernate.Connection.DriverConnectionProvider 
      </property> 
      <property name="connection.driver_class"> 
       NHibernate.Driver.SqlClientDriver 
      </property> 
      <property name="connection.connection_string"> 
       Data Source=dewashish-pc\sqlexpress;Initial Catalog=NHibernateTest;Integrated Security=True; 
      </property> 
      <property name="dialect"> 
       NHibernate.Dialect.MsSql2005Dialect 
      </property> 
      <property name="show_sql"> 
       false 
      </property> 
      <property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> 

     </session-factory> 
    </hibernate-configuration> 
</configuration> 

Branch.hbm.xml

<?xml version="1.0" encoding="utf-8"?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample" namespace="NHibernateSample"> 
<class name="Branch" table="Branch"> 
    <id name="BranchCode"/> 
    <property name="BranchCode"/> 
    <property name="BranchName"/> 
    <bag name="EmployeeList" cascade="all-delete-orphan" inverse="false" fetch="join" lazy="false"> 
     <key column="BranchCode"/> 
     <one-to-many class="Employee" /> 
    </bag> 
</class> 

</hibernate-mapping> 

Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample" namespace="NHibernateSample"> 
    <class name="Employee" table="Employee"> 
     <id name="EmployeeId"/> 
     <property name="EmployeeId"/> 
     <property name="FirstName"/> 
     <property name="LastName"/> 
     <property name="BranchCode"/> 
    </class> 
</hibernate-mapping> 

Banch.cs

using System.Collections.Generic; 
using System.Text; 
using System; 


namespace NHibernateSample 
{ 
    [Serializable] 
    public class Branch 
    { 
     private String branchCode; 
     private String branchName; 
     private IList<Employee> employeeList = new List<Employee>(); 

     public virtual IList<Employee> EmployeeList 
     { 
      get { return employeeList; } 
      set { employeeList = value; } 
     } 
     public virtual String BranchCode 
     { 
      get { return branchCode; } 
      set { branchCode = value; } 
     } 

     public virtual String BranchName 
     { 
      get { return branchName; } 
      set { branchName = value; } 
     } 

     public Branch() { } 
    } 
} 

Employee.cs

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace NHibernateSample 
{ 
    public class Employee 
    { 
     String employeeId; 
     String firstName; 
     String lastName; 
     String branchCode; 

     public virtual String EmployeeId 
     { 
      get { return employeeId; } 
      set { employeeId = value; } 
     } 

     public virtual String FirstName 
     { 
      get { return firstName; } 
      set { firstName = value; } 
     } 

     public virtual String LastName 
     { 
      get { return lastName; } 
      set { lastName = value; } 
     } 

     public virtual String BranchCode 
     { 
      get { return branchCode; } 
      set { branchCode = value; } 
     } 

     public Employee() 
     { } 
    } 
} 

Form1.cs的

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using NHibernate; 
using NHibernate.Cfg; 
using System.Reflection; 
using System.Collections; 

namespace NHibernateSample 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      ConfigureNHibernate(); 
      LoadData(); 
     } 

     static ISessionFactory SessionFactory; 
     System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); 


     private void LoadData() 
     { 
      sw.Start(); 

      using (ISession session = SessionFactory.OpenSession()) 
      { 
       long b = sw.ElapsedMilliseconds; 
       try 
       { 

        if (session.IsConnected) 
        { 
         // as br order by br.BranchCode asc 
         IQuery query = session.CreateQuery("from Branch"); 
         IList<Branch> iList = query.List<Branch>(); 
         dvData.DataSource = iList; 
         int a = 0; 
         foreach (Branch br in iList) 
         { 
          a++; 
         } 
         MessageBox.Show(((sw.ElapsedMilliseconds - b)) + " - MilliSeconds to fetch " + System.Environment.NewLine + a.ToString() + " - Rows"); 
        } 

       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 

       } 
      } 
     } 


     private void ConfigureNHibernate() 
     { 
      try 
      { 
       Configuration cfg = new Configuration(); 
       cfg.Configure(); 

       Assembly allocationAssembly = typeof(Branch).Assembly; 
       cfg.AddAssembly(allocationAssembly); 

       SessionFactory = cfg.BuildSessionFactory(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 

    } 
} 

因爲我沒有我不能發佈的SQL事件探查器的圖像足夠的聲譽,但會在dem上提供和。 謝謝....

+0

我認爲唯一的辦法就是爲此編寫HQL。 – shajivk 2012-07-26 09:56:08

+0

請您詳細說明一下。謝謝。 – 2012-07-26 10:04:28

+1

我們可以這樣寫'var query = this.Session.CreateQuery(@「從Employee作爲內部聯接e.Branch作爲b與b.BranchName =:branchName」);'。有關HQL的一些信息可以在這裏找到「http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-joins」 – shajivk 2012-07-26 10:33:56

回答

1

另一種方法是使用一個輕量級的微ORM,如Dapper與DB的任何大數據/高頻率通信。

它會顯着提高您的性能。

+0

我一定會檢查這個ORM的數據複雜性。 – 2012-12-04 19:51:31