【问题标题】:Redshift Concurrent TransactionsRedshift 并发事务
【发布时间】:2016-11-03 17:26:13
【问题描述】:

我在同时写入 Redshift 数据库时遇到问题。使用单个连接写入数据库效果很好,但有点慢,所以我尝试使用多个并发连接,但看起来一次只能有一个事务。

我通过单独运行以下python脚本进行调查,然后同时运行4次。

import psycopg2
import time
import os

if __name__ == "__main__":
  rds_conn = psycopg2.connect(host="www.host.com", port="5439", dbname='db_name', user='db_user', password='db_pwd')
  cur = rds_conn.cursor()
  with open("tmp/test.query", 'r') as file:
    query = file.read().replace('\n', '')

  counter = 0
  start_time = time.time()
  try:
    while True:
      cur.execute(query)
      rds_conn.commit() # first commit location
      print("sent couter: %s" % counter)
      counter += 1
  except KeyboardInterrupt:
    # rds_conn.commit() # secondary commit location
    total_time = time.time() - start_time
    queries_per_sec = counter / total_time
    print("total queries/sec: %s" % queries_per_sec)

正在加载的test.query 文件是一个multi-row insert 文件~16.8mb,看起来有点像:

insert into category_stage values
(default, default, default, default),
(20, default, 'Country', default),
(21, 'Concerts', 'Rock', default);

(只是更长的时间)

脚本结果显示:

---------------------------------------------------
| process count | queries/sec | total queries/sec |
---------------------------------------------------
| 1             | 0.1786      | 0.1786            |
---------------------------------------------------
| 8             | 0.0359      | 0.2872            |
---------------------------------------------------

...这与我正在寻找的增长相去甚远。当您可以看到脚本中的计数器在增加时,会出现一个清晰的循环模式,每个模式都在等待前一个脚本的查询完成。

当提交从第一个提交位置移动到第二个提交位置时(因此仅在脚本被中断时提交),一次只有一个脚本前进。如果那不是某种事务锁的明确指示,我不知道是什么。

据我搜索,没有文件说我们不能有并发事务,那么问题可能是什么?我突然想到查询的大小是如此之大,以至于一次只能执行一个,但我预计 Redshift 每笔交易的数据量将超过 ~17mb。

【问题讨论】:

  • 脚本在哪里运行?你试过增加checkpoint_timeout?
  • 不要在 Redshift 中使用 INSERT。这是 MPP 分析数据库,而不是 OLTP 数据库。如果您想在并行模式下工作,请使用 COPY,然后使用 UPSERT。

标签: postgresql concurrency transactions amazon-redshift psycopg2


【解决方案1】:

根据 Guy 的评论,我最终使用了来自 S3 存储桶的 COPY。这最终加快了一个数量级,只需要一个线程来调用查询,然后允许 AWS 并行处理来自 S3 的文件。我使用了详细的指南 here 并设法在一个多小时内插入了大约 120Gb 的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 2011-10-18
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多