rails-database-indexes

Design and implement database indexes for ActiveRecord models in Rails migrations.

4|Updated Feb 16, 2017
One-click install
npx skills add https://github.com/nekorush14/dotfiles --skill rails-database-indexes
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rails-database-indexes
Source: https://github.com/nekorush14/dotfiles/tree/main/configs/claude/skills/rails-database-indexes
Command: npx skills add https://github.com/nekorush14/dotfiles --skill rails-database-indexes

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill guides designing and implementing database indexes to optimize query performance in Rails applications.

Core Features & Use Cases

  • Index Strategy: Foreign keys, composite indexes, and unique constraints.
  • Partial & Conditional Indexes: Optimize for active records or filtered queries.
  • Migration Best Practices: Use Rails generators and proper migrations for index changes.

Quick Start

Generate a migration to add an index on user_id for orders and a composite index on user_id and status.

Frequently Asked Questions about rails-database-indexes

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I speed up slow database queries in Rails?

Database indexes speed up queries by organizing data for faster lookups. Create indexes on columns frequently used in WHERE clauses, JOIN conditions, and ORDER BY statements. Rails migrations make adding indexes straightforward with `add_index` directives during table creation or in separate migrations.

What types of indexes can I create in Rails migrations?

Rails supports single-column, composite (multi-column), unique, and partial indexes. You can also use specialized indexes like GiST or GIN for specific data types. Foreign key columns typically require indexes to accelerate JOIN operations. Migrations let you specify index names and ordering rules for precise control.

When should I add a composite index versus single-column indexes?

Use composite indexes when queries filter or join on multiple columns together. Composite indexes optimize queries matching the exact column order defined in the index. Single-column indexes work best for independent lookups. Rails migrations support both; choose based on your ActiveRecord query patterns.

Do I need indexes on foreign key columns in Rails?

Yes, foreign key columns should have indexes to speed up JOIN operations and lookups. Rails generators can add these automatically when creating associations. Indexing foreign keys prevents slow queries when filtering by related records and enforces referential integrity efficiently.

What's the difference between unique and partial indexes?

Unique indexes enforce data uniqueness and prevent duplicates while accelerating lookups. Partial indexes apply only to rows matching a condition, optimizing filtered queries on active records. Rails migrations support both; use partial indexes to reduce index size and improve performance for specific query subsets.

Can indexes improve performance for very large tables?

Yes, indexes significantly speed up lookups on large tables by reducing full table scans. Composite and partial indexes are especially effective for large datasets, targeting only the rows and columns your queries actually use. Migration-based index management in Rails scales from development to production.