Another approach using ROW_NUMBER()
, which is widely supported across RDBMS:
SELECT *
FROM (
SELECT
c.*,
ROW_NUMBER() OVER(PARTITION BY mmsi ORDER BY time DESC) rn
FROM core_message c
) AS cr WHERE rn = 1
This query should benefit of existing index core_messag_mmsi_b36d69_idx
.