【问题标题】:Is order preserved in the transaction sequence API of pg-promise?pg-promise的事务序列API中是否保留了顺序?
【发布时间】:2019-08-04 23:53:54
【问题描述】:

我必须插入很多记录,有些列有来自其他表的外键。在纯 SQL 中执行此操作的方法类似于

INSERT (foo, fk) VALUES (1, (SELECT id FROM f_table WHERE blah = 'something'))

但是在大​​量插入时,这太慢了(在我的用例中,5000 行约为 5000 毫秒)。我已经在使用tx.sequence 函数来加快依赖记录的插入,但是为了加快依赖记录的插入,我可以从第一个查询(分批)中返回 id 并保留订单吗?

例如,

const h = pgp.helpers

// insert dependency records
const registersCs = ColumnSet(['name', 'address'], { table: 'register' })
const metrics = await t.sequence(index => {
        const batch = results.slice(index * batchSize, (index * batchSize) + batchSize)
        if (batch && batch.length) {
          try {
            const bulk = `${h.insert(batch, registersCs)} ON CONFLICT (name, address) DO UPDATE ${h.sets(/* yada yada */)} RETURNING id`
            return t.many(bulk)
          } catch (err) {
            log.error(`Error inserting registers: ${err.message}`)
          }
        }
      }, {dest: (index, data) => { results[index].registerId = data.id }})

// insert dependent records, which now has the registerId I was originally going to have to use a subquery for...

【问题讨论】:

    标签: query-optimization pg-promise


    【解决方案1】:

    我可以从第一个查询中返回 id(分批)

    您可以将t.many(bulk) 替换为t.map(bulk, undefined, a => a.id)。并且sequence 支持选项track 来累积所有解析的数据。

    订单会保留吗?

    我不明白为什么不。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 2015-09-06
      • 2011-11-05
      相关资源
      最近更新 更多