postgresql-performance

Optimize GROUP BY query to retrieve latest row per user

心已入冬 提交于 2019-11-25 21:49:14
问题 I have the following log table for user messages (simplified form) in Postgres 9.2: CREATE TABLE log ( log_date DATE, user_id INTEGER, payload INTEGER ); It contains up to one record per user and per day. There will be approximately 500K records per day for 300 days. payload is ever increasing for each user (if that matters). I want to efficiently retrieve the latest record for each user before a specific date. My query is: SELECT user_id, max(log_date), max(payload) FROM log WHERE log_date <