New answers tagged greatest-n-per-group
1
vote
How do I optimize this query that gets the newest entry of each group?
You can try the following query.
Adding an index on (tid, timestamp, id) should help with efficiency:
SELECT a.*
FROM
( SELECT DISTINCT tid
FROM ast
) AS t
LEFT JOIN ast AS a
ON ...
Community wiki
0
votes
How do I optimize this query that gets the newest entry of each group?
The only efficient solution for the "newest one of the each group" is denormalization.
You have to create the table of the same stucture as table storing your groups but with UNIQUE index on ...
Top 50 recent answers are included
Related Tags
greatest-n-per-group × 358mysql × 159
postgresql × 104
sql-server × 51
group-by × 40
performance × 27
join × 26
mariadb × 21
subquery × 19
query-performance × 18
query × 18
oracle × 17
aggregate × 17
select × 16
t-sql × 13
optimization × 13
mysql-5.7 × 13
postgresql-performance × 13
sql-server-2012 × 12
window-functions × 10
mysql-8.0 × 8
distinct × 8
sql-server-2014 × 7
order-by × 7
index × 6