我需要执行Contains
列中的操作。对于包含操作,我们需要在单词前后使用通配符。
例如:个性化
查询 -> like '%sonal%'
因为这种类型的查询不能使用索引。有什么办法可以提高搜索速度。
注意:我使用MySql(InnoDB)和PSQL
PostgreSQL有解决方案——三元组索引。这里有一个article https://about.gitlab.com/2016/03/18/fast-search-using-postgresql-trigram-indexes/ or 文档 https://www.postgresql.org/docs/current/static/pgtrgm.html
postgres=# create extension pg_trgm ;
CREATE EXTENSION
postgres=# create index on obce using gin (nazev gin_trgm_ops);
CREATE INDEX
postgres=# explain select * from obce where nazev like '%Bene%';
┌──────────────────────────────────────────────────────────────────────────────┐
│ QUERY PLAN │
╞══════════════════════════════════════════════════════════════════════════════╡
│ Bitmap Heap Scan on obce (cost=20.00..24.02 rows=1 width=41) │
│ Recheck Cond: ((nazev)::text ~~ '%Bene%'::text) │
│ -> Bitmap Index Scan on obce_nazev_idx (cost=0.00..20.00 rows=1 width=0) │
│ Index Cond: ((nazev)::text ~~ '%Bene%'::text) │
└──────────────────────────────────────────────────────────────────────────────┘
(4 rows)
它也适用于正则表达式。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)