NHibernate 3.0 で Linq to NHibernate を使用している asp.net Web アプリケーションがあります。
関数では、20 列の 1000 万レコードを含むテーブルから約 20000 レコードを取得する必要があります。
レコードを取得するために Session.QueryOver<>() メソッドを使用しています。
機能コードは次のとおりです。
public IList<BatchDetails> GetBatchRecordsOnBatchId_BatchSize(int batchId,int stratingRowdId,int batchSize)
{
// If the stratingRowdId will be 0 than frist count of the Batch Records equivlent to the batchSize will be return
//If the batchSize will be 0 than all the Batch Records starting from the RowId equivlent to the stratingRowdId will be return
// If both the stratingRowdId & batchSize are 0 than all the BatchReocrds for the BatchId will be return
if (batchId <= 0)
{
throw new ArgumentException();
}
using (var session = _sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
try
{
//Get Batch data from the Database for the BatchId
var batchData = from batchRecords in session.QueryOver<BatchDetails>()
.Where(x => x.BatchId == batchId)
.List().Skip(stratingRowdId).Take(batchSize)
select batchRecords
;
transaction.Commit();
return batchData.ToList();
}
catch (ArgumentException ex)
{
if (transaction != null) transaction.Rollback();
throw;
}
catch (Exception exception)
{
if (transaction != null) transaction.Rollback();
throw;
}
finally
{
session.Flush();
}
}
}
}
このコードは、テーブルに 2 つの lac レコードが含まれるまで機能します。
しかし、テーブルに 10 個の lac レコードを追加した後、このメソッドは次のようなエラーをスローします。
NHibernate.Util.ADOExceptionReporter| タイプ 'System.OutOfMemoryException' の例外がスローされました。
NHibernate.Util.ADOExceptionReporter| タイプ 'System.OutOfMemoryException' の例外がスローされました。
NHibernate.Util.ADOExceptionReporter| タイプ 'System.OutOfMemoryException' の例外がスローされました。
PaymentCurrency は Payment19_2_0_、this_.PaymentDate は Payment20_2_0_、this_.TransactionId は Transac21_2_0_、this_.BatchId は BatchId2_0_ FROM TIOTestDB.dbo.BatchDetails this_ WHERE this_.BatchId = ? ] 位置パラメータ: #0>3 [SQL: this_.RefereId を Referenc1_2_0_ として、this_.AccNumber を AccountN2_2_0_ として、this_.AcStatus を AccountS3_2_0_ として、this_.AcType を AccountT4_2_0_ として、this_.AccSubType を AccountS5_2_0_ として、this_.AccountDescription を AccountD6_2_0_ として、this_. ActivationDate は Activati7_2_0_、this_.CombinedBilling は Combined8_2_0_、this_.PlanAmount は PlanAmount2_0_、this_.PlanCode は PlanCode2_0_、this_.CustomerName は Custome11_2_0_、this_.Email は Custome12_2_0_、this_.Phone は Custome13_2_0_、this_.Address は Customer14_2_0_、 Custome15_2_0_、this_.State は Custome16_2_0_、this_.ZipCode は Custome17_2_0_、this_.
foreach() 反復でこの関数を実行しているため、クエリは 1 回または 2 回実行されてデータを取得しますが、その後、メモリ不足の例外がスローされます。
経験豊富な NHibernate 開発者として、パフォーマンスを最適化するために LINQ クエリを再構築する必要があることを理解できます。
また、インターネットで検索しましたが、多くの情報を得ることができませんでした。
早めの返信をいただければ幸いです。