【问题标题】:Get values appearing at least N times in a table quickly快速获取在表格中至少出现 N 次的值
【发布时间】:2021-05-20 07:37:52
【问题描述】:

我有一个 Postgres 10.10 数据库,其中包含超过 600 万行的表和以下定义:

create table users (
    id              bigserial primary key,
    user_id         text      unique,
    username        text,
    first_name      text,
    last_name       text,
    language_code   text,
    gender          text,
    first_seen      timestamp with time zone,
    last_seen       timestamp with time zone,
    search_language text,
    age             text
);

create index users_language_code_idx on users (language_code);
create index users_last_seen_idx     on users (last_seen);
create index users_first_seen_idx1   on users (first_seen);
create index users_age_idx           on users (age);
create index users_last_seen_age_idx on users (last_seen, age);

我有一个查询来获取超过 100 个用户的流行语言代码:

SELECT language_code FROM users
GROUP BY language_code
HAVING count(*) > 100;

在某些时候,此查询开始需要大量时间才能完成(约 10 分钟)。 language_code 上的 Btree 索引没有帮助。我还能做些什么来提高性能?

这是explain analyze 输出:

https://explain.depesz.com/s/j2ga

Finalize GroupAggregate  (cost=7539479.67..7539480.34 rows=27 width=3) (actual time=620744.389..620744.458 rows=24 loops=1)
  Group Key: language_code
  Filter: (count(*) > 100)
  Rows Removed by Filter: 60
  ->  Sort  (cost=7539479.67..7539479.80 rows=54 width=11) (actual time=620744.359..620744.372 rows=84 loops=1)
        Sort Key: language_code
        Sort Method: quicksort  Memory: 28kB
        ->  Gather  (cost=7539472.44..7539478.11 rows=54 width=11) (actual time=620744.038..620744.727 rows=84 loops=1)
              Workers Planned: 2
              Workers Launched: 0
              ->  Partial HashAggregate  (cost=7538472.44..7538472.71 rows=27 width=11) (actual time=620743.596..620743.633 rows=84 loops=1)
                    Group Key: language_code
                    ->  Parallel Seq Scan on users  (cost=0.00..7525174.96 rows=2659496 width=3) (actual time=0.377..616632.155 rows=6334894 loops=1)
Planning time: 0.194 ms
Execution time: 620745.276 ms

【问题讨论】:

  • 你有一张表language_code 有一组不同的语言代码吗? (通常你应该有那个。)你有users(language_code) 的索引吗?考虑 PostgreSQL 性能问题的说明:stackoverflow.com/tags/postgresql-performance/info
  • @ErwinBrandstetter 你好,我在users(language_code) 上有一个索引,但我没有一个具有不同language_codes 的表
  • 哦,请以 文本 的形式提供您的表格结构,而不是图像。理想情况下,一个有效的CREATE TABLE 脚本。永远不要为文字图片。
  • @ErwinBrandstetter 抱歉,在文本中添加了表格结构 + 创建表格脚本

标签: sql postgresql indexing greatest-n-per-group postgresql-performance


【解决方案1】:

您可以通过模拟索引跳过扫描充分利用(language_code) 上的索引:

WITH RECURSIVE cte AS (
   SELECT min(language_code) AS language_code
   FROM   users
   
   UNION ALL
   SELECT (SELECT language_code
           FROM   users
           WHERE  language_code > c.language_code
           ORDER  BY language_code
           LIMIT  1)
   FROM   cte c
   WHERE  c.language_code IS NOT NULL
   )
SELECT language_code
FROM   cte c
JOIN   LATERAL (
   SELECT count(*) AS ct
   FROM  (
      SELECT -- can stay empty
      FROM   users
      WHERE  language_code = c.language_code 
      LIMIT  101
      ) sub
   ) u ON ct > 100  -- "more than 100"
WHERE  language_code IS NOT NULL;

db小提琴here

考虑到您的数字(6M 行,但只有一堆不同的语言代码),这应该会快几个数量级。

第一部分 - 名为 cte 的递归 CTE (rCTE) - 在表中生成一组不同的 language_codeNULL 除外)。具有不同语言代码的表可以替换该部分以更快。 (维护这样的表并使用 FK 约束来强制引用完整性可能是个好主意...)

第二部分仅查看每个语言代码最多 101 行(您的阈值)。这样我们就避免了对整个大表进行昂贵的顺序扫描。

如果您的表足够“清空”,您应该只看到仅索引扫描

升级到当前版本 Postgres 13 应该会有所帮助,因为新引入的 index deduplication 应该会大大减小所述索引(因为它是高度重复的)。

遗憾的是,自动索引跳过扫描没有进入版本 13。也许 Postgres 14。但上面的模拟应该几乎一样好。

进一步阅读(上面的查询技术有详细解释):

【讨论】:

  • 哇,它就像魔术一样,非常感谢您格式化我的问题的答案!
  • @qwertyqwerty:现在需要多少时间?
  • Factor 1000 与我预期的差不多。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-25
  • 2017-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多