0

1000000のドキュメントをRavenDBに挿入したい。

class Program
{
        private static string serverName;
        private static string databaseName;

        private static DocumentStore documentstore;
        private static IDocumentSession _session;

        static void Main(string[] args)
        {

            Console.WriteLine("Start...");

            serverName = ConfigurationManager.AppSettings["ServerName"];
            databaseName = ConfigurationManager.AppSettings["Database"];

            documentstore = new DocumentStore { Url = serverName };
            documentstore.Initialize();

            Console.WriteLine("Initial Databse...");

            _session = documentstore.OpenSession(databaseName);

            for (int i = 0; i < 1000000; i++)
            {
                var person = new Person()    
                {
                    Fname = "Meysam" + i,
                    Lname = " Savameri" + i,
                    Bdate = DateTime.Now,
                    Salary = 6001 + i,
                    Address = "BITS provides one foreground and three background priority levels that" +
                              "you can use to prioritize transBfer jobs. Higher priority jobs preempt"+
                              "lower priority jobs. Jobs at the same priority level share transfer time,"+
                              "which prevents a large job from blocking small jobs in the transfer"+
                              "queue. Lower priority jobs do not receive transfer time until all the "+
                              "higher priority jobs are complete or in an error state. Background"+
                              "transfers are optimal because BITS uses idle network bandwidth to"+
                              "transfer the files. BITS increases or decreases the rate at which files "+
                              "are transferred based on the amount of idle network bandwidth that is"+
                              "available. If a network application begins to consume more bandwidth,"+
                              "BITS decreases its transfer rate to preserve the user's interactive"+
                              "experience. BITS supports multiple foreground jobs and one background"+
                              "transfer job at the same time.",
                    Email = "Meysam" + i + "@hotmail.com",
                };

                _session.Store(person);

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Count:" + i);
                Console.ForegroundColor = ConsoleColor.White;
            }

            Console.WriteLine("Commit...");

            _session.SaveChanges();
            documentstore.Dispose();

            _session.Dispose();

            Console.WriteLine("Complete...");
            Console.ReadLine();
        }
    }

しかし、セッションは変更を保存しません、エラーが発生します:

タイプ'System.OutOfMemoryException'の未処理の例外がmscorlib.dllで発生しました

4

2 に答える 2

8

ドキュメントセッションは、少数のリクエストを処理することを目的としています。代わりに、1024のバッチで挿入してみてください。その後、セッションを破棄して、新しいセッションを作成します。を取得する理由OutOfMemoryExceptionは、ドキュメントセッションがすべての構成オブジェクトをキャッシュして作業単位を提供するためです。そのため、バッチを挿入した後にセッションを破棄する必要があります。

これを行うための適切な方法は、Batchlinq拡張機能を使用することです。

foreach (var batch in Enumerable.Range(1, 1000000)
 .Select(i => new Person { /* set properties */ })
 .Batch(1024))
{
 using (var session = documentstore.OpenSession())
 {
   foreach (var person in batch)
   {
     session.Store(person);
   }
   session.SaveChanges();
 }
}

Enumerable.Rangeとの両方の実装Batchは怠惰であり、すべてのオブジェクトをメモリに保持しません。

于 2012-10-13T17:48:00.487 に答える
1

RavenDBには、追加のLINQ拡張機能を必要とせずに同様のことを行うバルクAPIもあります。

using (var bulkInsert = store.BulkInsert())
{
    for (int i = 0; i < 1000 * 1000; i++)
    {
        bulkInsert.Store(new User
            {
                Name = "Users #" + i
            });
    }
}

Note.SaveChanges()は呼び出されず、バッチサイズに達したとき(BulkInsert()必要に応じて定義されます)、またはが破棄されたときに呼び出されますbulkInsert

于 2015-01-21T22:26:00.750 に答える