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

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.

主站蜘蛛池模板: 左云县| 太谷县| 九龙城区| 射洪县| 岢岚县| 石河子市| 望都县| 阿拉尔市| 阳春市| 吴旗县| 霍州市| 吴川市| 崇义县| 元江| 潜江市| 南雄市| 南丰县| 辽源市| 郎溪县| 武威市| 平度市| 马尔康县| 儋州市| 大港区| 辽源市| 辽阳市| 河北省| 灵寿县| 沙湾县| 唐山市| 汉寿县| 赤峰市| 惠安县| 顺昌县| 图们市| 南陵县| 牟定县| 尚义县| 额济纳旗| 新昌县| 丰镇市|