sql / intermediate
Snippet
Multi-Column Optimization using Composite Indexes
A composite index indexes values across multiple columns in a specified left-to-right hierarchy. This enhances query evaluation when filtering or sorting datasets by both columns simultaneously, reducing the need for full table scans.
snippet.sql
sql
1
2
CREATE INDEX idx_orders_customer_dateON orders (customer_id, order_date);
Breakdown
1
CREATE INDEX idx_orders_customer_date
Defines a new index named idx_orders_customer_date in the database schema.
2
ON orders (customer_id, order_date);
Attaches the index to the orders table spanning customer_id as the primary key and order_date as the secondary key.