Efficient latest record query with Postgresql

前端 未结 5 946
深忆病人
深忆病人 2020-12-25 09:59

I need to do a big query, but I only want the latest records.

For a single entry I would probably do something like

SELECT * FROM table WHERE id = ?          


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-25 10:13

    If you don't want to change your data model, you can use DISTINCT ON to fetch the newest record from table "b" for each entry in "a":

    SELECT DISTINCT ON (a.id) *
    FROM a
    INNER JOIN b ON a.id=b.id
    ORDER BY a.id, b.date DESC
    

    If you want to avoid a "sort" in the query, adding an index like this might help you, but I am not sure:

    CREATE INDEX b_id_date ON b (id, date DESC)
    
    SELECT DISTINCT ON (b.id) *
    FROM a
    INNER JOIN b ON a.id=b.id
    ORDER BY b.id, b.date DESC
    

    Alternatively, if you want to sort records from table "a" some way:

    SELECT DISTINCT ON (sort_column, a.id) *
    FROM a
    INNER JOIN b ON a.id=b.id
    ORDER BY sort_column, a.id, b.date DESC
    

    Alternative approaches

    However, all of the above queries still need to read all referenced rows from table "b", so if you have lots of data, it might still just be too slow.

    You could create a new table, which only holds the newest "b" record for each a.id -- or even move those columns into the "a" table itself.

提交回复
热议问题