Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 有没有办法加快这个重复检测查询的速度?_Postgresql_Query Optimization - Fatal编程技术网

Postgresql 有没有办法加快这个重复检测查询的速度?

Postgresql 有没有办法加快这个重复检测查询的速度?,postgresql,query-optimization,Postgresql,Query Optimization,这个查询在

这个查询在<10k记录上花费了很多分钟,这让我有点吃惊。有没有更有效的方法来运行计算基于列的重复数据的查询

UPDATE exportable_businesses e1 SET phone_dupe = 
 (CASE WHEN
   (SELECT COUNT(sidewalk_business_id) FROM exportable_businesses e2 WHERE query_id = #{id} AND e1.phone_number=e2.phone_number) > 1 
       THEN 'x' ELSE NULL END)

尝试先数一数电话号码,例如:

create temporary table phone_cnt as 
   select phone_number, count(*) as c from exportable_businesses 
   where query_id = #{id} 
   group by phone_number
然后使用预先计算的值设置
phone\u dupe
变量。Postgres应该能够使用连接进行更新,例如:

update exportable_businesses e1 
   set phone_dupe = (case when pc.c ...)
   from phone_cnt pc 
   where pc.phone_number = e1.phone_number
如果这仍然很慢,在执行更新查询之前,您需要在
phone\u cnt(phone\u number)
上创建一个显式索引。这样,整个计算将花费线性时间,而不是二次时间,就像您的子查询计数示例中的情况一样


您可以在查询后删除临时
phone\u cnt
表。

这是数量级更快的顺序。没有考虑临时表格的方法。感谢您可以使用物化视图,而不是临时表。