Creating Indexes is the starting point for boosting SQL query performance by enabling faster data retrieval. An index is a database structure that organizes data for quick lookups, like an index in a book, reducing the need to scan entire tables. In AI/ML, creating indexes is critical for speeding up queries on large datasets used in model training, feature extraction, and inference.
CREATE INDEX index_name ON table_name (column1, column2, ...);- Basic Form: Create an index on one or more columns.
- Example:
CREATE INDEX idx_model_id ON predictions (model_id);
- Multi-Column Index:
CREATE INDEX idx_pred_date_score ON predictions (prediction_date, score);
- Unique Index:
CREATE UNIQUE INDEX idx_unique_user ON users (email);
- Feature Lookup: Index columns like
user_idfor fast feature retrieval (e.g.,CREATE INDEX idx_user_id ON features (user_id)). - Prediction Filtering: Speed up queries on prediction data (e.g.,
CREATE INDEX idx_score ON predictions (score)forSELECT ... WHERE score > 0.9). - Data Pipeline Optimization: Index timestamp columns for efficient time-based queries (e.g.,
CREATE INDEX idx_timestamp ON logs (event_time)). - Deduplication: Use unique indexes to enforce data integrity (e.g.,
CREATE UNIQUE INDEX idx_model_name ON models (model_name)).
- Index Types: Supports B-Tree (default), Hash, R-Tree, Full-Text, and more, depending on the database.
- Single/Multi-Column: Index one column for simple lookups or multiple for complex queries.
- Unique Indexes: Enforce uniqueness, preventing duplicate values.
- Flexibility: Apply to any table column, with database-specific options (e.g.,
INCLUDEin SQL Server).
- Index Selectively: Create indexes on columns used in
WHERE,JOIN,GROUP BY, orORDER BYclauses. - Keep It Lean: Avoid indexing every column—focus on high-selectivity columns (e.g.,
user_idoverstatus). - Name Clearly: Use descriptive index names (e.g.,
idx_column_name) for maintainability. - Test Impact: Measure query performance before and after indexing using
EXPLAINorANALYZE.
- Over-Indexing: Too many indexes slow down
INSERT,UPDATE, andDELETEoperations. - Low Selectivity: Indexing columns with few unique values (e.g.,
gender) wastes space and offers little speedup. - Ignoring Maintenance: Indexes can become fragmented—monitor and rebuild as needed (e.g.,
REINDEXin PostgreSQL). - Wrong Column Order: In multi-column indexes, place frequently filtered columns first (e.g.,
CREATE INDEX ON table (freq_col, rare_col)).
- Database Variations: MySQL uses
CREATE INDEX, PostgreSQL supportsCREATE INDEX ... USING btree, SQL Server allowsCLUSTEREDorNONCLUSTERED. - Performance Trade-Off: Indexes speed up reads but slow down writes—balance based on workload (e.g., read-heavy ML queries vs. write-heavy logging).
- Storage Cost: Indexes consume disk space—monitor usage with tools like
pg_indexes_sizein PostgreSQL.