Finding unused indexes
Selecting the correct set of indexes for a workload is known to be a hard problem. It usually involves trial and error by developers and DBAs to get a good mix of indexes.
Tools for identifying slow queries exist and many SELECT
statements can be improved by adding an index.
What many people forget is to check whether the mix of indexes remains valuable over time, which is something for the DBA to investigate and optimize.
How to do it…
PostgreSQL keeps track of each access against an index. We can view that information and use it to see whether an index is unused, as follows:
postgres=# SELECT schemaname, relname, indexrelname, idx_scan FROM pg_stat_user_indexes ORDER BY idx_scan; schemaname | indexrelname | idx_scan ------------+--------------------------+---------- public | pgbench_accounts_bid_idx | ...