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

Speeding 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 wildcards at the end of the query.

主站蜘蛛池模板: 吉林省| 芦溪县| 海阳市| 乐安县| 平舆县| 宿松县| 怀远县| 常山县| 河曲县| 霸州市| 克拉玛依市| 兴文县| 克东县| 临邑县| 松江区| 新建县| 鸡西市| 楚雄市| 永登县| 舞钢市| 民和| 绥棱县| 葵青区| 丰台区| 昌宁县| 新竹市| 孝昌县| 汉川市| 山东省| 宝兴县| 伊春市| 常熟市| 锡林浩特市| 陈巴尔虎旗| 吴江市| 西平县| 酒泉市| 策勒县| 黔南| 金寨县| 襄汾县|