What is an index in SQL?
Answer
An index is a database structure (separate from the table data) that allows the database engine to find rows quickly without scanning the entire table. Analogous to a book's index — instead of reading every page to find a topic, you check the index for the page number. Without an index, MySQL performs a full table scan (O(n)). With a B-tree index, lookups are O(log n). Creating an index: CREATE INDEX idx_email ON users (email);. MySQL automatically creates an index on PRIMARY KEY and UNIQUE columns. Indexes speed up: SELECT WHERE clauses, JOIN conditions, ORDER BY and GROUP BY (can avoid sort), and covering queries (all needed columns are in the index). Indexes slow down: INSERT, UPDATE, DELETE (the index must be maintained). Storage cost: indexes take additional disk space. Types in MySQL: B-tree (default, most common), FULLTEXT (for text search), SPATIAL (for geometry data), HASH (only in Memory engine). Choosing which columns to index: frequently queried columns, JOIN columns, columns in ORDER BY, columns with high cardinality (many unique values — indexing gender with M/F is wasteful, indexing email is beneficial).
Previous
What is denormalization?
Next
What is the difference between a clustered and non-clustered index?