Simple Tips for PostgreSQL Query Optimization

  1. Explain analyze 
  2. One index per query
  3. Column order in a multicolumn index
  4. Filters + joins
Image result for optimization of queries in postgresql
Add caption
Reference : https://statsbot.co/blog/postgresql-query-optimization/



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

Popular posts from this blog