postgres statistics - ghdrako/doc_snipets GitHub Wiki
The statistic collector collects various statistics about tables, indexes, and database activity, providing valuable insights into data distribution and access patterns. This information is then used by the statistic analyzer, which analyzes the collected statistics and updates them accordingly. By enabling these components and regularly analyzing the statistics, database administrators can ensure that the query planner makes accurate decisions based on the actual data distribution, resulting in more efficient query execution and improved overall performance of the PostgreSQL database.
- Enabling the statistic collect or postgresql.conf
track_counts = on
- Querying statistics
SELECT * FROM pg_stat_all_tables WHERE relname = 'testings';
-
Analyzing statistics
ANALYZE <Table_Name>;
VACUUM ANALYZE <Table_Name>; -- command to perform both vacuuming and analysis simultaneously.
ALTER TABLE users
ALTER COLUMN first_name SET STATISTICS 5_000;
ANALYZE users;
SELECT
attname,
n_distinct,
most_common_vals
FROM pg_stats
WHERE schemaname = 'rideshare'
AND tablename = 'users'
AND attname = 'first_name';
-- -[ RECORD 1 ]----+---------------------------------------------------
-- attname | first_name
-- n_distinct | -0.9931146
-- most_common_vals | {Alonso,Fred,Harris,Corey,
-- Dominique,Jamal,Marion,Mary,Reed,Sam,Sandy,Stevie}
-- Set statistics target back to default of 1000