Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

📈 Creating Indexes - The Foundation of SQL Optimization

🌟 Overview

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.


📜 Syntax

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);

💡 Use Cases in AI/ML

  • Feature Lookup: Index columns like user_id for 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) for SELECT ... 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)).

🔑 Key Features

  • 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., INCLUDE in SQL Server).

✅ Best Practices

  • Index Selectively: Create indexes on columns used in WHERE, JOIN, GROUP BY, or ORDER BY clauses.
  • Keep It Lean: Avoid indexing every column—focus on high-selectivity columns (e.g., user_id over status).
  • Name Clearly: Use descriptive index names (e.g., idx_column_name) for maintainability.
  • Test Impact: Measure query performance before and after indexing using EXPLAIN or ANALYZE.

⚠️ Common Pitfalls

  • Over-Indexing: Too many indexes slow down INSERT, UPDATE, and DELETE operations.
  • 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., REINDEX in PostgreSQL).
  • Wrong Column Order: In multi-column indexes, place frequently filtered columns first (e.g., CREATE INDEX ON table (freq_col, rare_col)).

📝 Additional Notes

  • Database Variations: MySQL uses CREATE INDEX, PostgreSQL supports CREATE INDEX ... USING btree, SQL Server allows CLUSTERED or NONCLUSTERED.
  • 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_size in PostgreSQL.