簡體   English   中英

pg_dump 和 pg_restore 之后 PostgreSQL db 本地與 Heroku 的不同查詢計划

[英]Different query plans for PostgreSQL db locally vs. on Heroku after pg_dump and pg_restore

我使用pg_dumppg_restore將一個大型數據庫(包括 3 個索引)從我的本地機器移動到 Heroku(按照這些說明)。 我已經確認:

  • Postgres 版本相同 (13.2)
  • 指標是一樣的
  • 行數相同

然而,某種類型的查詢的查詢計划仍然不同。 這是在同一數據庫的先前版本上工作 - 以相同的方式創建(只是行數更少)? 為什么會這樣,本地耗時不到 100 毫秒。 而在 Heroku 上幾乎是 90000 毫秒。

下面我將概述有關數據庫的更多詳細信息:

           Column            |       Type        | Collation | Nullable |                   Default
-----------------------------+-------------------+-----------+----------+---------------------------------------------
 id                          | integer           |           | not null | nextval('property_claims_id_seq'::regclass)
 PROPERTY_ID                 | integer           |           | not null |
 OWNER_NAME                  | character varying |           |          |

Indexes:
    "property_claim_pkey" PRIMARY KEY, btree (id)
    "gin_index_property_claims_on_OWNER_NAME" gin ("OWNER_NAME" gin_trgm_ops)
    "index_property_claims_on_OWNER_NAME" btree ("OWNER_NAME")
    "index_property_claims_on_PROPERTY_ID" btree ("PROPERTY_ID")

本地:

PropertyClaim.where('"OWNER_NAME" like ?', "QUERY STRING%").explain
  PropertyClaim Load (32.5ms)  SELECT "property_claims".* FROM "property_claims" WHERE ("OWNER_NAME" like 'QUERY STRING%')
=> EXPLAIN for: SELECT "property_claims".* FROM "property_claims" WHERE ("OWNER_NAME" like 'QUERY STRING%')
                                                    QUERY PLAN
------------------------------------------------------------------------------------------------------------------
 Index Scan using "index_property_claims_on_OWNER_NAME" on property_claims  (cost=0.56..8.58 rows=2607 width=278)
   Index Cond: ((("OWNER_NAME")::text >= 'QUERY STRING'::text) AND (("OWNER_NAME")::text < 'QUERY STRINO'::text))
   Filter: (("OWNER_NAME")::text ~~ 'QUERY STRING%'::text)
(3 rows)

遠程在 Heroku 上:

PropertyClaim.where('"OWNER_NAME" like ?', "QUERY STRING%").explain
=>
EXPLAIN for: SELECT "property_claims".* FROM "property_claims" WHERE ("OWNER_NAME" like 'QUERY STRING%')
                                                  QUERY PLAN
---------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on property_claims  (cost=364.95..194660.14 rows=130938 width=728)
   Recheck Cond: (("OWNER_NAME")::text ~~ 'QUERY STRING%'::text)
   ->  Bitmap Index Scan on "gin_index_property_claims_on_OWNER_NAME"  (cost=0.00..358.41 rows=130938 width=0)
         Index Cond: (("OWNER_NAME")::text ~~ 'QUERY STRING%'::text)
 JIT:
   Functions: 2
   Options: Inlining false, Optimization false, Expressions true, Deforming true
(7 rows)

如果模式不以通配符開頭(您在那里似乎很好)並且排序規則是 C,或者如果索引是使用特殊運算符之一創建的,則 btree 索引只能支持 LIKE 查詢,例如text_pattern_ops。 所以我猜你的本地數據庫的排序規則為 C,而 Heroku 沒有。

如果您ON property_claims ("OWNER_NAME" varchar_pattern_ops)上創建索引,那么您也應該在 heroku 上獲得更快的計划。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM