What is the fastest way to fetch the last row from a table?
-
02-11-2019 - |
Question
I have a PostgreSQL table, Prices
, with the columns:
price
(Decimal)product_id
(Int)
There are also created_at
and updated_at
columns.
Prices get updated regularly and I keep old prices in the table. For a given product, the last price in the table is the current price.
What is the most efficient way to get the last price for a specific product:
- Index
product_id
and query for the last record - Add a third column
active
(Boolean) to mark the latest price and create a composite index (product_id
andactive
) - Or something else?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange