如何做“any(::text[]) ilike::text”

发布于 2024-10-23 22:17:37 字数 1165 浏览 1 评论 0原文

这是表结构,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-10-30 22:17:37

创建一个“向后”实现 ILIKE 的运算符,例如:(

CREATE FUNCTION backward_texticlike(text, text) RETURNS booleans
    STRICT IMMUTABLE LANGUAGE SQL
    AS $ SELECT texticlike($2, $1) $;

CREATE OPERATOR !!!~~* (
    PROCEDURE = backward_texticlike,
    LEFTARG = text,
    RIGHTARG = text,
    COMMUTATOR = ~~*
);

请注意,ILIKE 内部对应于运算符 ~~*。选择您自己的名称则相反。)

然后你可以运行

SELECT * FROM table1 WHERE '%code%' !!!~~* ANY(ARRAY[code1, code2, ...]);

Create an operator that implements ILIKE "backwards", e.g.:

CREATE FUNCTION backward_texticlike(text, text) RETURNS booleans
    STRICT IMMUTABLE LANGUAGE SQL
    AS $ SELECT texticlike($2, $1) $;

CREATE OPERATOR !!!~~* (
    PROCEDURE = backward_texticlike,
    LEFTARG = text,
    RIGHTARG = text,
    COMMUTATOR = ~~*
);

(Note that ILIKE internally corresponds to the operator ~~*. Pick your own name for the reverse.)

Then you can run

SELECT * FROM table1 WHERE '%code%' !!!~~* ANY(ARRAY[code1, code2, ...]);
善良天后 2024-10-30 22:17:37

将电子邮件地址存储在规范化的表结构中。然后,您可以避免unnest的费用,拥有“正确的”数据库设计,并充分利用索引。如果您希望进行全文样式查询,则应该将电子邮件地址存储在表中,然后使用 tsvector 数据类型,以便您可以执行全文查询并使用索引。 ILIKE '%whatever%' 将导致全表扫描,因为规划器无法利用任何查询。根据您当前的设计和足够数量的记录,unnest 将是您最不用担心的问题。

更新 即使更新了问题,使用标准化代码表也会给您带来最少的麻烦并获得最佳的扫描结果。任何时候您发现自己创建了编号列,这都很好地表明您可能想要标准化。话虽这么说,您可以创建一个计算文本列来用作搜索词列。在您的情况下,您可以创建一个 search_words 列,该列在插入和更新时由触发器填充。然后,您可以创建一个 tsvectorsearch_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 a tsvector to build full text queries on the search_words

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文