So i just started working in PostgreSQL after some experience with Oracle and I have this query, that in Oracle returns in 200ms and in Postgres returns in 1.40 mins. The culprit seems to be
AND product_cost_view.product_type_id = product.product_type_id
When i remove this portion or hardcode product_cost_view.product_type_id with some ID, it runs fast. Explain plan didn't seem give and insight, it just says INDEX SCAN ON TABLE product TOTAL COST 776403 1913 ROWS.
Yes, product_cost_view is a view, I've also remarked that if i replace that view with a table that also has product_type_id then it also works fast. I tried using CTE and subselects in 100 different forms but when i use that product.product_type_id in the where clause with that view it just works hellish slow and i can't see what I miss. Thanks in advance :)
P.S. Yes, i have the exact same data and indexes in both databases
SELECT COUNT(*)
FROM product
WHERE user_id = 1000000
AND (product_id IN (SELECT DISTINCT product_id
FROM product_cost_view
WHERE user_id = 1000000
AND cost_type = 'X'
AND product_cost_view.product_type_id = product.product_type_id)
);
distinctin the subquery is completely useless. Oracle might optimize it away, but I don't think Postgres willexplain (analyze, buffers, format text)(not just a "simple" explain) as formatted text and make sure you preserve the indention of the plan. Paste the text, then put```on the line before the plan and on a line after the plan. Please also include completecreate indexstatements for all indexes as well.