Django 如何防止此PostgreSQL查询中的完整表扫描?

Django 如何防止此PostgreSQL查询中的完整表扫描?,django,postgresql,postgresql-performance,Django,Postgresql,Postgresql Performance,此查询是在PostgreSQL 11(最新版本)之上使用Django ORM构造的。PostgreSQL在docker容器中运行。在生产中,我们使用云SQL,查询时间更快,但总体上相当慢 更新:查询和解释分析也可在此处获得: 我在所有ID列上都建立了索引,所以当EXPLAIN在orders\u query上显示一个长得令人望而却步的全表扫描时,我感到惊讶。长期运行的部分位于解释日志的最顶端,但以下是相关部分(最顶端的几行): 问题:我是否可以制作另一个索引来避免这一减慢SQL查询速度的Seq扫描

此查询是在PostgreSQL 11(最新版本)之上使用Django ORM构造的。PostgreSQL在docker容器中运行。在生产中,我们使用云SQL,查询时间更快,但总体上相当慢


更新:查询和
解释分析
也可在此处获得:

我在所有ID列上都建立了索引,所以当
EXPLAIN
orders\u query
上显示一个长得令人望而却步的全表扫描时,我感到惊讶。长期运行的部分位于
解释
日志的最顶端,但以下是相关部分(最顶端的几行):

问题:我是否可以制作另一个索引来避免这一减慢SQL查询速度的
Seq扫描


选择“用户配置文件”“id”,
“用户配置文件”“已创建”,
“用户配置文件”“已修改”,
“用户配置文件”“guid”,
“用户配置文件”“用户id”,
“用户配置文件”“特许人角色\u id”,
“用户配置文件”“所有者id”,
“用户配置文件”“容量限制”,
“用户档案”“音量限制”,
“用户档案”;“章程、数量、限制、责任”,
“用户档案”“赞助商档案”,
“用户配置文件”“查看对等点”,
“用户\u配置文件”“继承\u许可证”,
将(不同的“订单”\u订单“.id”)计数为“订单”,
(选择计数(不同的U0.“guid”)作为“io”
从“订单”U0
内部联接“用户\用户”U1
ON(U0.“用户id”=U1.“id”)
内部连接“用户配置文件”U2
ON(U1.“id”=U2.“用户id”)
其中U2.“所有者id”=“用户配置文件”。“id”
按U2分组。“所有者id”)作为“间接订单”,
将(不同的“订单查询”、“id”)计数为“搜索”,
(选择计数(不同的U0.“guid”)作为“isch”
从“订单查询”U0
内部联接“用户\用户”U1
ON(U0.“用户id”=U1.“id”)
内部连接“用户配置文件”U2
ON(U1.“id”=U2.“用户id”)
其中U2.“所有者id”=“用户配置文件”。“id”
按U2分组。“所有者id”)作为“间接搜索”,
(选择计数(不同的U0.“guid”)作为“sg”
从“用户配置文件”U0
其中U0.“所有者\u id”=“用户\u配置文件”。“id”
按U0分组。“所有者id”)作为“注册”,
“用户”“上次登录”“作为“上次活动”
从“用户档案”
内部联接“用户\用户”
在(“用户档案”“用户id”“用户用户用户”“id”)
左外连接“订单\订单”
在(“用户”;“id”=“订单”;“用户id”)
左外联接“订单\查询”
关于(“用户”;“id”=“订单”;“用户id”)
按“用户配置文件”“id”“分组”,
“用户”“上次登录”

解释分析

解释

在我看来,您将用户的每个“订单”与他们的每个“订单查询”连接起来,结果中产生的行太多(2亿),需要按分组进行排序,这需要很长时间


试着用与“间接指令”和“间接搜索”相同的方式为“订单”和“搜索”写子查询,以便删除这两个左外部连接。请考虑。这将在代码< >解释< <代码> >代码>解释分析< /代码>添加。addedMy查询现在大约需要6秒(快170倍)

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)
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

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)
                          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)