有没有办法计算在amazon redshift中通过SQL更新查询影响的最后行数?
类似于PG_LAST_COPY_COUNT()这样的函数来计算亚马逊红移中的最后一次复制计数。
感谢任何帮助。
答案 0 :(得分:1)
你是对的,没有等效的PG_LAST_COPY_COUNT()
用于从插入/更新/删除中捕获最后一个受影响的行数,但是你可以通过使用系统表STV_SESSIONS
来实现这一点,{{ 1}}和STL_QUERY
。关于如何构建查询,有一篇很棒的博客文章here。
希望这有帮助!
答案 1 :(得分:1)
这应该提供信息
SELECT CASE
WHEN d.relname IS NOT NULL THEN d.relname
ELSE i.relname
END TableName,
CASE
WHEN nvl (Deleted_Rows,0) = 0 THEN nvl (Inserted_Rows,0)
ELSE 0
END Inserted_Rows,
CASE
WHEN nvl (Deleted_Rows,0) = nvl (Inserted_Rows,0) THEN nvl (Inserted_Rows,0)
ELSE 0
END Updated_Rows,
CASE
WHEN nvl (Inserted_Rows,0) = 0 THEN nvl (Deleted_Rows,0)
ELSE 0
END Deleted_Rows
FROM (SELECT query,
relname,
SUM(ROWS) Deleted_Rows
FROM stl_delete sd
JOIN pg_class t ON t.oid = sd.tbl
GROUP BY query,
relname) d
FULL JOIN (SELECT query,
relname,
SUM(ROWS) Inserted_Rows
FROM stl_insert si
JOIN pg_class t ON t.oid = si.tbl
GROUP BY query,
relname) i ON i.query = d.query
WHERE CASE WHEN d.query IS NOT NULL THEN d.query ELSE i.query END = pg_last_query_id();