【问题标题】:Redshift: Support for concurrent inserts in the same tableRedshift:支持同一张表中的并发插入
【发布时间】:2022-01-17 00:04:51
【问题描述】:

我有一个 lambda 代码,它通过 redshift data api 同时触发一些对同一个表的插入查询。


1. Insert into Table ( select <some analytical logic> from someTable_1)
2. Insert into Table ( select <some analytical logic> from someTable_2)
3. Insert into Table ( select <some analytical logic> from someTable_n)

考虑到这样的查询将被同时触发,Redshift 是否会为每个插入对表应用锁定?或者它是否允许在同一个表中并行插入查询? 我问是因为 postgres 允许并发插入。

https://www.postgresql.org/files/developer/concurrency.pdf

【问题讨论】:

  • 大多数应用程序在 PostgreSQL 中执行并发插入和并发事务,并且没有打嗝。你有什么具体的问题吗?
  • 我想知道redshift中查询对性能的影响。对于 postgres 它工作正常,考虑到 redshift 在下面实现了相同引擎的修改版本,我想知道 redshift 会在并发插入的情况下锁定表,还是会像 postgres 一样灵活。

标签: sql amazon-web-services amazon-redshift


【解决方案1】:

Redshift 和 Postgres DB 都使用 MVCC - https://en.wikipedia.org/wiki/Multiversion_concurrency_control - 所以它们的工作方式可能相同。没有写锁,只有在看到提交时通过提交队列的串行进程。我在 Redshift 中没有发现任何功能问题,所以你应该很好。

功能上这很好,但 Redshift 是列式的,而 Postgres 是基于行的。这导致更新方面的差异。由于这些 INSERT 可能只添加少量(对于 Redshift)行,并且 Redshift 上的最小写入大小为每列每片 1MB,因此这些块中可能有很多未使用的空间。如果经常这样做,桌子上会浪费很多空间,并且需要大量吸尘。如果可以的话,你会想看看这个写入模式,看看是否可以完成更多的插入数据批处理。

【讨论】:

  • 但是从docs来看,我认为它表明它获得了锁。
  • 对不起,您是正确的 - INSERT 会序列化。我在考虑不(总是)的更新。避免许多快速插入的一般建议是有效的。列式数据库专为少量、大量写入而设计。
【解决方案2】:

根据 cmets 中的讨论,可以得出结论,Redshift 中对同一张表的并发插入本质上是阻塞的,而不是 postgres。 请参阅文档:- https://docs.aws.amazon.com/redshift/latest/dg/r_Serializable_isolation_example.html

编辑:-

仅供参考,如果您正在考虑在上述文档中查找的确切信息是什么,我直接将其粘贴在下面:-

Concurrent COPY operations into the same table
Transaction 1 copies rows into the LISTING table:

begin;
copy listing from ...;
end;
Transaction 2 starts concurrently in a separate session and attempts to copy more rows into the LISTING table. Transaction 2 must wait until transaction 1 releases the write lock on the LISTING table, then it can proceed.

begin;
[waits]
copy listing from ;
end;
The same behavior would occur if one or both transactions contained an INSERT command instead of a COPY command.

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。您可以在帮助中心找到更多关于如何写出好的答案的信息。此外,最好在答案中添加相关内容,而不仅仅是链接,因为链接可以及时更改或编辑。直接在答案本身中提供代码可以防止将来成为问题。
猜你喜欢
  • 2023-04-09
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多