doucong3048 2018-04-12 13:32
浏览 27

PostgreSQL关闭语句需要很长时间

In order to add millions of records to a Postgres database with constant memory consumption, I am using a thread pool with several workers as well as a gorp.Transaction.

Per million records, the following code is called from different threads about a hundred times, each time handling a batch of 10000 records or so:

func batchCopy(p importParams) error {
  copy := pq.CopyIn("entity", "startdate", "value", "expirydate", "accountid")
  stmt, err := p.txn.Prepare(copy)
  if err != nil {
    return err
  }

  for _, r := range p.records {
    _, err := stmt.Exec(
        r.startDate,
        r.value,
        r.expiryDate,
        p.accountId)

    if err != nil {
      return err
    }
  }

  if err := stmt.Close(); err != nil {
    return err
  }
  return nil
}

For some reason, I've noticed that the process tends to be very slow with the Prepare and Close calls taking very long.

I then tried to reuse the same statement for all of my calls to batchCopy and close it after all of them are done. In this case, the batchCopy finishes very fast, but when I call stmt.Close() - it takes forever.


Questions:

  • What would be the right way to go about statements? Should I create one per batch or reuse them?
  • What is happening in stmt.Close() and why does it take so long after many calls to stmt.Exec?
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向