Postgres Slow group by query with max

前端 未结 3 1635
南旧
南旧 2021-01-14 08:37

I am using postgres 9.1 and I have a table with about 3.5M rows of eventtype (varchar) and eventtime (timestamp) - and some other fields. There are only about 20 different

3条回答
  •  心在旅途
    2021-01-14 08:59

    What you need is a "skip scan" or "loose index scan". PostgreSQL's planner does not yet implement those automatically, but you can trick it into using one by using a recursive query.

    WITH RECURSIVE  t AS (
    SELECT min(eventtype) AS eventtype FROM allevents
               UNION ALL
    SELECT (SELECT min(eventtype) as eventtype FROM allevents WHERE eventtype > t.eventtype)
       FROM t where t.eventtype is not null
    )
    select eventtype, (select max(eventtime) from allevents where eventtype=t.eventtype) from t;
    

    There may be a way to collapse the max(eventtime) into the recursive query rather than doing it outside that query, but if so I have not hit upon it.

    This needs an index on (eventtype, eventtime) in order to be efficient. You can have it be DESC on the eventtime, but that is not necessary. This is efficiently only if eventtype has only a few distinct values (21 of them, in your case).

提交回复
热议问题