Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently get the last timestamp per asset without sequential scan on timescaledb?

I have a table

| Asset |   timestamp   | open | high | low | close | volume |
|-------|---------------|------|------|-----|-------|--------|
| AAPL  | 1551274920000 | 200  | 300  | 200 | 250   | 10240  |
| AAPL  | 1551274980000 | 201.4| 299.5| 200 | 244.5 | 11871  |
| GOOG  | 1551274980000 | 471.2| 488.2|464.6| 474.84| 5476.58|

How do I get the latest timestamp per asset. Here is what I tried so far

Approach 1

SELECT symbol, max(ts) from ohlc_60 group by symbol;

This seems to scan all the chunks
explain select symbol, max(ts) from ohlc_60 group by symbol;
                                      QUERY PLAN                                      
--------------------------------------------------------------------------------------
 HashAggregate  (cost=1199.37..1201.37 rows=200 width=16)
   Group Key: ohlc_60.symbol
   ->  Append  (cost=0.00..1014.27 rows=37019 width=16)
         ->  Seq Scan on binance_ohlc_60  (cost=0.00..0.00 rows=1 width=40)
         ->  Seq Scan on _hyper_67_199_chunk  (cost=0.00..197.81 rows=8681 width=15)
         ->  Seq Scan on _hyper_67_200_chunk  (cost=0.00..284.32 rows=13032 width=15)
         ->  Seq Scan on _hyper_67_201_chunk  (cost=0.00..281.14 rows=12414 width=15)
         ->  Seq Scan on _hyper_67_202_chunk  (cost=0.00..48.41 rows=2141 width=15)
         ->  Seq Scan on _hyper_67_203_chunk  (cost=0.00..17.50 rows=750 width=40)
(9 rows)

I am storing 1m data into 1 hour chunks currently but will probably turn them into 1 day chunks. I need to get the last timestamp of each asset without doing a sequential scan, Any suggestions are super appreciated?

like image 603
PirateApp Avatar asked Dec 23 '22 00:12

PirateApp


2 Answers

Your query should benefit well from the following index:

CREATE INDEX idx ON ohlc_60 (symbol, ts);

The reason this should work is that you are asking for the max value of the timestamp for each symbol group. This can easily be gotten from a scan of a B-tree.

like image 146
Tim Biegeleisen Avatar answered Jan 17 '23 17:01

Tim Biegeleisen


use corelated subquery

select t1.* from table_name t1
where t1.timestamp=(select max(timestamp) from table_name t2 where t2.Asset=t1.Asset)

And for making faster scan you need to adapt indexing in the appropriate column that already suggested in other answer by @Tim

like image 31
Zaynul Abadin Tuhin Avatar answered Jan 17 '23 16:01

Zaynul Abadin Tuhin