小编Yar*_*rin的帖子

为什么 LIMIT 会扼杀这个 Postgres 查询的性能?

我有一个 Postgres 物化视图:

Column        |       Type        | Modifiers 
---------------------+-------------------+-----------
document_id         | character varying | 
recorded_date       | date              | 
parcels             | jsonb             | 
Indexes:
"index_my_view_on_document_id" btree (document_id)
"index_my_view_on_recorded_date" btree (recorded_date)
"index_my_view_on_parcels" gin (parcels)
Run Code Online (Sandbox Code Playgroud)

我正在尝试执行一个分页查询,该查询在parcelsjsonb 数组字段上进行过滤,但是每当我添加 LIMIT 时,我的性能就会下降:

无限制:

EXPLAIN ANALYZE SELECT document_id FROM my_view WHERE (parcels @> '[3022890014]') ORDER BY recorded_date DESC;
                                                                       QUERY PLAN                                                                       
--------------------------------------------------------------------------------------------------------------------------------------------------------
 Sort  (cost=24178.50..24194.79 rows=6518 width=21) (actual time=11.272..11.275 rows=22 loops=1)
   Sort Key: recorded_date DESC
   Sort Method: quicksort  Memory: 26kB
   ->  Bitmap Heap Scan on my_view  (cost=78.51..23765.58 …
Run Code Online (Sandbox Code Playgroud)

postgresql performance query-performance

5
推荐指数
1
解决办法
5741
查看次数