加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – 如何使用node-postgres进行批量插入

发布时间:2020-12-13 15:53:25 所属栏目:百科 来源:网络整理
导读:我正在使用express和node-pg将excel文件导入postgres数据库 目前我循环遍历excel行并为每一行执行插入,但我觉得这不是正确的方法: workbook.xlsx.readFile(excel_file).then(function () { // get the first worksheet var worksheet = workbook.getWorkshe
我正在使用express和node-pg将excel文件导入postgres数据库

目前我循环遍历excel行并为每一行执行插入,但我觉得这不是正确的方法:

workbook.xlsx.readFile(excel_file).then(function () {
        // get the first worksheet          
        var worksheet = workbook.getWorksheet(1);
        // Loop through all rows
        worksheet.eachRow(function (row,rowNumber) {
            // Commit to DB only from line 2 and up. We want to exclude headers from excel file
            if (rowNumber > 1) {
                // Loop through all values and build array to pass to DB function
                row.eachCell(function (cell,colNumber) {
                    arrSQLParams.push(cell.value)                   
                })

                // Add the user id from session to the array
                arrSQLParams.push(user);

                // Insert into DB
                db.query(strSQL,arrSQLParams,function (err,result) {
                    if (err) {
                        console.log(err);
                            ret = false;
                        }
                })

                // Empty the array for new query
                arrSQLParams = [];
            }
        })          
    });

有没有更好的方法来提高性能?

解决方法

在作者提供澄清之后,为了一次最多插入1000条记录,Multi-row insert with pg-promise中建议的解决方案正是作者在性能和灵活性方面所需要的.

UPDATE

必读文章:Data Imports.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读