I use the id directly in the query, it has a low cost:
-- Query 1.
-- Nested Loop Semi Join (cost=2.27..280.78 rows=1 width=129)
EXPLAIN SELECT *
FROM view_companies_extra
WHERE company_id iN (
SELECT id
FROM companies
WHERE id = 'cddba3ad-dd13-48bb-a5b0-f0e325f27d51'
);
And if I use the id from query, I get a much higher cost?
-- Query 2.
-- Seq Scan on companies (cost=0.00..18581.05 rows=1 width=16)
-- Filter: ((inn)::text = '7731394650'::text)
EXPLAIN SELECT id
FROM companies
WHERE number = '7731394650';
-- Query 3.
-- Nested Loop (cost=3.10..10324011.11 rows=12 width=129)
EXPLAIN SELECT *
FROM view_companies_extra
WHERE company_id iN (
SELECT id
FROM companies
WHERE inn = '7731394650'
);
If you run “query 2” to get the id, and then use it in query “query 1”, it should be very fast.
But when I combine all this in one “query 3”, it takes an extremely long time. Why is that?
Postgres 13.10 and 13.14.