Skip to content
Advertisement

Retrieve all records with the 5 most recent distinct dates

I need to retrieve data for a history module with pagination. The requirement is to show the records for the five most recent dates first (regardless of the number of rows) and load the remaining records in lots (next five dates, any number of rows) as requested by the user.

I came up with the query below, which is extremely simplified based on the real one.

Performance is important here and this query is not performing very well. In productions there are several JOINs and a bunch of conditions, which need to be applied twice (main and subquery).

Poor performance could be related to the relationships and all the conditions, however I am wondering if there is any other way I could filter by the 5 most recent (distinct) dates and retrieve all related records. Any help is much appreciated.

Advertisement

Answer

Try using JOIN instead:

Or dense_rank() might be yet faster:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement