A PostgreSQL 15 database hosts a sales table with more than 10 million rows:
sale_id BIGINT (PK)
product_id INT
quantity INT
transaction_date DATE
store_id INT
Analysts run this parameterized query dozens of times per hour:
SELECT product_id, SUM(quantity) AS total_qty
FROM sales
WHERE transaction_date BETWEEN :start_date AND :end_date
GROUP BY product_id;
The query currently performs a sequential scan and finishes in about 45 seconds. Apart from the primary-key index on sale_id, no other indexes exist. You must speed up the query without rewriting it. Which indexing strategy is most likely to cut execution time to under a second?
Create a hash index on product_id.
Create a composite B-tree index on (transaction_date, product_id).
Create a B-tree index on quantity.
Create a GIN index on product_id for full-text search.
The filter predicates restrict rows only by transaction_date, so the first column in any useful index must be that date field. Adding product_id as the second column makes the index covering for the query: the database can read the needed rows within the date range and return product_id values that are already grouped in index order, allowing an index-only scan. A single-column index on transaction_date helps the filter but still forces a heap visit for every qualifying row; the composite index avoids most of those visits. Hash indexes accelerate equality predicates only, so one on product_id would not help the range filter. A GIN index is designed for full-text search over tsvector data, not for numeric product IDs, and a B-tree on quantity does nothing for either the filter or the grouping. Therefore, a composite B-tree index on (transaction_date, product_id) is the most effective option.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What is a composite B-tree index and why is it useful?
Open an interactive chat with Bash
Why doesn't a single-column index on transaction_date improve performance enough?
Open an interactive chat with Bash
Why is a GIN index not suitable for this query?
Open an interactive chat with Bash
CompTIA Data+ DA0-002 (V2)
Data Acquisition and Preparation
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
Pass with Confidence.
IT & Cybersecurity Package
You have hit the limits of our free tier, become a Premium Member today for unlimited access.
Military, Healthcare worker, Gov. employee or Teacher? See if you qualify for a Community Discount.
Monthly
$19.99 $11.99
$11.99/mo
Billed monthly, Cancel any time.
$19.99 after promotion ends
3 Month Pass
$44.99 $26.99
$8.99/mo
One time purchase of $26.99, Does not auto-renew.
$44.99 after promotion ends
Save $18!
MOST POPULAR
Annual Pass
$119.99 $71.99
$5.99/mo
One time purchase of $71.99, Does not auto-renew.
$119.99 after promotion ends
Save $48!
BEST DEAL
Lifetime Pass
$189.99 $113.99
One time purchase, Good for life.
Save $76!
What You Get
All IT & Cybersecurity Package plans include the following perks and exams .