Given a table employees(id INT, name TEXT, department TEXT, salary INT) with an index created as CREATE INDEX idx_dept_salary ON employees(department) INCLUDE (salary);, which query can fully use the index without accessing the table heap?
Think about which columns are in the index key and which are included.
The index idx_dept_salary has department as key and salary as included column. Queries selecting only these columns and filtering by department can use index-only scans, avoiding heap access.
Which of the following is the correct syntax to create a covering index on orders(order_date) including customer_id and total_amount columns in PostgreSQL?
Remember the position of the INCLUDE clause in the syntax.
The correct syntax places the INCLUDE clause after the indexed columns list. Option B follows this pattern.
What is the main advantage of adding columns with INCLUDE in a PostgreSQL index?
Think about how included columns affect index size and query performance.
Including columns in an index stores them only in the index leaf nodes, not in the key, allowing index-only scans without bloating the index key size.
Consider the table products(id INT, category TEXT, price NUMERIC, stock INT) and the index:
CREATE INDEX idx_cat_price ON products(category, price);
Why might a query SELECT category, stock FROM products WHERE category = 'Books'; not use an index-only scan?
Think about which columns are available in the index and which are requested by the query.
The query selects stock, which is not in the index key or included columns, so PostgreSQL must access the table heap to get that data, preventing index-only scans.
You have a large table sales(order_id INT, customer_id INT, order_date DATE, total NUMERIC, status TEXT). You often run this query:
SELECT order_date, total FROM sales WHERE status = 'completed';
Which index will best optimize this query for index-only scans?
Consider which columns are filtered and which are selected, and how INCLUDE affects index size.
Option D indexes status as key for filtering and includes order_date and total to cover the query columns without bloating the key size, enabling efficient index-only scans.