Closed gnudiff closed 4 months ago
I cannot think of any way to improve oracle_fdw so that it handles that more efficiently.
But why do you need to create a CSV file? I would write a PL/pgSQL function that first queries the Oracle table for the list of IDs, and store that in a temporary table or an array. Then the function eliminates the IDs you don't want, and finally it constructs a dynamic SQL statement with the literal numbers in it.
Something like
CREATE FUNCTION xyz() RETURNS SETOF foreign_table_b
LANGUAGE plpgsql AS
$$DECLARE
ids bigint[];
BEGIN
SELECT array_agg(a.id) INTO ids FROM foreign_table_a AS a
WHERE /* condition */
AND NOT EXISTS (SELECT FROM local_table AS l
WHERE l.id = a.id);
RETURN QUERY EXECUTE
format(
'SELECT FROM foreign_table_b WHERE id = ANY (%s)', ids;
END;$$;
Can we close the issue?
Can we close the issue?
Sure. I'll see if I can work with the function, thanks a lot for considering the case and sketching the idea!
(as regards export to csv it is just that the whole queries are part of task flow app and I might need to rerun tasks from previous results).
My typical use of oracle_fdw is to pull the data from large Oracle tables according to criteria that sometimes need to be specified in my PG tables -- I only have read-only access to Oracle DB.
So, a typical go would be to:
Like:
This query takes 10mins due to the join of ORA_T and PG_T. If I run the query directly from oracle (without trying to match existing cargoids, it takes 10secs.
The next part is "ask Oracle for details on pulled IDs", which is essentially:
SELECT [columns] FROM oracle_details ORA_DT WHERE [Oracle expressions] AND ORA_DT.cargoid IN (SELECT cargoid FROM my_pg_table)
This takes in excess of 20mins, whereas it only takes seconds to do if I supply the list of cargoids as a simple expression IN ( 1,2,3,4,5...)
Currently I have workarounds like:
But that is ugly and will cease to work if I get to 1000+ cargoids (limit for IN() clause for Oracle). And it means I actually am not using oracle_fdw for the queries!
The EXPLAIN on actual first query goes like (ead_cargos is the only PG table in EXPLAIN):
Is there anything I can do to speed things up using oracle_fdw?