Simple Tips for PostgreSQL Query Optimization
- Explain analyze
- One index per query
- Column order in a multicolumn index
- Filters + joins
![]() |
Add caption |
Reference : https://statsbot.co/blog/postgresql-query-optimization/
One obvious way to start with tuning is to find specific statements that are performing poorly.
Finding Slow Queries
One obvious way to start with tuning is to find specific statements that are performing poorly.
pg_stats_statements
The pg_stats_statements module is a great place to start. It simply tracks execution statistics of SQL statements and can be an easy way to find poor performing queries.Once you have this module installed, a system view named pg_stat_statements will be available with all sorts of goodness. Once it has had a chance to collect a good amount of data, look for queries that have relatively high total_time value. Focus on these statements first.SELECT *
FROM
pg_stat_statements
ORDER BY
total_time DESC;
auto_explain
The auto_explain module is also helpful for finding slow queries but has 2 distinct advantages: it logs the actual execution plan and supports logging nested statements using the log_nested_statements option.
Comments
Post a Comment