簡體   English   中英

Timescale/Postgres 查詢非常慢/低效?

[英]Timescale/Postgres queries very slow/inefficient?

我在 TimescaleDb 中有具有以下架構的表:

create table market_quotes
(
    instrument     varchar(16) not null,
    exchange       varchar(16) not null,
    time           timestamp   not null,
    bid_1_price    double precision,
    ask_1_price    double precision,
    bid_1_quantity double precision,
    ask_1_quantity double precision,
    bid_2_price    double precision,
    ask_2_price    double precision,
    bid_2_quantity double precision,
    ask_2_quantity double precision,
    bid_3_price    double precision,
    ask_3_price    double precision,
    bid_3_quantity double precision,
    ask_3_quantity double precision,
    bid_4_price    double precision,
    ask_4_price    double precision,
    bid_4_quantity double precision,
    ask_4_quantity double precision,
    bid_5_price    double precision,
    ask_5_price    double precision,
    bid_5_quantity double precision,
    ask_5_quantity double precision
);

以及以下綜合指數:

create index market_quotes_instrument_exchange_time_idx
    on market_quotes (instrument asc, exchange asc, time desc);

當我運行查詢時:

EXPLAIN ANALYZE select * from market_quotes where instrument='BTC/USD' and exchange='gdax' and time between '2020-06-02 00:00:00' and '2020-06-03 00:00:00'

返回 500k 行大約需要 2 分鍾:

Index Scan using _hyper_1_1_chunk_market_quotes_instrument_exchange_time_idx on _hyper_1_1_chunk  (cost=0.70..1353661.85 rows=1274806 width=183) (actual time=5.165..99990.424 rows=952931 loops=1)
"  Index Cond: (((instrument)::text = 'BTC/USD'::text) AND ((exchange)::text = 'gdax'::text) AND (""time"" >= '2020-06-02 00:00:00'::timestamp without time zone) AND (""time"" <= '2020-06-02 01:00:00'::timestamp without time zone))"
Planning Time: 11.389 ms
JIT:
  Functions: 2
"  Options: Inlining true, Optimization true, Expressions true, Deforming true"
"  Timing: Generation 0.404 ms, Inlining 0.000 ms, Optimization 0.000 ms, Emission 0.000 ms, Total 0.404 ms"
Execution Time: 100121.392 ms

當我運行下面的查詢時:

select * from market_quotes where instrument='BTC/USD' and exchange='gdax' and time between '2020-06-02 00:00:00' and '2020-06-03 00:00:00'

這運行了> 40分鍾並崩潰了。

我能做些什么來加快查詢速度? 我經常進行 1 天的查詢 - 如果我添加與一周中的某一天相對應的另一列並對其進行索引是否會有所幫助?

我是否應該每次都查詢一部分行並將信息拼湊在一起? (即一次 10000 行)

在將track_io_timing設置為on之后查看查詢的EXPLAIN (ANALYZE, BUFFERS)的結果會很有趣。

但是,如果您迫切希望加快索引范圍掃描,您能做的最好的事情就是對表進行集群:

CLUSTER market_quotes USING market_quotes_instrument_exchange_time_idx;

這將重寫表並阻止任何並發訪問。

如果您可以忍受稍微陳舊的數據,另一種方法是使用預先聚合的物化視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM