【问题标题】:Would my postgresql trigger slows down my insert我的 postgresql 触发器会减慢我的插入速度吗
【发布时间】:2021-11-07 21:35:27
【问题描述】:

我正在构建一个触发器来跟踪进入我的 postgresql/timescaleDB 表的每个符号的最新时间戳。

表创建定义如下所示

create table symbol_cache (
  symbol text not null primary key,
  timestamp timestamptz not null
)

create or replace function refresh_symbol_cache() returns trigger
    language plpgsql
as $$
BEGIN
    INSERT INTO symbol_cache (symbol, timestamp)
    VALUES (
               NEW.symbol,
               NEW.timestamp
           )
    ON CONFLICT (symbol)
        DO UPDATE SET
                      symbol = NEW.symbol,
                      timestamp = GREATEST(old.timestamp, new.timestamp);
    RETURN NEW;
END;
$$;

create trigger update_symbol_cache
    after insert or update
    on db_009a005a_df_downloaded_grand
    for each row
execute procedure refresh_symbol_cache();

基本上我想问一下,我添加了这样的触发器后,我插入db_009a005a_df_downloaded_grand的速度会明显变慢吗?

【问题讨论】:

  • 一些问题: 1) 不要用保留字命名您的字段,例如“时间戳”。 2) GREATEST(old.timestamp, new.timestamp); 将在 INSERT 上失败,因为插入时没有 OLD 记录。 3) 不需要symbol = NEW.symbol,。如果你到达CONFLICT,它将在symbol,所以你知道它已经是正确的了。解决这些问题后,请尝试触发并查看开销是多少。
  • @AdrianKlaver,知道了!非常感谢您的帮助!

标签: postgresql timescaledb


【解决方案1】:

是的,触发器会显着降低数据修改速度。天下没有免费的午餐。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-02
    • 2011-02-23
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 2014-07-03
    相关资源
    最近更新 更多