我對 sql/postgres 索引完全沒有經驗,所以我什至不確定如何正確地提出我的問題。但是這里...
在我們的 JpaRepositories 之一中,我們有一個如下所示的查詢:
@Query(nativeQuery = true, value =
"SELECT * FROM instrument i "
"left join instrument_profile ip ON ip.instrument_id = i.id "
"WHERE (ip.profile IS NULL or upper(ip.profile) = :assetMgr)"
" and i.transaction_type = :transactionType "
" and i.customer_id = :customerId "
" and ( i.bloomberg_id ilike %:key% OR "
" i.instrument_short ilike %:key% or "
" i.instrument_name ilike %:key% OR "
" i.cusip ilike %:key% OR "
" i.sedol ilike %:key%) "
"limit :limit")
查詢可以找到,但我們現在正在尋找優化整體性能的方法,表索引就是其中之一。問題是.. 我不知道如何為這種查詢索引表。部分原因是它包含一個連接表,而且我們正在多個欄位中搜索“搜索鍵”值。這個特定的表應該包含很多記錄,所以如果我們不能創建一個支持上述的索引,我們可能會被迫更改整個查詢。
非常感謝有關如何處理此問題的任何建議或指示。
/凱特
uj5u.com熱心網友回復:
Join 列應該被索引以獲得更好的性能。您可以嘗試在instrument_profile表和instrument table id列中的instrument_id上創建索引,如下所示
CREATE INDEX instrument_profile_instrument_id_idx ON instrument_profile (instrument_id);
CREATE INDEX instrument_instrument_id_idx ON instrument (id);
uj5u.com熱心網友回復:
我正在考慮將搜索欄位連接到一個單獨的列中,我可以用 pg_trgm 索引并在查詢中使用。在這一點上似乎是一個出路
您可以這樣做,但您也可以在 5 個單獨的列上創建 pg_trgm 索引。我傾向于認為這比連接欄位上的索引更好。您可以以更自然的方式撰寫查詢,如果您有時希望省略可以執行的列之一。它還可以節省復制表中資料的空間。
create index on instrument using gin (bloomberg_id gin_trgm_ops, instrument_short gin_trgm_ops, instrument_name gin_trgm_ops, cusip gin_trgm_ops, sedol gin_trgm_ops);
我還將在其他 2 列上創建常規(btree)索引:
create index on instrument (transaction_type , customer_id);
根據行估計,它可能決定將這兩個索引與 BitmapAnd 組合,或者它可能決定只使用一個或另一個,然后根據它選擇不使用索引的任何標準進行過濾。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/383310.html
標籤:sql PostgreSQL的 jpa 索引 弹簧数据-jpa
