Skip to content

Instantly share code, notes, and snippets.

View AAlvarez90's full-sized avatar
🏠
Working from home

Abraham Alvarez AAlvarez90

🏠
Working from home
  • Miami, FL
View GitHub Profile
@owaiswiz
owaiswiz / print_redundant_indexes.rb
Last active September 5, 2024 04:42
Print redundant indexes in a Rails app.
# Unnecessary indexes slows down writes and consumes additional storage and memory.
# Just paste this snippet in your Rails console (bundle exec rails c).
# And it will print all redundant indexes that are already covered by another index on the table:
# Table `pages`: index `site_idx` (site_id) already covered by `site_slug_idx` (site_id,slug)
# Table `optins`: index `list_idx` (list_id) already covered by `list_active_idx` (list_id,active)
ActiveRecord::Base.connection.tables.map do |table|
indexes = ActiveRecord::Base.connection.indexes(table).select(&:valid).reject(&:where)