Introduction
A covering index helps the database find all the data it needs from the index alone, making queries faster without looking at the main table.
CREATE INDEX index_name ON table_name (column1, column2, ...);
CREATE INDEX idx_customer_name_email ON customers (name, email);
CREATE INDEX idx_orders_date_status ON orders (order_date, status);
CREATE TABLE products ( product_id INT PRIMARY KEY, name VARCHAR(50), category VARCHAR(30), 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 idx_products_name_category ON products (name, category); -- Query that can use the covering index SELECT name, category FROM products WHERE name = 'Pen';
covering index in a database?name and age in a table users?CREATE INDEX index_name ON table_name(column1, column2);orders(order_id, customer_id, order_date, total) and an index on (customer_id, order_date, total), what will the query below use to speed up retrieval?SELECT customer_id, order_date, total FROM orders WHERE customer_id = 5;(product_id, price) but your query SELECT product_id, price, stock FROM products WHERE product_id = 10; is slow. What is the likely reason?stock is missing, causing table lookups. -> Option DSELECT user_id, last_login, status FROM users WHERE status = 'active' AND last_login > '2024-01-01';Pass the test and this pattern moves unknown