Home > Mobile >  what index do I need when I use DISTINCT with Postgres?
what index do I need when I use DISTINCT with Postgres?

Time:11-04

with the following view:

CREATE OR REPLACE VIEW {accountId}.last_assets AS
SELECT DISTINCT ON (coin) * from {accountId}.{tableAssetsName} ORDER BY ts DESC LIMIT 1;

I want to get the latest of all rows with different 'coin' values. In short, the latest update of each coin.

Should I index (ts) and (coin) separately ? or should I index together (ts, coin)? (and why?)

CodePudding user response:

You could use an index on (coin, ts) in this order.

It is not guaranteed that PostgreSQL will use it, but it is the best index for the query.

  • Related