patternsqlModerate
Is it necessary to ANALYZE a table after an index has been created?
Viewed 0 times
afternecessaryanalyzecreatedbeenhasindextable
Problem
In my transaction, I am creating a temporary table:
I also add an index on that table:
Is it now necessary to run
In other words, my question is: does the creation of an index already imply an
create temporary table x on commit drop as
select ...I also add an index on that table:
create index on x(some_column);Is it now necessary to run
analyze on that table? Or do I only need to analyze the table for updates/deletes after index creation?In other words, my question is: does the creation of an index already imply an
analyze execution?Solution
If the index is just on simple columns like in your case, it is not necessary to
That is because statistics on the value distribution of the table columns are always collected, no matter if there is an index on the column or not.
However, if you are indexing an expression like
PostgreSQL will then also collect statistics on the value distribution of the indexed expression. This happens automatically whenever autoanalyze runs, but it is a good idea to do it manually right after creating the index so you have good statistics right away.
ANALYZE the table after you create the index.That is because statistics on the value distribution of the table columns are always collected, no matter if there is an index on the column or not.
However, if you are indexing an expression like
upper(some_column) or (CAST(some_column AS date)), you should run ANALYZE after creating the index.PostgreSQL will then also collect statistics on the value distribution of the indexed expression. This happens automatically whenever autoanalyze runs, but it is a good idea to do it manually right after creating the index so you have good statistics right away.
Context
StackExchange Database Administrators Q#241257, answer score: 18
Revisions (0)
No revisions yet.