- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试遵循 pg-promise 库作者推荐的性能模式 here .
基本上 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
});
使用以下辅助函数:
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(',');
};
}
我的问题是,如果插入有约束,你会怎么做?我的代码:
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');
});
});
});
显然,我无法访问参数,因为我不在异步循环中。
我也试过做这样的事情:
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));
当然,它不遵守 postgresql 的标准。
有办法实现吗?
谢谢!
最佳答案
我为 Performance Boost 写了那个例子article 生成简单形式的多插入,仅此而已,这对文章来说已经足够了。
您在这里尝试做的事情有点复杂,显然 function Inserts(template, data)
没有那种逻辑。
我无法从头脑中告诉你需要做什么才能改变它以适应你的场景,但它可能会变得非常复杂,甚至可能根本不值得做。
幸运的是,您不必这样做。我一次又一次地被问及我最近发布的某些格式化助手 - helpers namespace .您需要更新到最新版本的库(当前为 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');
});
});
});
这样就可以了。
另请参阅:ColumnSet .
关于javascript - Pg-promise 性能提升 : ON CONFLICT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37208693/
如何从 promise 中退出 promise ? perl6 文档没有提供简单的方法。例如: my $x = start { loop { # loop forever until "qui
我的用户 Controller 中有一个索引操作,其中我试图连续做两件事,并且在它们都有机会完成之前不执行所需的 res.json() 方法。 我有一个加入用户的友谊加入模型。一列是 friender
请帮我解释一下为什么日志结果有两种不同: 方式 1:每 1 秒顺序记录一次 方式 2:1 秒后记录所有元素。 // Way 1 let sequence = Promise.resolve(); [1
我的问题很简单。 Promise.all() 方法可以返回 Promise 吗?让我解释一下: function simpleFunction() { let queue = [];
我正在使用 Promise 从存储中读取文件并转换为 base64 字符串。我有图像数组,使用 RNFS 读取图像 const promise_Images = _Images.map(async (
如果使用非空数组调用 Promise.all 或 Promise.race,它们将返回一个待处理的 Promise: console.log(Promise.all([1])); // prints
Promise.all 是否可以在没有包装 promise 的情况下返回链的最后一个值? 如果不使用 await,它在我的上下文中不起作用 没有包装的例子: function sum1(x){ r
我一直在玩 promise,通常能想出如何处理好它们,但在这种情况下,我不知道如何删除一个 promise-wrapping level。 代码如下: let promise2 = promise1.
考虑以下嵌套的Promises结构: const getData = async() => { const refs = [{ name: "John33", age: 3
我已经阅读了 Promise/A+ 规范,但据我了解,还有诸如 Promise/A 和 Promise 之类的东西。它们之间有什么区别? Promise 和 Promise/A 规范也是如此吗?如果是
当我运行以下代码时: my $timer = Promise.in(2); my $after = $timer.then({ say "2 seconds are over!"; 'result'
以下简单的 promise 是发誓的,我不允许打破它。 my $my_promise = start { loop {} # or sleep x; 'promise re
我正在尝试扩展Promise: class PersistedPromise extends Promise { } 然后在派生类上调用静态resolve以直接创建一个已解决的Promise: Per
我有两个返回 promise 的函数,我独立使用它们作为: getLocal().then(...) 和 getWeb().then(...) 但是现在我遇到了一个奇怪的问题: 1) 我需要第三个
我不知道 promise.all 解决方案中的 promise.all 是否是一个好的实践。我不确定。 我需要从一组用户获取信息,然后通过此信息响应,我需要发送消息通知。 let userList =
我一直在尝试使用 queueMicrotask() 函数,但我没有弄清楚当回调是微任务时回调的优先级如何。查看以下代码: function tasksAndMicroTasks() { const
我一直在尝试使用 queueMicrotask() 函数,但我没有弄清楚当回调是微任务时回调的优先级如何。查看以下代码: function tasksAndMicroTasks() { const
今年早些时候,我在 Pharo Smalltalk 参与了一个 promise 项目。这个想法是为了实现以下行为: ([ 30 seconds wait. 4 ]promiseValue )then:
大家好,提前感谢您的帮助。 下面是我正在尝试做的事情 function1(){ throw some error(); } function2() { // dosomething suc
我有以下未解析的代码。f2 解决了,所以我不会添加该代码,它是 f1 我有问题。 我调用函数,它到达最里面如果,它调用函数“find”,它执行函数 findId,完美返回 Id,然后执行 editId
我是一名优秀的程序员,十分优秀!