簡體   English   中英

NodeJs MYSQL序列批量插入最佳實踐

[英]NodeJs MYSQL Sequence bulk insert best practice

我有一個要使用nodejs插入MySQL數據庫的數據列表。 我將代碼編寫為:

con.connect(function (err) {
     myArray.forEach((el)=>{
        con.query(1stQuery,1stValue,(error,result)=>{
        //dosomething with result
           con.query("SELECT LAST_INSERT_ID();",(error2,result2)=>{
                myArray.child.foreach((el2)=>{
                    //insert child into an array after processing 
                    //keep counter of for each, when every child is looped do insert

                       con.query(2ndQuery,ArrayOfValue,(error3,result3){
                        //when everything is finished then send some data back to user
                       })

                })

             })

        })
    })
}

一切正常,但速度太慢。 我目前正在使用xammp在本地服務器上進行測試,並插入了約400項父數據條目和800項其子項條目,耗時約60秒。 有什么辦法可以改善運行時性能?

  1. 您應該准備查詢,並為每個插入重復使用它。 參見https://www.npmjs.com/package/mysql#preparing-queries
  2. 您應該對批量插入使用事務。 由於MySQL不必在每次插入后都提交,因此可以大大提高性能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM