【问题标题】:MongoDB (C#) Insert operation taking more time compared to SQL Server Insert operation(1000 times more)与 SQL Server 插入操作相比,MongoDB (C#) 插入操作花费更多时间(1000 倍)
【发布时间】:2013-09-30 11:12:58
【问题描述】:

我正在从如图所示的文件结构插入数据。

文件夹-->子文件夹-->子文件夹-->文件

以前,此数据存储在 SQL Server 中。 将所有这些数据插入 SQL 所需的时间为 26 秒,而将相同的数据插入 MongoDB 则需要 2500 秒(无法承受)。

该文件是一个 xml 文件。文件夹/子文件夹详细信息存储在一个表中,文件详细信息存储在另一个表中,文件中的一些标签存储在另一个表中。 我的代码递归地检查和存储数据。

插入操作同时插入到 4 个集合中(类似于 SQL 插入期间的 4 个表)。

我不想使用多个服务器,因为我正在使用一台服务器来处理 SQL。除了 MongoDbs _id 字段索引(这是隐式的)之外,我也没有显式地进行任何索引

请找到以下代码-

在 SQL 中:

public int AddRowToFolder(string fRelativePosition,
        string flabel, string fPhysicalName, int fParentId, int fViewOrder, EntityState fViewState,
        EntityProtection fProtection)
    {
       //Increment the Folder row count by 1.
        folderRowCount++;
        try
        {
            folderDataRow = folderDataSet.Tables[0].NewRow();

            //Adding the values to the Folder Dataset.

            folderDataRow[Convert.ToInt32(FolderColumns.RelativePosition)] = fRelativePosition;
            folderDataRow[Convert.ToInt32(FolderColumns.Label)] = flabel;
            folderDataRow[Convert.ToInt32(FolderColumns.PhysicalName)] = fPhysicalName;
            folderDataRow[Convert.ToInt32(FolderColumns.ParentId)] = fParentId;
            folderDataRow[Convert.ToInt32(FolderColumns.ViewOrder)] = fViewOrder;
            folderDataRow[Convert.ToInt32(FolderColumns.ViewState)] = fViewState;
            folderDataRow[Convert.ToInt32(FolderColumns.Protection)] = fProtection;

            folderDataSet.Tables[0].Rows.Add(folderDataRow);                
        }

        return folderRowCount;
    }

在 MongoDB 中:

public int AddRowToFolder(string fRelativePosition,
        string flabel, string fPhysicalName, int fParentId, int fViewOrder, EntityState fViewState,
        EntityProtection fProtection)
    {
       //Increment the Folder row count by 1.
        folderRowCount++;
        try
        {
            BsonDocument doc = new BsonDocument();

            //Adding the values to the Folder Dataset.

            doc.Add(FolderColumns.RelativePosition.ToString() , fRelativePosition);
            doc.Add((FolderColumns.Label).ToString(), flabel);
            doc.Add((FolderColumns.PhysicalName).ToString(), fPhysicalName);
            doc.Add((FolderColumns.ParentId).ToString(), fParentId);
            doc.Add((FolderColumns.ViewOrder).ToString(),fViewOrder);
            doc.Add((FolderColumns.ViewState).ToString(),fViewState);
            doc.Add((FolderColumns.Protection).ToString(), fProtection);

            foldersCollection.Insert(doc, WriteConcern.Unacknowledged);
        }

        return folderRowCount;
    }

我使用的连接字符串是:

DbConnString = "mongodb://localhost"

与 SQL 相比,MongoDB 的写入操作预计会更快,但我没有看到。有人可以帮忙吗?

【问题讨论】:

  • 请用换行符等重新格式化您的问题...这很难读。此外,您所指的图像也不可见。
  • 与 SQL 相比,MongoDB 的写入操作预计会更快您可能将 MongoDB 置于“不丢失数据模式”。将其切换到“丢失数据模式”(未确认的写入)并观察性能上升。
  • 没有更多细节,我们能提供什么帮助?插入了多少文件?您的架构可能没有优化。
  • @Mixxiphoid : 用换行符格式化。但我不允许插入图像,除非我的声望为 10。
  • NoSQL 数据库具有不同的设计和性能特征。我并不惊讶他们会表现不同。另外,您为什么认为 MongoDb 会更快?

标签: c# mongodb


【解决方案1】:

我发现我的错误非常愚蠢。我正在调试模式下运行我的应用程序。当我执行 exe 时速度更快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    相关资源
    最近更新 更多