Core Module
12 min forge
SQL Indexing & Performance
How indexes work, B-Trees vs Hash, and query optimization.
SQL Indexing & Performance
π What is it
An index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space.
ποΈ How it works (B-Tree)
Most SQL databases use B-Tree structures for indexing.
- It keeps data sorted and allows for binary search.
- When you search for a value, the DB traverses the tree instead of scanning the whole table (Full Table Scan).
β‘ Types of Indexes
- Clustered Index: Determines the physical order of data in the table. Each table can have only one (usually the Primary Key).
- Non-Clustered Index: Stores a sorted list of values and pointers to the actual data rows.
- Unique Index: Ensures that indexed columns do not have duplicate values.
- Composite Index: An index on multiple columns.
π§ Index Optimization Rules
- Index the Right Columns: Search columns (
WHERE), Join columns (ON), and Sorting columns (ORDER BY). - Avoid Over-indexing: Every index slows down
INSERT,UPDATE, andDELETEoperations. - Selectivity: High selectivity (columns with many unique values) makes for better indexes.
π» Code example
sql Standard-- Creating an index CREATE INDEX idx_user_email ON Users(Email); -- Composite index CREATE INDEX idx_order_date_status ON Orders(OrderDate, Status);
β Common mistakes
- Indexing Boolean columns: Creating an index on a
bitorbooleancolumn (low selectivity; rarely used by DB optimizer). - Wildcard searching: Using
LIKE '%keyword'(leading wildcard). This prevents the DB from using the index. - Functions on Columns: Using functions on indexed columns in
WHEREclause (e.g.,WHERE YEAR(JoinDate) = 2023). This disables index usage (SARGability).