MagicStack / MagicStack/asyncpg
advice: best bulk upsert method that still allows to track # of affected rows?
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 8.1k
- Fork
- 468
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
I've been relying on the newest implementation of executemany() to perform bulk upserts, but it has the shortcoming that it will not allow to easily determine the number of affected rows by parsing the statusmsg.
The number of effectively upserted rows can easily be less than the number of rows I attempt to upsert, since I qualify my ON CONFLICT clause with a further WHERE clause specifying that the update should only happen if the new and excluded tuples are distinct.
INSERT INTO "table_name" AS __destination_row (
id,
other_column
) VALUES ($1, $2)
ON CONFLICT (id)
DO UPDATE SET
id = excluded.id,
other_column = excluded.other_column
WHERE
(__destination_row.id IS DISTINCT FROM excluded.id)
OR
(__destination_row.other_column IS DISTINCT FROM excluded.other_column)
;
(regular Postgres would allow for a much terser syntax, but this is the only syntax that is accepted by CockroachDB)
Suppose that at times knowing the exact number of effectively upserted rows is more crucial than the bulk performance, and yet I would prefer not to go to the extreme of upserting one row at a time, what would be the best compromise?
Should I rely on a temporary table and then upserting into the physical tables from that temporary table?
INSERT INTO "table_name" AS __destination_row (
id,
other_column
) SELECT (
id,
other_column
) FROM "__temp_table_name"
ON CONFLICT (id)
DO UPDATE SET
id = excluded.id,
other_column = excluded.other_column
WHERE
(__destination_row.id IS DISTINCT FROM excluded.id)
OR
(__destination_row.other_column IS DISTINCT FROM excluded.other_column)
;
Should I instead use a transaction with several individual upserts of values once again provided by the client?
Are there other approaches I should explore?
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách xem xét entry point executemany() của asyncpg và hành vi của statusmsg được mô tả trong issue. So sánh bulk upsert thông qua một bảng tạm với các upsert riêng lẻ trong giao dịch, sau đó ghi lại phương án dung hòa được hỗ trợ để lấy chính xác số hàng bị ảnh hưởng mà không yêu cầu các thao tác trên từng hàng một.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- postgresql, python
- Lĩnh vực
- databases
- Loại issue
- Tài liệu
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100