I have a Postgres query that involves joining a view with a table that isn’t performing as expected.
My query is as follows:
SELECT networks.* FROM "vat"."test_network" net INNER JOIN "vat"."network" networks ON networks."network_id" = net."network_id";
Where test_network is a table with two rows in it:
| network_id | vat_return_key |
| ———- | ————– |
| 255448 | 266 |
| 388499 | 266 |
And networks is a view onto another table called network_nodes. The view DDL is:
CREATE OR REPLACE VIEW "vat"."network" AS SELECT "network_id" FROM "vat"."network_nodes" GROUP BY "network_id";
Where network_nodes is a table with ~9M rows and an index on the network_id column. The network view results in 450k rows.
When I run the explain plan for the initial query it shows me the following:
initial explain plan
Which I think is basically saying that it’s reading the full network_nodes table i.e. the full 9M rows?
When go directly to the underlying table i.e. using network_nodes instead of network it seems to read a lot less rows from the network_nodes table. So when I change the query to:
SELECT networks.* FROM "vat"."test_network" net INNER JOIN "vat".**"network_nodes"** networks ON networks."network_id" = net."network_id";
The explain plan is:
updated explain plan
It seems like in my initial query the group by is happening before the join and hence the full table is read? Has anyone got any pointers for how I can speed up this query perhaps by having the join part executed before the group by that is part of the view code?
Apologies if i’m missing any information below, happy to provide any additional information.
Many thanks!