如何优化插入速度到超表?

时间:2018-12-24 13:44:08

标签: postgresql hypertable timescaledb

我有一个像这样的表(PostgreSQL 9.6)

CREATE TABLE m_trade ( 
    "alias" Character Varying( 32 ),
    "ts" Bigint NOT NULL,
    "side" Character( 1 ),
    "price" Double Precision,
    "qty" Bigint );

具有5亿行。

创建timescaledb扩展名后-

CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;

我从空表创建hyper_table-

CREATE TABLE d_trade ( 
    "alias" Character Varying( 32 ),
    "ts" Bigint NOT NULL,
    "side" Character( 1 ),
    "price" Double Precision,
    "qty" Bigint );


SELECT create_hypertable('d_trade', 'ts', chunk_time_interval => 86400 , number_partitions =>31);

在此之后,我在d_trade表上得到

INDEX "d_trade_ts_idx"  and 

TRIGGER ts_insert_blocker BEFORE INSERT  

INSERT INTO
    "public"."d_trade"( "alias", "price", "qty", "side", "ts" )
select "alias", "price", "qty", "side", "ts" from m_trade

现在,如果我尝试通过

将行插入到m_trade表中
INSERT INTO
    "public"."m_trade"( "alias", "price", "qty", "side", "ts" )
VALUES
    ('TESTALS', 16000, 5, 2, 1545307519)

这将花费1-1.5毫秒,但此延迟将延迟4-5毫秒

INSERT INTO
    "public"."d_trade"( "alias", "price", "qty", "side", "ts" )
VALUES
    ('TESTALS', 16000, 5, 2, 1545307519)

如何优化d_trade表上的插入速度?

1 个答案:

答案 0 :(得分:0)

您如何测量此刀片的性能?如果使用的是EXPLAIN之类的内容,则timescaledb需要额外的开销,这可能会使此比较无效。尝试改用\ timing。此外,timescaledb在规模上要比postgresql更好,因此我将尝试运行批处理语句以查看是否仍然获得与上述相同的结果。