I would like to insert records into table_a
from table_b
that don't already exist in table table_a
. I already have Postgres SQL code to do this, but now my team has requested that I use an ORM (SQLAlchemy) instead.
INSERT INTO table_a
SELECT
composite_pk1,
composite_pk2,
col_c,
col_d
FROM table_b
ON CONFLICT (
composite_pk1,
composite_pk2
) DO NOTHING
I have nearly a million rows and about 15 columns (not shown in the example). I need this query to be fast, which is why I don't think the solution posted here will work for my use case.
For performance reasons I also want to avoid treating my Python function as a data conduit. I don't want to transfer many rows of table_b
over the network to my function just to push them back over the network again to table_a
. That is, I would prefer the insert to happen entirely on Postgres, which I already accomplish with my original SQL query.