DBAtlas · Database operations atlas

Detect unused indexes

Oracle

-- Start monitoring:
ALTER INDEX customer_city_idx MONITORING USAGE;
-- Check after a while (12c+, all schemas):
SELECT owner, index_name, used FROM dba_object_usage;

If USED = NO, the index was not used during the monitoring period. Turn monitoring off with NOMONITORING USAGE. The old v$object_usage shows only the indexes of the session user and is deprecated since 12c. In 12.2+, index usage is tracked automatically in DBA_INDEX_USAGE.

PostgreSQL

SELECT relname AS table, indexrelname AS index, idx_scan
FROM pg_stat_user_indexes
WHERE idx_scan = 0
ORDER BY relname;

idx_scan = 0 means the index has not been used since statistics were last reset; do not drop UNIQUE/PK indexes.

MySQL

SELECT * FROM sys.schema_unused_indexes;

performance_schema must be enabled and the server must have been running for a while; UNIQUE constraints are excluded.

SQL Server

SELECT OBJECT_NAME(i.object_id) AS table_name, i.name AS index_name
FROM sys.indexes i
LEFT JOIN sys.dm_db_index_usage_stats s
  ON i.object_id = s.object_id AND i.index_id = s.index_id
  AND s.database_id = DB_ID()
WHERE i.type_desc = 'NONCLUSTERED'
  AND COALESCE(s.user_seeks + s.user_scans + s.user_lookups, 0) = 0;

usage_stats is reset when the server restarts; allow a sufficient observation window.

MongoDB

db.customer.aggregate([{ $indexStats: {} }])
// Indexes with low/0 accesses.ops are candidates

accesses.ops counts accesses since the accesses.since timestamp; the _id index cannot be dropped.

Redis

# Not applicable: core Redis has no secondary indexes.
# If the RediSearch module is used, check statistics with FT.INFO <index>.

Since the data model is key-value, there is no traditional index concept.

Open in DBAtlas →