sql—postgresql中任意(数组[…])与任意(值(),()…)之间的差异

nkoocmlb  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(295)

我正在尝试对id进行查询优化。不确定应该使用哪种方法。下面是使用 explain 从成本Angular 看也差不多。

1. explain (analyze, buffers) SELECT * FROM table1 WHERE id = ANY (ARRAY['00e289b0-1ac8-451f-957f-e00bc289148e'::uuid,...]);

QUERY PLAN:
    Index Scan using table1_pkey on table1  (cost=0.42..641.44 rows=76 width=835) (actual time=0.258..2.603 rows=76 loops=1)
    Index Cond: (id = ANY ('{00e289b0-1ac8-451f-957f-e00bc289148e,...}'::uuid[]))
    Buffers: shared hit=231 read=73
    Planning Time: 0.487 ms
    Execution Time: 2.715 ms)

2. explain (analyze, buffers) SELECT * FROM table1 WHERE id = ANY (VALUES ('00e289b0-1ac8-451f-957f-e00bc289148e'::uuid),...);

QUERY PLAN:
Nested Loop  (cost=1.56..644.10 rows=76 width=835) (actual time=0.058..0.297 rows=76 loops=1)
   Buffers: shared hit=304
   ->  HashAggregate  (cost=1.14..1.90 rows=76 width=16) (actual time=0.049..0.060 rows=76 loops=1)
         Group Key: "*VALUES*".column1
         ->  Values Scan on "*VALUES*"  (cost=0.00..0.95 rows=76 width=16) (actual time=0.006..0.022 rows=76 loops=1)
   ->  Index Scan using table1_pkey on table1  (cost=0.42..8.44 rows=1 width=835) (actual time=0.002..0.003 rows=1 loops=76)
         Index Cond: (id = "*VALUES*".column1)
         Buffers: shared hit=304
Planning Time: 0.437 ms
Execution Time: 0.389 ms

看起来像 VALUES () 执行一些散列和联接以提高性能,但不确定。
注意:在我的实际用例中, iduuid_generate_v4() e、 十。 d31cddc0-1771-4de8-ad41-e6c568b39a5d 但该列不能按此方式编制索引。另外,我还有一张table 5-10 million records . 哪种方式可以提高查询性能?

z31licg0

z31licg01#

两种选择似乎都是合理的。不过,我还是建议避免使用你筛选的专栏。相反,您应该将文本值强制转换为 uuid :

SELECT * 
FROM table1 
WHERE id = ANY (ARRAY['00e289b0-1ac8-451f-957f-e00bc289148e'::uuid, ...]);

这应该允许数据库利用列上的索引 id .

相关问题