简体   繁体   English

更新速度非常慢

[英]Very slow update performance

I am parsing a CSV file, for each row I want to check if corresponding entry exists in the database, and if it does I want to update it, if it doesn't I want to enter a new entry. 我正在解析一个CSV文件,对于每一行,我想检查数据库中是否存在相应的条目,如果要更新,如果不想要,请输入一个新条目。

It is very slow - only around 30 entries per second. 这非常慢-每秒大约只有30个条目。

Am I doing something incorrectly? 我做错了什么吗?

Using node, mongodb, monk 使用节点,mongodb,和尚

 function loadShopsCSV(ShopsName) {
    var filename = 'test.csv'

        csv
            .fromPath(filename)
            .on("data", function(data) {               
                           var entry = {
                                PeriodEST: Date.parse(data[0]),
                                TextDate: textDateM,
                                ShopId: parseInt(data[1]),
                                ShopName: data[2],
                                State: data[3],
                                AreaUS: parseInt(data[4]),
                                AreaUSX: AreaUSArray[stateArray.indexOf(data[3])],
                                ProductClass: data[5],
                                Type: data[6],
                                SumNetVolume: parseInt(data[7]),                                
                                Weekday: weekdayNum,                              
                                WeightedAvgPrice: parseFloat(data[8]),

                            }


                            db.get(ShopsDBname).update(
                                {"PeriodEST" : entry.PeriodEST,
                                 "ShopName": entry.ShopName,
                                 "State" : entry.State,
                                 "AreaUS" : entry.AreaUS,
                                 "ProductClass" : entry.ProductClass,
                                 "Type" : entry.Type},
                                  {$set : entry},
                                  function(err, result) {

                                  }
                            );
                    }
                }
            })
            .on("end", function() {
                console.log('finished loading: '+ShopsName)
            });
    }, function(err) {
        console.error(err);
    });
}

First I would suggest to localize problem: 首先,我建议定位问题:

  • replace .on("data", function(data) with dummy .on("data", function() {return;}) and confirm speed of csv parsing. 用虚拟.on("data", function() {return;})替换.on("data", function(data) .on("data", function() {return;})并确认csv解析的速度。
  • turn on mongo profiler db.setProfilingLevel(1) and check slow log if there is any query slower than 100 ms. 打开mongo profiler db.setProfilingLevel(1)并检查慢速日志,是否有任何查询慢于100毫秒。

If there are no problems above - the bottleneck is in one of nodejs libraries you are using to prepare and send query. 如果上面没有问题-瓶颈在您用于准备和发送查询的nodejs库之一中。

Assuming the problem is with slow mongodb queries, you can use explain for the update query for details. 假设问题出在缓慢的mongodb查询中,那么您可以将explain用于更新查询以获取详细信息。 It may be the case it does not use any indexes and run a table scan for every update. 可能是它不使用任何索引并为每个更新运行表扫描的情况。

Finally, it is recommended to use bulk operations, which was designed for exactly your usecase. 最后,建议使用专门为您的用例设计的批量操作。

Have you tried updating with no write concern? 您是否尝试过无写更新? as MongoDB blocks until whole update is successful and DB sends back that acknowledgement? MongoDB阻塞直到整个更新成功并且DB发送回该确认? Are you on cluster or something? 您在集群还是其他东西上? (might want to write into primary node if so) (如果可能,可能要写入主节点)

after your {$set : entry}, {writeConcern: {w: 0}} 在您的{$ set:entry}之后,{writeConcern:{w:0}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM