如何在此PostgreSQL查询中阻止全表扫描? EXPLAIN ANALYZE EXPLAIN

如何解决如何在此PostgreSQL查询中阻止全表扫描? EXPLAIN ANALYZE EXPLAIN

此查询是使用PostgreSQL 11(最新)顶部的Django ORM构造的。此处的PostgreSQL在docker容器中运行。在生产中,我们使用Cloud SQL,查询时间虽然更快,但总体上却很慢。

更新:查询和EXPLAIN ANALYZE也可在此处使用:https://explain.depesz.com/s/yQE7

我已在所有ID列上建立索引,因此EXPLAINorders_query上显示了过长的全表扫描时,我感到很惊讶。长期运行的代码位于EXPLAIN日志的最顶部,但这是相关部分(最顶部的行):

GroupAggregate  (cost=999695.25..19231670646.03 rows=1897315 width=105)
  Group Key: users_profile.id,users_user.last_login
  ->  Sort  (cost=999695.25..1007470.17 rows=3109968 width=73)
        Sort Key: users_profile.id,users_user.last_login
        ->  Hash Right Join  (cost=738.71..387928.02 rows=3109968 width=73)
              Hash Cond: (orders_query.user_id = users_user.id)
              ->  Seq Scan on orders_query  (cost=0.00..345054.24 rows=2943324 width=8)
              ->  Hash  (cost=702.78..702.78 rows=2874 width=73)
                    ->  Hash Right Join  (cost=301.35..702.78 rows=2874 width=73)

问题:我是否可以制定另一个索引来避免此Seq Scan减慢SQL查询的速度?


 SELECT "users_profile"."id","users_profile"."created","users_profile"."modified","users_profile"."guid","users_profile"."user_id","users_profile"."charter_role_id","users_profile"."owner_id","users_profile"."volume_limit","users_profile"."vol_limit_ovrd","users_profile"."charter_vol_limit_contrib_ovrd","users_profile"."sponsored_ovrd","users_profile"."view_peers_ovrd","users_profile"."inherit_permits",Count(DISTINCT "orders_order"."id") AS "orders",(SELECT Count(DISTINCT U0."guid") AS "io"
        FROM   "orders_order" U0
               INNER JOIN "users_user" U1
                       ON ( U0."user_id" = U1."id" )
               INNER JOIN "users_profile" U2
                       ON ( U1."id" = U2."user_id" )
        WHERE  U2."owner_id" = "users_profile"."id"
        GROUP  BY U2."owner_id")           AS "indirect_orders",Count(DISTINCT "orders_query"."id") AS "searches",(SELECT Count(DISTINCT U0."guid") AS "isch"
        FROM   "orders_query" U0
               INNER JOIN "users_user" U1
                       ON ( U0."user_id" = U1."id" )
               INNER JOIN "users_profile" U2
                       ON ( U1."id" = U2."user_id" )
        WHERE  U2."owner_id" = "users_profile"."id"
        GROUP  BY U2."owner_id")           AS "indirect_searches",(SELECT Count(DISTINCT U0."guid") AS "sg"
        FROM   "users_profile" U0
        WHERE  U0."owner_id" = "users_profile"."id"
        GROUP  BY U0."owner_id")           AS "signups","users_user"."last_login"           AS "last_activity"
FROM   "users_profile"
       INNER JOIN "users_user"
               ON ( "users_profile"."user_id" = "users_user"."id" )
       LEFT OUTER JOIN "orders_order"
                    ON ( "users_user"."id" = "orders_order"."user_id" )
       LEFT OUTER JOIN "orders_query"
                    ON ( "users_user"."id" = "orders_query"."user_id" )
GROUP  BY "users_profile"."id","users_user"."last_login"  

EXPLAIN ANALYZE

EXPLAIN ANALYZE

GroupAggregate  (cost=999695.25..19231670646.03 rows=1897315 width=105) (actual time=682829.362..1112031.341 rows=1057 loops=1)
"  Group Key: users_profile.id,users_user.last_login"
  ->  Sort  (cost=999695.25..1007470.17 rows=3109968 width=73) (actual time=682711.948..768155.952 rows=222437142 loops=1)
"        Sort Key: users_profile.id,users_user.last_login"
        Sort Method: external merge  Disk: 18873640kB
        ->  Hash Right Join  (cost=738.71..387928.02 rows=3109968 width=73) (actual time=12.263..115509.811 rows=222437142 loops=1)
              Hash Cond: (orders_query.user_id = users_user.id)
              ->  Seq Scan on orders_query  (cost=0.00..345054.24 rows=2943324 width=8) (actual time=0.024..6992.146 rows=2945348 loops=1)
              ->  Hash  (cost=702.78..702.78 rows=2874 width=73) (actual time=12.203..12.203 rows=3456 loops=1)
                    Buckets: 4096  Batches: 1  Memory Usage: 409kB
                    ->  Hash Right Join  (cost=301.35..702.78 rows=2874 width=73) (actual time=3.807..10.194 rows=3456 loops=1)
                          Hash Cond: (orders_order.user_id = users_user.id)
                          ->  Seq Scan on orders_order  (cost=0.00..344.96 rows=7396 width=8) (actual time=0.009..1.771 rows=7396 loops=1)
                          ->  Hash  (cost=288.13..288.13 rows=1057 width=69) (actual time=3.762..3.762 rows=1057 loops=1)
                                Buckets: 2048  Batches: 1  Memory Usage: 128kB
                                ->  Hash Join  (cost=36.78..288.13 rows=1057 width=69) (actual time=1.137..3.169 rows=1057 loops=1)
                                      Hash Cond: (users_user.id = users_profile.user_id)
                                      ->  Seq Scan on users_user  (cost=0.00..244.20 rows=2720 width=12) (actual time=0.014..0.610 rows=2720 loops=1)
                                      ->  Hash  (cost=23.57..23.57 rows=1057 width=57) (actual time=1.022..1.174 rows=1057 loops=1)
                                            Buckets: 2048  Batches: 1  Memory Usage: 113kB
                                            ->  Seq Scan on users_profile  (cost=0.00..23.57 rows=1057 width=57) (actual time=0.008..0.504 rows=1057 loops=1)
  SubPlan 1
    ->  GroupAggregate  (cost=4.96..87.25 rows=14 width=12) (actual time=0.016..0.016 rows=0 loops=1057)
          Group Key: u2.owner_id
          ->  Nested Loop  (cost=4.96..86.89 rows=44 width=20) (actual time=0.008..0.013 rows=2 loops=1057)
                ->  Nested Loop  (cost=4.68..71.05 rows=16 width=12) (actual time=0.005..0.008 rows=1 loops=1057)
                      ->  Bitmap Heap Scan on users_profile u2  (cost=4.40..18.29 rows=16 width=8) (actual time=0.003..0.004 rows=1 loops=1057)
                            Recheck Cond: (owner_id = users_profile.id)
                            Heap Blocks: exact=300
                            ->  Bitmap Index Scan on users_profile_owner_id_a9029da4  (cost=0.00..4.40 rows=16 width=0) (actual time=0.002..0.002 rows=1 loops=1057)
                                  Index Cond: (owner_id = users_profile.id)
                      ->  Index Only Scan using users_user_pkey on users_user u1  (cost=0.28..3.30 rows=1 width=4) (actual time=0.002..0.002 rows=1 loops=1037)
                            Index Cond: (id = u2.user_id)
                            Heap Fetches: 0
                ->  Index Scan using orders_order_user_id_e9b59eb1 on orders_order u0  (cost=0.28..0.89 rows=10 width=20) (actual time=0.003..0.004 rows=2 loops=1037)
                      Index Cond: (user_id = u1.id)
  SubPlan 2
    ->  GroupAggregate  (cost=0.99..10029.94 rows=14 width=12) (actual time=5.109..5.109 rows=0 loops=1057)
          Group Key: u2_1.owner_id
          ->  Nested Loop  (cost=0.99..9943.23 rows=17314 width=20) (actual time=0.197..4.366 rows=858 loops=1057)
                ->  Nested Loop  (cost=0.56..110.20 rows=16 width=12) (actual time=0.182..0.196 rows=1 loops=1057)
                      ->  Index Scan using users_profile_user_id_key on users_profile u2_1  (cost=0.28..57.44 rows=16 width=8) (actual time=0.182..0.192 rows=1 loops=1057)
                            Filter: (owner_id = users_profile.id)
                            Rows Removed by Filter: 1056
                      ->  Index Only Scan using users_user_pkey on users_user u1_1  (cost=0.28..3.30 rows=1 width=4) (actual time=0.002..0.002 rows=1 loops=1037)
                            Index Cond: (id = u2_1.user_id)
                            Heap Fetches: 0
                ->  Index Scan using orders_query_user_id_a0b49874 on orders_query u0_1  (cost=0.43..564.34 rows=5022 width=20) (actual time=0.038..4.010 rows=874 loops=1037)
                      Index Cond: (user_id = u1_1.id)
  SubPlan 3
    ->  GroupAggregate  (cost=4.40..18.51 rows=14 width=12) (actual time=0.005..0.005 rows=0 loops=1057)
          Group Key: u0_2.owner_id
          ->  Bitmap Heap Scan on users_profile u0_2  (cost=4.40..18.29 rows=16 width=20) (actual time=0.002..0.002 rows=1 loops=1057)
                Recheck Cond: (owner_id = users_profile.id)
                Heap Blocks: exact=300
                ->  Bitmap Index Scan on users_profile_owner_id_a9029da4  (cost=0.00..4.40 rows=16 width=0) (actual time=0.001..0.001 rows=1 loops=1057)
                      Index Cond: (owner_id = users_profile.id)
Planning Time: 4.279 ms
Execution Time: 1112994.119 ms


EXPLAIN

GroupAggregate  (cost=999695.25..19231670646.03 rows=1897315 width=105)
  Group Key: users_profile.id,users_user.last_login
        ->  Hash Right Join  (cost=738.71..387928.02 rows=3109968 width=73)
              Hash Cond: (orders_query.user_id = users_user.id)
              ->  Seq Scan on orders_query  (cost=0.00..345054.24 rows=2943324 width=8)
              ->  Hash  (cost=702.78..702.78 rows=2874 width=73)
                    ->  Hash Right Join  (cost=301.35..702.78 rows=2874 width=73)
                          Hash Cond: (orders_order.user_id = users_user.id)
                          ->  Seq Scan on orders_order  (cost=0.00..344.96 rows=7396 width=8)
                          ->  Hash  (cost=288.13..288.13 rows=1057 width=69)
                                ->  Hash Join  (cost=36.78..288.13 rows=1057 width=69)
                                      Hash Cond: (users_user.id = users_profile.user_id)
                                      ->  Seq Scan on users_user  (cost=0.00..244.20 rows=2720 width=12)
                                      ->  Hash  (cost=23.57..23.57 rows=1057 width=57)
                                            ->  Seq Scan on users_profile  (cost=0.00..23.57 rows=1057 width=57)
  SubPlan 1
    ->  GroupAggregate  (cost=4.96..87.25 rows=14 width=12)
          Group Key: u2.owner_id
          ->  Nested Loop  (cost=4.96..86.89 rows=44 width=20)
                ->  Nested Loop  (cost=4.68..71.05 rows=16 width=12)
                      ->  Bitmap Heap Scan on users_profile u2  (cost=4.40..18.29 rows=16 width=8)
                            Recheck Cond: (owner_id = users_profile.id)
                            ->  Bitmap Index Scan on users_profile_owner_id_a9029da4  (cost=0.00..4.40 rows=16 width=0)
                                  Index Cond: (owner_id = users_profile.id)
                      ->  Index Only Scan using users_user_pkey on users_user u1  (cost=0.28..3.30 rows=1 width=4)
                            Index Cond: (id = u2.user_id)
                ->  Index Scan using orders_order_user_id_e9b59eb1 on orders_order u0  (cost=0.28..0.89 rows=10 width=20)
                      Index Cond: (user_id = u1.id)
  SubPlan 2
    ->  GroupAggregate  (cost=0.99..10029.94 rows=14 width=12)
          Group Key: u2_1.owner_id
          ->  Nested Loop  (cost=0.99..9943.23 rows=17314 width=20)
                ->  Nested Loop  (cost=0.56..110.20 rows=16 width=12)
                      ->  Index Scan using users_profile_user_id_key on users_profile u2_1  (cost=0.28..57.44 rows=16 width=8)
                            Filter: (owner_id = users_profile.id)
                      ->  Index Only Scan using users_user_pkey on users_user u1_1  (cost=0.28..3.30 rows=1 width=4)
                            Index Cond: (id = u2_1.user_id)
                ->  Index Scan using orders_query_user_id_a0b49874 on orders_query u0_1  (cost=0.43..564.34 rows=5022 width=20)
                      Index Cond: (user_id = u1_1.id)
  SubPlan 3
    ->  GroupAggregate  (cost=4.40..18.51 rows=14 width=12)
          Group Key: u0_2.owner_id
          ->  Bitmap Heap Scan on users_profile u0_2  (cost=4.40..18.29 rows=16 width=20)
                Recheck Cond: (owner_id = users_profile.id)
                ->  Bitmap Index Scan on users_profile_owner_id_a9029da4  (cost=0.00..4.40 rows=16 width=0)
                      Index Cond: (owner_id = users_profile.id)

解决方法

在我看来,您正在将用户的每个“ orders_order”与每个用户的“ orders_query”结合在一起,结果产生的行太多(2亿),因此需要按该组对行进行排序,这需要很长时间。

尝试以与“ indirect_orders”和“ indirect_searches”相同的方式为“ orders”和“ searches”编写子查询,以便可以删除这两个左外部联接。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res