What is the fastest way to fetch the last row from a table?
-
02-11-2019 - |
문제
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?
올바른 솔루션이 없습니다
제휴하지 않습니다 dba.stackexchange