ruby-on-rails - 我想搜索2个字符,有什么解决办法吗?(三元组索引仅适用于至少 3 个字符)
问题描述
我在名字和姓氏列上有一个三元索引,我想搜索 2 个字符。我尝试在打开或关闭顺序扫描的情况下执行查询,并测量每种情况下需要多长时间,但两者都需要很多时间。有没有让我的 2 个字符搜索工作更快的解决方案?
在架构中我有:
t.index "(((((first_name)::text || ' '::text) || (last_name)::text)) gin_trgm_ops",名称:"index_users_full_name",使用::gin
解决方案
我不认为有任何内置的东西可以做到这一点。您可以创建一个函数,将文本转换为二元数组(决定如何处理空格、标点符号、非 ASCII 等),然后对该数组进行索引并使用&&
运算符进行查询。但是,除非您的两个字符类似于“qz”,否则您可能无法获得足够的选择性来使索引变得有价值。
create function bigram(text) returns text[] language sql as $$
select array_agg(substring($1,x,2)) from generate_series(1,length($1)-1) f(x);
$$ immutable;
create index on foobar using gin (bigram(t));
select * from foobar where bigram(t) && bigram('aa');
推荐阅读
- python-3.x - 如何减少 Python 的 itertools.product 的内存消耗和处理时间?
- android - 收到通知时 onSaveInstanceState 后无法执行此操作-Android
- javascript - 如何根据表中的另一个选择选项更改选项?
- node.js - Express 服务器在通过 Postman 测试时没有响应并崩溃
- javascript - Javascript,异步问题所需的解决方案
- shell - TeamCity 系统找不到指定的文件
- asp.net - 使用 Swashbuckle 自定义生成模型文档
- wordpress - Mamp PRO 在 Windows 上没有所有者/权限问题(运行 Wordpress)
- c - 使用指针从 C 中 main 中的另一个函数更改字符串值
- python - 每 N 毫秒重复 N 时间的函数