如何做“any(::text[]) ilike::text”
这是表结构,
table1
pk int, email character varying(100)[]
data
1, {'[email protected]', '[email protected]', '[email protected]'}
我试图实现的是从记录中查找任何“gmail”
query
select * from table1 where any(email) ilike '%gmail%';
,但 any() 只能位于左侧,unnest() 可能会降低性能。有人知道吗?
编辑
实际上,当我第一次发帖时,我有点困惑。我尝试通过any(array[])来实现。
这是我的实际结构
pk int,
code1 character varying(100),
code2 character varying(100),
code3 character varying(100), ...
,我的第一个方法是
select * from tabl1 where code1 ilike '%code%' or code2 ilike '%code%' or...
然后我尝试
select * from table1 where any(array[code1, code2, ...]) ilike '%code%'
哪个不起作用。
here is table structure
table1
pk int, email character varying(100)[]
data
1, {'[email protected]', '[email protected]', '[email protected]'}
what i try to achieve is find any 'gmail' from record
query
select * from table1 where any(email) ilike '%gmail%';
but any() can only be in left-side and unnest() might slow down performance. anyone have any idea?
edit
actually i kinda confuse a bit when i first post. i try to achieve through any(array[]).
this is my actual structure
pk int,
code1 character varying(100),
code2 character varying(100),
code3 character varying(100), ...
my first approch is
select * from tabl1 where code1 ilike '%code%' or code2 ilike '%code%' or...
then i try
select * from table1 where any(array[code1, code2, ...]) ilike '%code%'
which is not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个“向后”实现
ILIKE
的运算符,例如:(请注意,
ILIKE
内部对应于运算符~~*
。选择您自己的名称则相反。)然后你可以运行
Create an operator that implements
ILIKE
"backwards", e.g.:(Note that
ILIKE
internally corresponds to the operator~~*
. Pick your own name for the reverse.)Then you can run
将电子邮件地址存储在规范化的表结构中。然后,您可以避免
unnest
的费用,拥有“正确的”数据库设计,并充分利用索引。如果您希望进行全文样式查询,则应该将电子邮件地址存储在表中,然后使用 tsvector 数据类型,以便您可以执行全文查询并使用索引。ILIKE '%whatever%'
将导致全表扫描,因为规划器无法利用任何查询。根据您当前的设计和足够数量的记录,unnest
将是您最不用担心的问题。更新 即使更新了问题,使用标准化代码表也会给您带来最少的麻烦并获得最佳的扫描结果。任何时候您发现自己创建了编号列,这都很好地表明您可能想要标准化。话虽这么说,您可以创建一个计算文本列来用作搜索词列。在您的情况下,您可以创建一个
search_words
列,该列在插入和更新时由触发器填充。然后,您可以创建一个tsvector
在search_words
上构建全文查询Store email addresses in a normalized table structure. Then you can avoid the expense of
unnest
, have "proper" database design, and take full advantage of indexing. If you're looking to do full text style queries, you should be storing your email addresses in a table and then using a tsvector datatype so you can perform full text queries AND use indexes.ILIKE '%whatever%'
is going to result in a full table scan since the planner can't take advantage of any query. With your current design and a sufficient number of records,unnest
will be the least of your worries.Update Even with the updates to the question, using a normalized codes table will cause you the least amount of headache and result in optimal scans. Anytime that you find yourself creating numbered columns, it's a good indication that you might want to normalize. That being said, you can create a computed text column to use as a search words column. In your case you could create a
search_words
column that is populated on insert and update by a trigger. Then you can create atsvector
to build full text queries on thesearch_words