An Index for a Calculated Value
MediumDescription
Finance often looks for order lines by their value, quantity * unit_price, with this query:
CodeSELECT * FROM order_items WHERE quantity * unit_price = 24999;
Create an index named idx_line_value that this query can use. Then show the query's plan
without costs, with exactly this statement:
CodeEXPLAIN (COSTS OFF) SELECT * FROM order_items WHERE quantity * unit_price = 24999;
Tables
| order_items |
|---|
| item_id (PK) |
| order_id (FK) |
| product_id (FK) |
| quantity |
| unit_price |
| discount_pct |