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

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 this, 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 this 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 that we deployed in the previous section is also suitable for speeding 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.

主站蜘蛛池模板: 汾西县| 台南县| 内乡县| 台东市| 隆子县| 鲁山县| 理塘县| 平凉市| 凌源市| 阳城县| 黑龙江省| 安顺市| 武安市| 榆林市| 栾川县| 明星| 新闻| 湘潭县| 永安市| 尼勒克县| 黄梅县| 宝兴县| 额尔古纳市| 乌拉特后旗| 麻城市| 忻州市| 大丰市| 团风县| 临邑县| 廉江市| 蓝田县| 都兰县| 昌江| 勐海县| 工布江达县| 乌兰浩特市| 朔州市| 峡江县| 佳木斯市| 剑阁县| 山阴县|