Raven DB文档数据库 - 抛出“内存不足”异常

4

我有这样的代码:

public bool Set(IEnumerable<WhiteForest.Common.Entities.Projections.RequestProjection> requests)
    {
        var documentSession = _documentStore.OpenSession();
        //{
        try
        {
            foreach (var request in requests)
            {
                documentSession.Store(request);
            }
            //requests.AsParallel().ForAll(x => documentSession.Store(x));
            documentSession.SaveChanges();
            documentSession.Dispose();
            return true;
        }
        catch (Exception e)
        {
            _log.LogDebug("Exception in RavenRequstRepository - Set. Exception is [{0}]", e.ToString());
            return false;
        }
        //}
    }

这段代码被调用了很多次。当通过它的文件数达到大约50,000个时,我会收到OutOfMemoryException异常。 有什么想法吗?也许过了一段时间我需要声明一个新的DocumentStore?

谢谢

**

  • 更新:

**

最终我使用了批处理API来执行所需的更新操作。 您可以在此处查看讨论:https://groups.google.com/d/topic/ravendb/3wRT9c8Y-YE/discussion

基本上,由于我只需要更新对象的1个属性,并考虑到ayendes对重新将所有对象序列化为JSON的评论,我做了这样的事情:

internal void Patch()
    {
        List<string> docIds = new List<string>() { "596548a7-61ef-4465-95bc-b651079f4888", "cbbca8d5-be45-4e0d-91cf-f4129e13e65e" };
        using (var session = _documentStore.OpenSession())
        {
            session.Advanced.DatabaseCommands.Batch(GenerateCommands(docIds));
        }
    }

    private List<ICommandData> GenerateCommands(List<string> docIds )
    {
        List<ICommandData> retList = new List<ICommandData>();

        foreach (var item in docIds)
        {
            retList.Add(new PatchCommandData()
            {
                Key = item,
                Patches = new[] { new  Raven.Abstractions.Data.PatchRequest () {
                Name = "Processed",
                Type = Raven.Abstractions.Data.PatchCommandType.Set,
                Value = new RavenJValue(true)
            }}});
        }

        return retList;
    }

希望这能有所帮助...

非常感谢。

3个回答

4
我刚为我的当前项目做了这个。我把数据分成块并将每个块保存在一个新会话中。这也可能适用于您。
请注意,此示例显示每次分块1024个文档,但需要至少2000个文档才能决定是否值得进行分块。到目前为止,我的插入操作在块大小为4096时获得了最佳性能。我认为这是因为我的文档相对较小。
internal static void WriteObjectList<T>(List<T> objectList)
{
    int numberOfObjectsThatWarrantChunking = 2000;  // Don't bother chunking unless we have at least this many objects.

    if (objectList.Count < numberOfObjectsThatWarrantChunking)
    {
        // Just write them all at once.
        using (IDocumentSession ravenSession = GetRavenSession())
        {
            objectList.ForEach(x => ravenSession.Store(x));
            ravenSession.SaveChanges();
        }

        return;
    }

    int numberOfDocumentsPerSession = 1024;  // Chunk size

    List<List<T>> objectListInChunks = new List<List<T>>();

    for (int i = 0; i < objectList.Count; i += numberOfDocumentsPerSession)
    {
        objectListInChunks.Add(objectList.Skip(i).Take(numberOfDocumentsPerSession).ToList());
    }

    Parallel.ForEach(objectListInChunks, listOfObjects =>
    {
        using (IDocumentSession ravenSession = GetRavenSession())
        {
            listOfObjects.ForEach(x => ravenSession.Store(x));
            ravenSession.SaveChanges();
        }
    });
}

private static IDocumentSession GetRavenSession()
{
    return _ravenDatabase.OpenSession();
}

谢谢!这可能是正确的方式。 - JanivZ

2

你是想在一次调用中保存所有内容吗? DocumentSession需要将您传递给它的所有对象转换为单个请求发送到服务器。这意味着它可能会为向服务器写入内容分配大量内存。 通常我们建议每批保存约1,024个项目。


嗨,Ayende,谢谢!我能问一下吗——当您说1024个项目时,它们的平均大小是多少?这样我就可以估算出我的对象中可以处理多少个了。再次感谢。 - JanivZ

0

DocumentStore 是一个可处理的类,所以我通过在每个块之后处理实例来解决了这个问题。我非常怀疑这是运行操作的最有效方式,但它将防止发生显著的内存开销。

我正在运行一种“全部删除”的操作,如下所示。您可以看到using块在每个块之后处理DocumentStoreIDocumentSession对象。

static DocumentStore GetDataStore()
{
    DocumentStore ds = new DocumentStore
    {
        DefaultDatabase = "test",
        Url = "http://localhost:8080"
    };

    ds.Initialize();
    return ds;
}

static IDocumentSession GetDbInstance(DocumentStore ds)
{
    return ds.OpenSession();
}

static void Main(string[] args)
{
    do
    {
        using (var ds = GetDataStore())
        using (var db = GetDbInstance(ds))
        {
            //The `Take` operation will cap out at 1,024 by default, per Raven documentation
            var list = db.Query<MyClass>().Skip(deleteSum).Take(5000).ToList(); 
            deleteCount = list.Count;
            deleteSum += deleteCount;

            foreach (var item in list)
            {
                db.Delete(item);
            }
            db.SaveChanges();
            list.Clear();
        }
    } while (deleteCount > 0);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接