postgresql - Postgres 不使用索引对数据进行排序

标签 postgresql indexing sql-order-by

我有一个表 learners,它有大约 320 万行。该表包含用户相关信息,如姓名和电子邮件。我需要优化一些在某些列上使用 order by 的查询。因此,为了进行测试,我创建了一个 temp_learners 表,其中包含 80 万行。我在这个表上创建了两个索引:

CREATE UNIQUE INDEX "temp_learners_companyId_userId_idx"
  ON temp_learners ("companyId" ASC, "userId" ASC, "learnerUserName" ASC, "learnerEmailId" ASC);

CREATE INDEX temp_learners_company_name_email_index
  ON temp_learners ("companyId", "learnerUserName", "learnerEmailId");

第二个索引仅用于测试。 现在,当我运行此查询时:

SELECT *
FROM temp_learners
WHERE "companyId" = 909666665757230431 AND "userId" IN (
                                                        4990609084216745771,
                                                        4990610022492247987,
                                                        4990609742667096366,
                                                        4990609476136523663,
                                                        5451985767018841230,
                                                        5451985767078553638,
                                                        5270390122102920730,
                                                        4763688819142650938,
                                                        5056979692501246449,
                                                        5279569274741647114,
                                                        5031660827132289520,
                                                        4862889373349389098,
                                                        5299864070077160421,
                                                        4740222596778406913,
                                                        5320170488686569878,
                                                        5270367618320474818,
                                                        5320170488587895729,
                                                        5228888485293847415,
                                                        4778050469432720821,
                                                        5270392314970177842,
                                                        4849087862439244546,
                                                        5270392117430427860,
                                                        5270351184072717902,
                                                        5330263074228870897,
                                                        4763688829301614114,
                                                        4763684609695916489,
                                                        5270390232949727716
  ) ORDER BY "learnerUserName","learnerEmailId";

db使用的查询计划是这样的:

Sort  (cost=138.75..138.76 rows=4 width=1581) (actual time=0.169..0.171 rows=27 loops=1)
"  Sort Key: ""learnerUserName"", ""learnerEmailId"""
  Sort Method: quicksort  Memory: 73kB
  ->  Index Scan using "temp_learners_companyId_userId_idx" on temp_learners  (cost=0.55..138.71 rows=4 width=1581) (actual time=0.018..0.112 rows=27 loops=1)
"        Index Cond: ((""companyId"" = '909666665757230431'::bigint) AND (""userId"" = ANY ('{4990609084216745771,4990610022492247987,4990609742667096366,4990609476136523663,5451985767018841230,5451985767078553638,5270390122102920730,4763688819142650938,5056979692501246449,5279569274741647114,5031660827132289520,4862889373349389098,5299864070077160421,4740222596778406913,5320170488686569878,5270367618320474818,5320170488587895729,5228888485293847415,4778050469432720821,5270392314970177842,4849087862439244546,5270392117430427860,5270351184072717902,5330263074228870897,4763688829301614114,4763684609695916489,5270390232949727716}'::bigint[])))"
Planning time: 0.116 ms
Execution time: 0.191 ms

在此它不按索引排序。 但是当我运行这个查询时

SELECT *
FROM temp_learners
WHERE "companyId" = 909666665757230431
   ORDER BY "learnerUserName","learnerEmailId" limit 500;

此查询在排序时使用索引。

Limit  (cost=0.42..1360.05 rows=500 width=1581) (actual time=0.018..0.477 rows=500 loops=1)
  ->  Index Scan using temp_learners_company_name_email_index on temp_learners  (cost=0.42..332639.30 rows=122327 width=1581) (actual time=0.018..0.442 rows=500 loops=1)
        Index Cond: ("companyId" = '909666665757230431'::bigint)
Planning time: 0.093 ms
Execution time: 0.513 ms

我不明白的是为什么postgre在第一个查询中不使用索引?另外,我想弄清楚这个表 learner 的正常用例是与其他表连接。所以我写的第一个查询更类似于连接方程。例如,

SELECT *
FROM temp_learners AS l
INNER JOIN entity_learners_basic AS elb
ON l."companyId" = elb."companyId" AND l."userId" = elb."userId"
WHERE l."companyId" = 909666665757230431 AND elb."gameId" = 1050403501267716928
ORDER BY "learnerUserName", "learnerEmailId" limit 5000;

即使在更正索引之后,查询计划也不会为排序建立索引。

QUERY PLAN
Limit  (cost=3785.11..3785.22 rows=44 width=1767) (actual time=163.554..173.135 rows=5000 loops=1)
  ->  Sort  (cost=3785.11..3785.22 rows=44 width=1767) (actual time=163.553..172.791 rows=5000 loops=1)
"        Sort Key: l.""learnerUserName"", l.""learnerEmailId"""
        Sort Method: external merge  Disk: 35416kB
        ->  Nested Loop  (cost=1.12..3783.91 rows=44 width=1767) (actual time=0.019..63.743 rows=21195 loops=1)
              ->  Index Scan using primary_index__entity_learners_basic on entity_learners_basic elb  (cost=0.57..1109.79 rows=314 width=186) (actual time=0.010..6.221 rows=21195 loops=1)
                    Index Cond: (("companyId" = '909666665757230431'::bigint) AND ("gameId" = '1050403501267716928'::bigint))
              ->  Index Scan using "temp_learners_companyId_userId_idx" on temp_learners l  (cost=0.55..8.51 rows=1 width=1581) (actual time=0.002..0.002 rows=1 loops=21195)
                    Index Cond: (("companyId" = '909666665757230431'::bigint) AND ("userId" = elb."userId"))
Planning time: 0.309 ms
Execution time: 178.422 ms

Postgres 在连接和排序数据时不使用索引吗?

最佳答案

PostgreSQL 选择它认为会更快的计划。使用以正确顺序提供行的索引意味着使用选择性低得多的索引,因此它认为总体上不会更快。

如果您想强制 PostgreSQL 相信排序是世界上最糟糕的事情,您可以设置 enable_sort=off。如果在那之后它仍然排序,那么你知道 PostgreSQL 没有正确的索引来避免排序,而不是仅仅认为它们实际上不会更快。

关于postgresql - Postgres 不使用索引对数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63778881/

相关文章:

python - 用数组索引 torch 张量

mysql - 优化 Mysql UNION 查询以加快加载时间

postgresql - pg_basebackup 完成后 Postgresql 9.3 复制未启动

java - 如何使用 Java 和 PostgreSQL 执行\d表名

PostgreSQL:聚合成数组并连接

mysql - 按记录中的最后一个字母排序列

c# - 动态 Linq & ICompare

java - 通过SQLWarning访问原始查询

oracle - 在 Oracle 中使用索引反转字符串

zend-framework - Zend framework SQL select查询构造(ORDER BY)