Redshift query optimization issue

I am trying to improve the performance of one of our pipelines via redefining the distkey and sortkey on the primary source table. I was able to get a factor of 5 speed increase on the select and insert queries but am getting the inverse on our update queries and can’t figure out why.

Previous table definition

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>item_history (
a text,
b text,
c text,
...
from timestamp distkey,
until timestamp
) sortkey(c)
</code>
<code>item_history ( a text, b text, c text, ... from timestamp distkey, until timestamp ) sortkey(c) </code>
item_history (
  a text,
  b text,
  c text,
  ...
  from timestamp distkey,
  until timestamp
) sortkey(c)

Almost every query we run against the table uses both a and b in the where and/or join along with the from date filter, so I updated the table definition to:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>item_history (
a || '-' || b text distkey,
a text,
b text,
c text,
...
from timestamp,
until timestamp
) sortkey(from)
</code>
<code>item_history ( a || '-' || b text distkey, a text, b text, c text, ... from timestamp, until timestamp ) sortkey(from) </code>
item_history (
  a || '-' || b text distkey,
  a text,
  b text,
  c text,
  ...
  from timestamp,
  until timestamp
) sortkey(from)

The pipeline:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>-- select relevant records from kafka stream mv
CREATE TABLE temp_streamed_records
diststyle key
distkey (ab)
sortkey (kafka_timestamp)
AS
(
with (set of CTE to target and transform data from the kafka_stream_mv)
select
a || '-' || b as ab
a,
b,
...
kafka_timestamp
from CTE
)
-- update records marked as expired or removed from the streamed data
UPDATE item_history
set until = kafka_timestamp
FROM temp_streamed_records
WHERE
item_history.ab = temp_streamed_records.ab
and
(e <> e or f <> f)
and until is null
-- insert new records
INSERT INTO item_history
select
...
FROM temp_streamed_records
LEFT JOIN item_history
ON
item_history.ab = temp_streamed_records.ab
and
e = e and f = f
and until is null
where not found
</code>
<code>-- select relevant records from kafka stream mv CREATE TABLE temp_streamed_records diststyle key distkey (ab) sortkey (kafka_timestamp) AS ( with (set of CTE to target and transform data from the kafka_stream_mv) select a || '-' || b as ab a, b, ... kafka_timestamp from CTE ) -- update records marked as expired or removed from the streamed data UPDATE item_history set until = kafka_timestamp FROM temp_streamed_records WHERE item_history.ab = temp_streamed_records.ab and (e <> e or f <> f) and until is null -- insert new records INSERT INTO item_history select ... FROM temp_streamed_records LEFT JOIN item_history ON item_history.ab = temp_streamed_records.ab and e = e and f = f and until is null where not found </code>
-- select relevant records from kafka stream mv
CREATE TABLE temp_streamed_records 
  diststyle key
  distkey (ab)
  sortkey (kafka_timestamp)
  AS
(
  with (set of CTE to target and transform data from the kafka_stream_mv)
  select
    a || '-' || b as ab
    a,
    b,
    ...
    kafka_timestamp
  from CTE
)

-- update records marked as expired or removed from the streamed data
UPDATE item_history
  set until = kafka_timestamp
FROM temp_streamed_records
WHERE
  item_history.ab = temp_streamed_records.ab
  and
  (e <> e or f <> f)
  and until is null

-- insert new records
INSERT INTO item_history
select 
  ...
FROM temp_streamed_records
LEFT JOIN item_history
  ON
    item_history.ab = temp_streamed_records.ab
    and
    e = e and f = f
    and until is null
where not found

The schema update resolved a ‘distributed a large number of rows…’ error on the select/insert queries. That warning does not appear for the updates (neither before nor after)

Looking at the update query details in the aws console from before and after the only possibly meaningful difference I see is that we went from ~60% disk usage to ~75% usage (duplicating the source tables). The hash condition and join filters are essentially unchanged

before: hash – a = a and b = b, join – (e <> e or f <> f)
after: hash – ab = ab, join – (e <> e or f <> f)

Why does this schema change improve the insert/select 5x better but make the update 5x worse?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật