Skip to content
Advertisement

Need help understanding Postgresql query execution

I have a generated query from an ORM that selects data from a nested join on a junction table.
When executing it looks like it performs a full table scan on a table with >55 million records resulting in a slow execution time.
My expectation was that it would perform the filtering first then lookup by index for the join.

Here is the query: (additional columns have been removed for brevity)

Query Plan

All referenced columns appear to have the correct foreign key and indexing setup.

ForeignKey an Indexes ForeignKey an Indexes ForeignKey an Indexes

Could someone explain why a full table scan is being performed and how I can adjust the query to prevent that?

Thanks!

Advertisement

Answer

JSON format is not very readable by humans, you would probably be better off with text format (the default) for your plan.

So e."ClientId" = 386707 yields 40 times fewer rows than it thought it would. Which means it thinks the nested loop would be about 40 times more expensive than it actually is. It is not surprising the planner makes the wrong choice (well, assuming it is the wrong choice, we don’t actually know that).

Why is that stat off by so much? Are your stats way out of date? Do a VACUUM ANALYZE on “EFACSnapshot” and see if things look better after that. If nothing changes, then I’d want to see the data select * from pg_stats where relname='EFACSnapshot' and attname='ClientId', as well as knowing the PostgreSQL version and the setting of default_statistics_target

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement