douju1953
2017-12-27 08:19
浏览 310

如何使用Golang在Postgres中批处理SQL语句

What is the fastest way to send batch requests to Postgres database using Golang? Each request contains 500-200000 rows.
Methods I know about are-
1. Using database/sql package's transaction Begin, Prepare, Commit.
2. Sending all data in one statement.
3. Sending a list of statements using sql.Exec() method.
Is there some other way to send batch requests without making a connection at every statement? If not which is the best way of these?

This question is similar to question at- Golang how do I batch sql statements with package database.sql

图片转代码服务由CSDN问答提供 功能建议

使用Golang将批处理请求发送到Postgres数据库的最快方法是什么? 每个请求包含500-200000行。
我知道的方法是-
1.使用数据库/ sql包的事务开始,准备,提交。
2.在一条语句中发送所有数据 。
3.使用sql.Exec()方法发送语句列表。
是否还有其他方法可以发送批处理请求而不在每个语句之间建立连接? 如果不是,哪一种是最好的方法?

此问题与以下问题类似- Golang如何使用包database.sql批处理sql语句

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duangewu5234 2017-12-27 08:34
    已采纳

    There is a bit old depesz blog post on that. His programs are Perl scripts, but if you concentrate on SQL... Anyway - from DB perspective, you can use COPY, or INSERT with many rows in VALUES. It looks that around 20 is good choice, but it is worth to test that in your case. If performance is key factor, I would put around 2000-5000 rows per transaction. Also, from DB perspective transaction, and session are two separate things. So you can open session, and to many transactions in it.

    For PostgreSQL starting new session per operation is really bad idea - DB spawns new process for each session. One of answers for the question you referred contains this. So you open connection, and then transaction, as it should be done.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题