當時數量量比較龐大的時候,做模糊查詢效率很慢,為了優化查詢效率,嘗試如下方法做效率對比
一、對比情況說明:
- 1、數據量100w條數據
- 2、執行sql
二、對比結果
explain analyze SELECT
c_patent,
c_applyissno,
d_applyissdate,
d_applydate,
c_patenttype_dimn,
c_newlawstatus,
c_abstract
FROM
public.t_knowl_patent_zlxx_temp
WHERE
c_applicant LIKE '%本溪滿族自治縣連山關鎮安平安養殖場%';
1、未建索時執行計划:
"Gather (cost=1000.00..83803.53 rows=92 width=1278) (actual time=217.264..217.264 rows=0 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Parallel Seq Scan on t_knowl_patent_zlxx (cost=0.00..82794.33 rows=38 width=1278) (actual time=212.355..212.355 rows=0 loops=3)
Filter: ((c_applicant)::text ~~ '%本溪滿族自治縣連山關鎮安平安養殖場%'::text)
Rows Removed by Filter: 333333
Planning time: 0.272 ms
Execution time: 228.116 ms"
2、btree索引
建索引語句
CREATE INDEX idx_public_t_knowl_patent_zlxx_applicant ON public.t_knowl_patent_zlxx(c_applicant varchar_pattern_ops);
執行計划
"Gather (cost=1000.00..83803.53 rows=92 width=1278) (actual time=208.253..208.253 rows=0 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Parallel Seq Scan on t_knowl_patent_zlxx (cost=0.00..82794.33 rows=38 width=1278) (actual time=203.573..203.573 rows=0 loops=3)
Filter: ((c_applicant)::text ~~ '%本溪滿族自治縣連山關鎮安平安養殖場%'::text)
Rows Removed by Filter: 333333
Planning time: 0.116 ms
Execution time: 218.189 ms"
但是如果將查詢sql稍微改動一下,把like查詢中的前置%去掉是這樣的
Index Scan using idx_public_t_knowl_patent_zlxx_applicant on t_knowl_patent_zlxx_temp (cost=0.55..8.57 rows=92 width=1278) (actual time=0.292..0.292 rows=0 loops=1)
Index Cond: (((c_applicant)::text ~>=~ '本溪滿族自治縣連山關鎮安平安養殖場'::text) AND ((c_applicant)::text ~<~ '本溪滿族自治縣連山關鎮安平安養殖圻'::text))
Filter: ((c_applicant)::text ~~ '本溪滿族自治縣連山關鎮安平安養殖場%'::text)
Planning time: 0.710 ms
Execution time: 0.378 ms
3、gin索引
創建索引語句(postgresql要求在9.6版本及以上)
create extension pg_trgm;
CREATE INDEX idx_public_t_knowl_patent_zlxx_applicant ON public.t_knowl_patent_zlxx USING gin (c_applicant gin_trgm_ops);
執行計划
Bitmap Heap Scan on t_knowl_patent_zlxx (cost=244.71..600.42 rows=91 width=1268) (actual time=0.649..0.649 rows=0 loops=1)
Recheck Cond: ((c_applicant)::text ~~ '%本溪滿族自治縣連山關鎮安平安養殖場%'::text)
-> Bitmap Index Scan on idx_public_t_knowl_patent_zlxx_applicant (cost=0.00..244.69 rows=91 width=0) (actual time=0.647..0.647 rows=0 loops=1)
Index Cond: ((c_applicant)::text ~~ '%本溪滿族自治縣連山關鎮安平安養殖場%'::text)
Planning time: 0.673 ms
Execution time: 0.740 ms
三、結論
btree索引可以讓后置% "
abc%"的模糊匹配走索引,gin + gp_trgm可以讓前后置% "%abc%" 走索引。但是gin 索引也有弊端,以下情況可能導致無法命中:
- 搜索字段少於3個字符時,不會命中索引,這是gin自身機制導致。
- 當搜索字段過長時,比如email檢索,可能也不會命中索引,造成原因暫時未知。
