Is it bad practice to have a "most_recent" boolean column in addition to a "create_at" timestamp column to track the latest version of a record?
11
votes
6
answers
3207
views
The table looks like this, it's SCD type 2:
+-----------+------------------+------------------------+
| id (text) | version (serial) | created_at (timestamp) |
+-----------+------------------+------------------------+
For 99% of queries we will be searching the entire table and filtering by additional columns and join tables. For these queries we're only interested in the most recent version of a record per unique ID. We will also be sorting by created_at and other columns.
To make it easy to find the most current records I was considering to add a most_recent (boolean)
column as described in the answer here:
https://stackoverflow.com/questions/34495479/add-constraint-to-make-column-unique-per-group-of-rows/34495621#34495621
However I realized we already have the created_at
column which tells us this information - we could use a DISTINCT clause in our search queries and order by created_date as described by @Svet's answer here:
https://stackoverflow.com/questions/17327043/how-can-i-select-rows-with-most-recent-timestamp-for-each-key-value
However, we'd then have to re-order the results by the column that we actually want to use to show the data.
It seems simpler in the long run to add the extra 'current' field, and like it would be more performant, but is it also bad practice?
Asked by Henry
(213 rep)
Jan 11, 2024, 07:19 PM
Last activity: Jan 14, 2024, 12:43 AM
Last activity: Jan 14, 2024, 12:43 AM