Introduction
Indexes make searching in a database faster, but they can slow down adding or changing data.
CREATE INDEX index_name ON table_name (column_name);
CREATE INDEX idx_customer_name ON customers (name);
CREATE INDEX idx_order_date ON orders (order_date);
CREATE INDEX idx_product_category_price ON products (category, price);
CREATE TABLE products ( id INT PRIMARY KEY, name VARCHAR(50), category VARCHAR(20), price DECIMAL(10,2) ); INSERT INTO products VALUES (1, 'Pen', 'Stationery', 1.20), (2, 'Notebook', 'Stationery', 2.50), (3, 'Coffee Mug', 'Kitchen', 5.00); -- Create index to speed up searches by category CREATE INDEX idx_category ON products (category); -- Query to find all stationery products SELECT * FROM products WHERE category = 'Stationery';
email in the table users?orders with 1 million rows and an index on customer_id, what is the expected effect of running SELECT * FROM orders WHERE customer_id = 123;?products has indexes on category_id and price. You run a query filtering on category_id and sorting by price. How can indexes help, and what might hurt performance?Pass the test and this pattern moves unknown