💎 Ruby on Rails
Intermediate
What is database indexing and why is it important in Rails?
Answer
A database index is a data structure that speeds up queries on specific columns at the cost of extra storage and slower writes. Without an index, queries do a full table scan; with an index, they use a B-tree lookup. In Rails migrations: add_index :users, :email or inline: t.string :email, index: true. Always index: foreign keys (Rails 5+ adds these automatically with belongs_to), unique constraint columns (add_index :users, :email, unique: true), and columns frequently used in WHERE clauses or JOINs. Use explain in the console to check if queries use indexes.
Previous
What is Rails testing (RSpec vs Minitest)?
Next
What is the Rails router's namespace and scope?