I have a table core_message in Postgres, with millions of rows that looks like this (simplified):
core_message
┌────────────────┬──
In Postgres, I recommend distinct on:
distinct on
SELECT DISTINCT ON (mmsi) m.* FROM core_message m ORDER BY mmsi, time DESC;
For best performance, you want an index on (mmsi, time desc).
(mmsi, time desc)