【发布时间】:2025-11-23 03:15:01
【问题描述】:
我在 Google Cloud SQL 上有一个数据库。它包含一个简单的表格,如下所示:
url_id user_id
url_id 是一个包含整数的字符串,user_id 是一个 14 字符的字符串。 我在 url_id 上有一个索引:
CREATE INDEX index_test ON table1 (url_id);
我要运行的请求是获取具有不在给定 id 列表中的 url_id 的不同 user_id 的数量。我是这样做的:
SET work_mem='4GB';
select count(*) from (select distinct afficheW from table1 where url_id != '1880' and url_id != '2022' and url_id != '1963' and url_id != '11' and url_id != '32893' and url_id != '19' ) t ;
结果:
count
---------
1242298
(1 row)
Time: 2118,917 ms
该表包含 180 万行。 有没有办法让这种类型的请求更快?
【问题讨论】:
-
请显示您当前查询的
explain (analyze, buffers)。
标签: sql postgresql count