带有某些参数的Postgresql错误查询计划

带有某些参数的Postgresql错误查询计划,postgresql,performance,sql-execution-plan,query-analyzer,Postgresql,Performance,Sql Execution Plan,Query Analyzer,我在一个大表上有一个查询,根据参数的不同,查询时间从2秒到3分钟不等。有没有办法告诉Postgres(9.6)它的查询计划有时非常糟糕 解释 我有两张桌子: Table "public.ItemUpdates" Column | Type | Modifiers ------------+-----------------------------+---------- item_id | integer

我在一个大表上有一个查询,根据参数的不同,查询时间从2秒到3分钟不等。有没有办法告诉Postgres(9.6)它的查询计划有时非常糟糕

解释

我有两张桌子:

      Table "public.ItemUpdates"
  Column    |            Type             | Modifiers 
------------+-----------------------------+----------
item_id     | integer                     | not null
type        | integer                     | not null
next_update | timestamp without time zone | not null
Indexes:
    "item_updates_item_id_unique" UNIQUE CONSTRAINT, btree (item_id)
    "ItemUpdates_type_next_update_idx" btree (type, next_update)
Foreign-key constraints:
    "fk_itemupdates_item_id" FOREIGN KEY (item_id) REFERENCES "ItemInfo"(id)

        Table "public.ItemInfo"
     Column     |      Type       |   Modifiers                      
----------------+-----------------+-----------------
id              | integer         | not null default nextval('items_id_seq'::regclass)
status          | smallint        | not null
...
and 10 more columns with text and ts_vectors (74 GB without indexes)
共有4个项目更新。类型为:1、2、3和4,重新分区为:

从“项目更新”分组中按类型选择类型、计数(*)

 type |  count
------+----------
    1 |  1859706
    2 |  5061595
    3 | 61973086
    4 |  2018415
我使用的查询是:

SELECT iu.item_id
FROM "ItemUpdates" iu
INNER JOIN "ItemInfo" i ON i.id = iu.item_id
WHERE iu.type = $1
AND iu.next_update < now()
AND i.status = 1
LIMIT 200000
选择iu.item\u id
从“项目更新”iu
i.id=iu.item\u id上的内部联接“ItemInfo”i
其中iu.type=$1
和iu.next_update
使用类型134,查询速度足够快(不到3秒)。 问题是当我查询类型2:大约4分钟时

查询计划根据类型不同而不同

对于1和4(2000000项):

查询计划
限制(成本=28410.75..1679381.70行=200000宽=4)(实际时间=88.930..2696.893行=35圈=1)
->嵌套循环(成本=28410.75..8170462.90行=986335宽度=4)(实际时间=88.929..2696.860行=35个循环=1)
->“ItemUpdates”iu上的位图堆扫描(成本=28410.18..459949.42行=993328宽度=4)(实际时间=79.411..2266.702行=78312循环=1)
重新检查条件:((type=1)和(next_update“ItemUpdates\u type\u next\u update\u idx”上的位图索引扫描(成本=0.00..28161.85行=993328宽度=0)(实际时间=73.388..73.388行=262838循环=1)
索引条件:((type=1)和(next_update使用“ItemInfo”i上的“Items_pkey”进行索引扫描(成本=0.57..7.75行=1宽度=4)(实际时间=0.005..0.005行=0循环=78312)
索引条件:(id=iu.item\U id)
过滤器:(状态=1)
被筛选器删除的行:1
计划时间:0.316毫秒
执行时间:2696.948毫秒
对于3(62000000项):

查询计划
限制(成本=0.57..1481409.86行=200000宽=4)(实际时间=0.202..27676.311行=200000圈=1)
->嵌套循环(成本=0.57..244077225.62行=32952031宽度=4)(实际时间=0.201..27600.918行=200000个循环=1)
->“项目更新”iu上的顺序扫描(成本=0.00..1729530.37行=33185659宽度=4)(实际时间=0.015..237.416行=200123循环=1)
筛选器:((type=3)和(next_update使用“ItemInfo”i上的“Items_pkey”进行索引扫描(成本=0.57..7.29行=1宽度=4)(实际时间=0.135..0.136行=1循环=200123)
索引条件:(id=iu.item\U id)
过滤器:(状态=1)
已被筛选器删除的行:0
计划时间:0.352毫秒
执行时间:27716.495毫秒
它更长,但对于我们的用例来说仍然可以

类型为2(5000000项):

查询计划
限制(成本=583733.44..1446607.01行=200000宽=4)(实际时间=47634.435..130378.407行=461圈=1)
->散列联接(成本=583733.44..12198399.97行=2692090宽度=4)(实际时间=47634.434..130378.148行=461循环=1)
散列条件:(i.id=iu.item\u id)
->“ItemInfo”i上的顺序扫描(成本=0.00..10591334.10行=70216021宽度=4)(实际时间=0.007..99254.593行=70607997循环=1)
过滤器:(状态=1)
被筛选器删除的行:497449
->散列(成本=539252.73..539252.73行=2711177宽度=4)(实际时间=3723.732..3723.732行=262938循环=1)
存储桶:131072批:64内存使用率:1170kB
->“ItemUpdates”iu上的位图堆扫描(成本=77558.13..539252.73行=2711177宽度=4)(实际时间=147.440..3642.451行=262938循环=1)
重新检查条件:((type=2)和(next_update“ItemUpdates\u type\u next\u update\u idx”上的位图索引扫描(成本=0.00..76880.34行=2711177宽度=0)(实际时间=140.141..140.141行=358860循环=1)
索引条件:((type=2)和(next_update
74 GB表上的Seq扫描非常长,需要耗费大量资源

表格是自动抽真空的,真空分析不会改变任何内容

恢复:

  • 要分析的2M行=>OK
  • 要分析的行数=>不正常
  • 要分析的620万行=>OK

有没有办法拉皮条、强迫或破解查询计划?

item\u id
既是itemUpdates的PK,也是对items的FK。这意味着每个项目的ItemUpdate中只能有一行。你真的想要吗?是的。下一次更新是频繁更新的,在大行上的大量更新意味着许多问题(例如,在桌子和相关的烤面包桌上10小时的真空)。
item\u id
既是itemUpdates的主键,也是对items的FK。这意味着每个项目的ItemUpdate中只能有一行。你真的想要吗?是的。下一次的_更新是频繁更新的,在大行上的大量更新意味着许多问题(例如,在桌子上和相关的烤面包桌上保持10小时的真空)。
QUERY PLAN                                                                               
 Limit  (cost=28410.75..1679381.70 rows=200000 width=4) (actual time=88.930..2696.893 rows=35 loops=1)
   ->  Nested Loop  (cost=28410.75..8170462.90 rows=986335 width=4) (actual time=88.929..2696.860 rows=35 loops=1)
         ->  Bitmap Heap Scan on "ItemUpdates" iu  (cost=28410.18..459949.42 rows=993328 width=4) (actual time=79.411..2266.702 rows=78312 loops=1)
               Recheck Cond: ((type = 1) AND (next_update < now()))
               Rows Removed by Index Recheck: 14458426
               Heap Blocks: exact=30028 lossy=80441
               ->  Bitmap Index Scan on "ItemUpdates_type_next_update_idx"  (cost=0.00..28161.85 rows=993328 width=0) (actual time=73.388..73.388 rows=262838 loops=1)
                     Index Cond: ((type = 1) AND (next_update < now()))
         ->  Index Scan using "Items_pkey" on "ItemInfo" i  (cost=0.57..7.75 rows=1 width=4) (actual time=0.005..0.005 rows=0 loops=78312)
               Index Cond: (id = iu.item_id)
               Filter: (status = 1)
               Rows Removed by Filter: 1
 Planning time: 0.316 ms
 Execution time: 2696.948 ms
 QUERY PLAN                                                                 
 Limit  (cost=0.57..1481409.86 rows=200000 width=4) (actual time=0.202..27676.311 rows=200000 loops=1)
   ->  Nested Loop  (cost=0.57..244077225.62 rows=32952031 width=4) (actual time=0.201..27600.918 rows=200000 loops=1)
         ->  Seq Scan on "ItemUpdates" iu  (cost=0.00..1729530.37 rows=33185659 width=4) (actual time=0.015..237.416 rows=200123 loops=1)
               Filter: ((type = 3) AND (next_update < now()))
               Rows Removed by Filter: 205436
         ->  Index Scan using "Items_pkey" on "ItemInfo" i  (cost=0.57..7.29 rows=1 width=4) (actual time=0.135..0.136 rows=1 loops=200123)
               Index Cond: (id = iu.item_id)
               Filter: (status = 1)
               Rows Removed by Filter: 0
 Planning time: 0.352 ms
 Execution time: 27716.495 ms
QUERY PLAN                                                                                   
Limit  (cost=583733.44..1446607.01 rows=200000 width=4) (actual time=47634.435..130378.407 rows=461 loops=1)
  ->  Hash Join  (cost=583733.44..12198399.97 rows=2692090 width=4) (actual time=47634.434..130378.148 rows=461 loops=1)
        Hash Cond: (i.id = iu.item_id)
        ->  Seq Scan on "ItemInfo" i  (cost=0.00..10591334.10 rows=70216021 width=4) (actual time=0.007..99254.593 rows=70607997 loops=1)
              Filter: (status = 1)
              Rows Removed by Filter: 497449
        ->  Hash  (cost=539252.73..539252.73 rows=2711177 width=4) (actual time=3723.732..3723.732 rows=262938 loops=1)
              Buckets: 131072  Batches: 64  Memory Usage: 1170kB
              ->  Bitmap Heap Scan on "ItemUpdates" iu  (cost=77558.13..539252.73 rows=2711177 width=4) (actual time=147.440..3642.451 rows=262938 loops=1)
                    Recheck Cond: ((type = 2) AND (next_update < now()))
                    Rows Removed by Index Recheck: 9698516
                    Heap Blocks: exact=36509 lossy=54070
                    ->  Bitmap Index Scan on "ItemUpdates_type_next_update_idx"  (cost=0.00..76880.34 rows=2711177 width=0) (actual time=140.141..140.141 rows=358860 loops=1)
                          Index Cond: ((type = 2) AND (next_update < now()))
Planning time: 0.264 ms
Execution time: 130378.583 ms