When running the following query, it sometimes takes 15 seconds and sometimes 90mins. What causes this big difference?
INSERT INTO missing_products
SELECT table_name,
product_id
FROM products
WHERE table_name = 'xxxxxxxxx'
AND product_id NOT IN (SELECT id
FROM new_products);
I have tried an explain on it and the only thing I can see is an index only scan on new products. I did also rewrite out this query to have a left join instead and insert the rows where the right side is NULL but this causes the same problem with the time.
I have the following tables with a structure something like what follows.
products
id bigint not null,
product_id text not null,
table_name text not null,
primary key (id),
unique index (product_id)
new_products
id text not null,
title text not null,
primary key, btree (id)
missing_products
table_name text not null,
product_id text not null,
primary key (table_name, product_id)
Explain - This has an extra field in the where clause but should give a good idea. Time it took 22 seconds.
Insert on missing_products (cost=5184.80..82764.35 rows=207206 width=38) (actual time=22466.525..22466.525 rows=0 loops=1)
-> Seq Scan on products (cost=5184.80..82764.35 rows=207206 width=38) (actual time=0.055..836.217 rows=411150 loops=1)
Filter: ((active > (-30)) AND (NOT (hashed SubPlan 1)) AND (feed = 'xxxxxxxx'::text))
Rows Removed by Filter: 77436
SubPlan 1
-> Index Only Scan using new_products_pkey on new_products (cost=0.39..5184.74 rows=23 width=10) (actual time=0.027..0.027 rows=0 loops=1)
Heap Fetches: 0
Planning time: 0.220 ms
Execution time: 22466.596 ms
EXPLAIN ANALYZEoutput, that would help us know if there are any triggers that could be causing the slowness