Pg-promise 性能提升:冲突

Sta*_*ica 5 javascript performance node.js postgresql-9.5 pg-promise

我正在尝试遵循 pg-promise 库作者在这里推荐的性能模式。

基本上,Vitaly 建议使用插入来这样做:

var users = [['John', 23], ['Mike', 30], ['David', 18]];

// We can use Inserts as an inline function also:

db.none('INSERT INTO Users(name, age) VALUES $1', Inserts('$1, $2', users))
    .then(data=> {
        // OK, all records have been inserted
    })
    .catch(error=> {
        // Error, no records inserted
    });
Run Code Online (Sandbox Code Playgroud)

使用以下辅助函数:

function Inserts(template, data) {
    if (!(this instanceof Inserts)) {
        return new Inserts(template, data);
    }
    this._rawDBType = true;
    this.formatDBType = function () {
        return data.map(d=>'(' + pgp.as.format(template, d) + ')').join(',');
    };
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,当刀片受到限制时,您会怎么做?我的代码:

db.tx(function (t) {
    return t.any("SELECT... ",[params])
        .then(function (data) {

          var requestParameters = [];

          async.each(data,function(entry){
            requestParameters.push(entry.application_id,entry.country_id,collectionId)
          });

          db.none(
            " INSERT INTO application_average_ranking (application_id,country_id,collection_id) VALUES ($1)" +
            " ON CONFLICT ON CONSTRAINT constraint_name" +
            " DO UPDATE SET country_id=$2,collection_id=$3",
            [Inserts('$1, $2, $3',requestParameters),entry.country_id,collectionId])

            .then(data=> {
              console.log('success');
            })
            .catch(error=> {
              console.log('insert error');
            });

        });

});
Run Code Online (Sandbox Code Playgroud)

显然,我无法访问参数,因为我脱离了异步循环。

我也尝试做这样的事情:

        db.none(
        " INSERT INTO application_average_ranking (application_id,country_id,collection_id) VALUES ($1)" +
        " ON CONFLICT ON CONSTRAINT constraint_name" +
        " DO UPDATE SET (application_id,country_id,collection_id) = $1",
        Inserts('$1, $2, $3',requestParameters));
Run Code Online (Sandbox Code Playgroud)

但当然,它不尊重 postgresql 的标准。

有办法实现这一点吗?

谢谢 !

vit*_*y-t 5

我为性能提升文章编写的示例是为了生成简单形式的多重插入,仅此而已,这对于本文来说已经足够了。

您在这里尝试做的事情有点复杂,并且显然function Inserts(template, data)没有那种逻辑。

我无法从头到尾告诉你需要做什么才能改变它以适应你的情况,但它可能会变得非常复杂,甚至可能根本不值得做。

幸运的是,您不必这样做。人们一次又一次地向我询问某些格式化帮助程序,我最近刚刚发布了这些帮助程序命名空间。您需要更新到该库的最新版本(当前为 4.1.9)才能按照您需要的方式使用它。

将您的示例更改为以下内容:

var h = pgp.helpers;
var cs = new h.ColumnSet(['?application_id', 'country_id', 'collection_id'],
    {table: 'application_average_ranking'});

db.tx(t => {
    return t.any("SELECT... ", [params])
        .then(function (data) {

            var insertData = data.map(d => {
                return {
                    application_id: d.application_id,
                    country_id: d.country_id,
                    collection_id: collectionId
                };
            });

            var updateData = {
                country_id: entry.country_id,
                collection_id: collectionId
            };

            var query = h.insert(insertData, cs) +
                " ON CONFLICT ON CONSTRAINT constraint_name DO UPDATE SET " +
                h.sets(updateData, cs);

            db.none(query)
                .then(data => {
                    console.log('success');
                })
                .catch(error => {
                    console.log('insert error');
                });
        });
});
Run Code Online (Sandbox Code Playgroud)

这样就可以了。

另请参见:ColumnSet。