【问题标题】:SELECT with yb_hash_code() and DELETE in YugabyteDB在 YugabyteDB 中使用 yb_hash_code() 和 DELETE 选择
【发布时间】:2022-07-20 00:58:46
【问题描述】:

[用户在YugabyteDB Community Slack上发布的问题]

我们在使用 YSQL 的 postgresql (yugabyte DB 2.8.3) 中有以下模式:

CREATE TABLE IF NOT EXISTS public.table1
(
customer_id uuid NOT NULL ,
item_id uuid NOT NULL ,
kind character varying(100) NOT NULL ,
details character varying(100) NOT NULL ,
created_date timestamp without time zone NOT NULL,
modified_date timestamp without time zone NOT NULL,

CONSTRAINT table1_pkey PRIMARY KEY (customer_id, kind, item_id)
);

CREATE UNIQUE INDEX IF NOT EXISTS unique_item_id ON table1(item_id);
CREATE UNIQUE INDEX IF NOT EXISTS unique_item ON table1(customer_id, kind) WHERE kind='NEW' OR kind='BACKUP';

CREATE TABLE  IF NOT EXISTS public.item_data
(
    item_id uuid NOT NULL,
    id2 integer NOT NULL,
    create_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL,
        CONSTRAINT item_data_pkey PRIMARY KEY (item_id, id2)
);

目标:

步骤 1) 从 table1 中选择 item_id's WHERE modified_date

步骤 2) 从表 item_data 中删除,其中 item_id = 步骤 1 中的任何 item_id

目前我们使用查询

SELECT item_id FROM table1 WHERE modified_date < $1

SELECT 查询可以将yb_hash_code(item_id) 与 SELECT 查询一起应用吗?因为 table1item_id 上被索引?提高 SELECT 查询的性能

目前我们执行:

DELETE FROM item_data x WHERE x.item_id IN the listOfItemIds(provided in Step1 above).

使用给定的listOfItemIds,我们可以使用yb_hash_code(item_id) 来提高DELETE 操作的性能吗?

【问题讨论】:

    标签: yugabytedb


    【解决方案1】:

    是的,应该会解决的。比如:

    SELECT item_id FROM item_data WHERE yb_hash_code(customer_id, kind, item_id) <= 128 AND yb_hash_code(customer_id, kind, item_id) >= 0 AND modified_date < x;
    

    虽然您可以在 1 个查询中组合 SELECT + DELETE(如子选择),但这可能会更好,因为它会产生更小的事务。 此外,无需使用yb_hash_code。 db 应该能够找到正确的行,因为您正在发送用于分区的列。

    【讨论】:

      猜你喜欢
      • 2022-07-19
      • 2022-07-19
      • 2019-10-06
      • 1970-01-01
      • 2021-09-23
      • 2011-01-15
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多