What is the fastest way to fetch the last row from a table?
14
votes
3
answers
46516
views
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
and active
)
- Or something else?
Asked by Mike81
(315 rep)
Feb 16, 2014, 07:49 AM
Last activity: May 8, 2020, 02:58 AM
Last activity: May 8, 2020, 02:58 AM