"> <% if @query_stats_enabled %> Query stats are enabled <% else %> Query stats are not enabled <% end %>
"> <% if !@query_stats_enabled %> Query stats must be enabled for slow queries <% elsif @slow_queries.any? %> <%= pluralize(@slow_queries.size, "slow query") %> <% else %> No slow queries <% end %>
"> <% if @long_running_queries.any? %> <%= pluralize(@long_running_queries.size, "long running query") %> <% else %> No long running queries <% end %>
"> <% if @good_cache_rate %> Cache hit rate above 99% <% else %> Low cache hit rate <% end %>
"> <% if @missing_indexes.any? %> <%= pluralize(@missing_indexes.size, "table appears", "tables appear") %> to be missing indexes <% else %> No missing indexes <% end %>
"> <% if @unused_indexes.any? %> <%= pluralize(@unused_indexes.size, "unused index", "unused indexes") %> <% else %> No unused indexes <% end %>
<% if !@query_stats_enabled %>

Query Stats

<% if @query_stats_available %>

Query stats are available but not enabled. <%= button_to "Enable", enable_query_stats_path, class: "btn btn-info" %>

<% elsif @rds %>

Query stats are not available on Amazon RDS. <%= link_to "Tell Amazon you want this.", "https://forums.aws.amazon.com/thread.jspa?messageID=548724", target: "_blank" %>

<% else %>

Make them available by adding the following lines to postgresql.conf:

shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.track = all

Restart the server for the changes to take effect.

<% end %>
<% end %> <% if @query_stats_enabled && @slow_queries.any? %>

Slow Queries

Slow queries take 20 ms or more on average and have been called at least 100 times.

<%= link_to "Run EXPLAIN", explain_path %> on queries to see where to add indexes.

<%= render partial: "query_stats_table", locals: {queries: @slow_queries} %>
<% end %> <% if @long_running_queries.any? %>

Long Running Queries

<%= render partial: "queries_table", locals: {queries: @long_running_queries} %>
<% end %> <% if !@good_cache_rate %>

Low Cache Hit Rate

Index Hit Rate: <%= (@index_hit_rate * 100).round(1) %>%
Table Hit Rate: <%= (@table_hit_rate * 100).round(1) %>%

The cache hit rate <%= link_to "should be above 99%", "https://devcenter.heroku.com/articles/understanding-postgres-data-caching", target: "_blank" %> in most cases. You can often increase this by adding more memory.

<% end %> <% if @missing_indexes.any? %>

Missing Indexes

These tables have a large number of rows but indexes are not used often. Add indexes for faster queries.

<% @missing_indexes.each do |query| %> <% end %>
Table % of Time Index Used Rows in Table
<%= query["table"] %> <%= query["percent_of_times_index_used"] %> <%= number_with_delimiter(query["rows_in_table"]) %>
<% end %> <% if @unused_indexes.any? %>

Unused Indexes

Unused indexes cause unnecessary overhead. Remove them for faster writes.

<% @unused_indexes.each do |query| %> <% end %>
Name Index Size Index Scans
<%= query["index"] %>
on <%= query["table"] %>
<%= query["index_size"] %> <%= query["index_scans"] %>
<% end %>