I have a table myTable
with four columns:
id UUID,
user_id UUID ,
text VARCHAR ,
date TIMESTAMP
(id
is the primary key and user_id
is not unique in this table)
I want to retrieve the user_id
s ordered by their newest entry, which i am currently doing with this query:
SELECT user_id FROM myTable GROUP BY user_id ORDER BY MAX(date) DESC
The problem is that GROUP BY
takes a long time. Is there a faster way to accomplish this? I tried using a window function with PARTITION BY
as described here Retrieving the last record in each group - MySQL, but it didn't really speed things up. I've also made sure that user_id
is indexed.
My postgres version is 10.4
Edit: The query above that I'm currently using is functionally correct, the problem is that it's slow.