官术网_书友最值得收藏!

Speed up LIKE queries

LIKE queries definitely cause some of the worst performance problems faced by people around the globe these days. In most database systems, LIKE is pretty slow and requires a sequential scan. In addition to that, end users quickly figure out that a fuzzy search will in many cases return better results than precise queries. A single type of LIKE query on a large table can, therefore, often cripple the performance of an entire database server if it is called often enough.

Fortunately, PostgreSQL offers a solution to the problem and the solution happens to be installed already:

test=# explain SELECT * FROM t_location WHERE name LIKE '%neusi%'; 
QUERY PLAN
-----------------------------------------------------------------
Bitmap Heap Scan on t_location
(cost=4.33..19.05 rows=24 width=13)
Recheck Cond: (name ~~ '%neusi%'::text)
-> Bitmap Index Scan on idx_trgm (cost=0.00..4.32 rows=24 width=0)
Index Cond: (name ~~ '%neusi%'::text)
(4 rows)

The trigram index deployed in the previous section is also suitable to speed up LIKE. Note that the % symbols can be used at any point in the search string. This is a major advantage over standard B-trees, which just happen to speed up wild cards at the end of the query.

主站蜘蛛池模板: 和静县| 江门市| 乌鲁木齐市| 五指山市| 涿鹿县| 汤阴县| 简阳市| 台东市| 北票市| 新乐市| 南京市| 兴城市| 苏尼特右旗| 宁陵县| 永仁县| 德钦县| 泽普县| 闸北区| 德州市| 平阴县| 仪征市| 曲麻莱县| 铜鼓县| 弋阳县| 开江县| 灵山县| 靖江市| 博客| 新龙县| 新巴尔虎右旗| 通榆县| 彭阳县| 紫金县| 滁州市| 东港市| 贵港市| 行唐县| 上饶市| 姚安县| 宜章县| 年辖:市辖区|