PostgreSQL
 sql >> Base de données >  >> RDS >> PostgreSQL

Comment puis-je prendre une sous-requête DISTINCT ON ordonnée par une colonne distincte et la rendre rapide?

Je me demande si vous pouvez faire en sorte que cela fonctionne :

select article_id, id, article_published_date
from prediction p
where p.prediction_date = (select max(p2.prediction_date)
                           from prediction p2
                           where p2.article_id = p.article_id
                          )
order by article_published_date desc;

Utilisez ensuite ces deux index :

  • (article_published_date desc, prediction_date, article_id, id)
  • (article_id, prediction_date desc) .