sql >> Base de Datos >  >> RDS >> PostgreSQL

¿Cómo tomo una subconsulta DISTINCT ON que está ordenada por una columna separada y la hago rápida?

Me pregunto si puedes hacer que esto funcione:

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;

Luego use estos dos índices:

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