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

前端之家收集整理的这篇文章主要介绍了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.

猜你在找的Postgre SQL相关文章