sql / intermediate
Snippet
Optimizing Multi-Attribute Queries with Composite Indexes
A composite index spans multiple columns in a specific order. It speeds up filtering operations that evaluate combinations of these columns, significantly reducing database scan overhead.
snippet.sql
sql
1
CREATE INDEX idx_orders_customer_date ON orders (customer_id, order_date);
Breakdown
1
CREATE INDEX idx_orders_customer_date ON orders (customer_id, order_date);
Creates an index named idx_orders_customer_date on the orders table indexing customer_id followed by order_date.