微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐