[英]Redshift table update using two tables
I have a requirement to work on an update. 我需要进行更新。 The requirement is to update table 2 using the data from table 1. Please find below sample records from the two tables: 要求是使用表1中的数据更新表2。请在下面的两个表中找到示例记录:
TABLE A
-----------------
colA | colB | colC
-----------------
1 AAA ABC
2 BBB DEF
3 CCC GHI
3 CCC HIJ
TABLE B
-----------------
colA1 | colB1 | colC1
-----------------
1 AAA
2 BBB
3 CCC
3 CCC
I need to update the colC1 with values of ColC. 我需要使用ColC的值更新colC1。 Expected output is shown below 预期输出如下所示
TABLE B
-----------------
colA1 | colB1 | colC1
-----------------
1 AAA ABC
2 BBB DEF
3 CCC GHI
3 CCC HIJ
Do we need to use a cursor for this or a simple update statement like shown below would do? 我们需要为此使用游标还是像下面所示的简单更新语句?
Update table B
set colC1 = table A.colC
from TABLE A, TABLE B
where colA1 = colA
and colB1 = colB;
Your SQL seems perfectly fine. 您的SQL看起来很好。
Cursors are normally used for programmatic access to a database, where the program is stepping through the results one-at-a-time, with the cursor pointing to the 'current record'. 游标通常用于对数据库的编程访问,程序一次一次浏览结果,游标指向“当前记录”。 That isn't needed in normal SQL update statements. 在普通的SQL更新语句中不需要。
One thing to note... In Amazon Redshift, using an UDPATE on a row causes the existing row to be marked for deletion and a new row is created. 需要注意的一件事...在Amazon Redshift中,在一行上使用UDPATE会导致将现有行标记为删除并创建新行。 (This is a side-effect of using a columnar database.) If many rows are updated, it means that the disk storage becomes less efficient. (这是使用列式数据库的副作用。)如果更新了许多行,则意味着磁盘存储的效率降低。 It can be improved by occasionally running VACUUM tablename
, which will remove the deleted storage. 可以通过偶尔运行VACUUM tablename
来进行改进,该操作将删除已删除的存储。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.