小编Flo*_*ein的帖子

如何使用大表上的 CTE 优化键集分页查询?

在来这里打扰你之前,我试图尽可能多地记录我自己的话题,但无论如何我都在这里。

我们想在这个表上实现键集分页:

create table api.subscription (
    subscription_id uuid primary key,
    token_id uuid not null,
    product_id uuid not null references api.product(product_id) deferrable,
    spid bigint null,
    attributes_snapshot jsonb not null,
    created_at timestamp not null,
    refreshed_at timestamp,
    enriched_at timestamp null,
    valid_until timestamp not null,
    is_cancelled boolean not null,
    has_been_expired boolean not null,
    has_quality_data boolean not null
);
Run Code Online (Sandbox Code Playgroud)

为此,我们使用此查询来准备分页元数据:

with book as (
    select created_at, subscription_id
    from api.subscription
    where token_id = $1
    and refreshed_at >= $2
    and valid_until >= now()
    and not is_cancelled
    and …
Run Code Online (Sandbox Code Playgroud)

postgresql cte paging

5
推荐指数
1
解决办法
638
查看次数

标签 统计

cte ×1

paging ×1

postgresql ×1