Thanks for the reply! the schema is basically this (simplified): table users (user_id,user_group,user_name) table events (user_id,user_group,event_id,timestamp_inc,event_description) Views: "last_user_event_2" SELECT e.* FROM users u JOIN LATERAL (SELECT * FROM events WHERE user_id = u.user_id AND user_group = u.user_group ORDER BY timestamp_inc DESC LIMIT 1 ) e ON TRUE "last_user_event_1" SELECT DISTINCT ON (user_id) * FROM events ORDER BY user_id, timestamp_inc DESC
The query itself is: SELECT * FROM users u JOIN last_user_event_(1|2) e USING (user_id,user_group) This explain plan: https://explain.depesz.com/s/oyEp is what Postgres uses with "last_user_event_2" and https://explain.depesz.com/s/hWwF, "last_user_event_1" I do have a btree index on user_id,user_group,timestamp_inc DESC. |