PostgreSQL:不区分大小写的字符串比较

前端之家收集整理的这篇文章主要介绍了PostgreSQL:不区分大小写的字符串比较前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Postgresql有一个简单的忽略大小写比较?

我要替换:

  1. SELECT id,user_name
  2. FROM users
  3. WHERE lower(email) IN (lower('adamB@a.com'),lower('eveA@b.com'));

有类似的东西:

  1. SELECT id,user_name
  2. FROM users
  3. WHERE email IGNORE_CASE_IN ('adamB@a.com','eveA@b.com');

编辑:like和类似的运算符工作在单个值(例如像“adamB@a.com”),而不是集。

有任何想法吗?

亚当

首先,什么不做,不使用ilike …
  1. create table y
  2. (
  3. id serial not null,email text not null unique
  4. );
  5.  
  6. insert into y(email)
  7. values('iSteve.jobs@apple.com'),('linus.Torvalds@linUX.com');
  8. insert into y(email)
  9. select n from generate_series(1,1000) as i(n);
  10. create index ix_y on y(email);
  11.  
  12. explain select * from y
  13. where email ilike
  14. ANY(ARRAY['ISteve.Jobs@Apple.com','Linus.Torvalds@Linux.com']);

执行计划:

  1. memdb=# explain select * from y where email ilike ANY(ARRAY['ISteve.Jobs@Apple.com','Linus.Torvalds@Linux.com']);
  2. QUERY PLAN
  3. ----------------------------------------------------------------------------------------
  4. Seq Scan on y (cost=0.00..17.52 rows=1 width=7)
  5. Filter: (email ~~* ANY ('{ISteve.Jobs@Apple.com,Linus.Torvalds@Linux.com}'::text[]))
  6. (2 rows)

这是你创建一个索引的低级表达式…

  1. create function lower(t text[]) returns text[]
  2. as
  3. $$
  4. select lower($1::text)::text[]
  5. $$ language sql;
  6.  
  7. create unique index ix_y_2 on y(lower(email));
  8.  
  9. explain select * from y
  10. where lower(email) =
  11. ANY(lower(ARRAY['ISteve.Jobs@Apple.com','Linus.Torvalds@Linux.com']));

…其中正确使用索引:

  1. memdb=# explain select * from y where lower(email) = ANY(lower(ARRAY['ISteve.Jobs@Apple.com','Linus.Torvalds@Linux.com']));
  2. QUERY PLAN
  3. --------------------------------------------------------------------------------------------------------------------------------
  4. Bitmap Heap Scan on y (cost=22.60..27.98 rows=10 width=7)
  5. Recheck Cond: (lower(email) = ANY ((lower(('{ISteve.Jobs@Apple.com,Linus.Torvalds@Linux.com}'::text[])::text))::text[]))
  6. -> Bitmap Index Scan on ix_y_2 (cost=0.00..22.60 rows=10 width=0)
  7. Index Cond: (lower(email) = ANY ((lower(('{ISteve.Jobs@Apple.com,Linus.Torvalds@Linux.com}'::text[])::text))::text[]))
  8. (4 rows)

或者使用citext数据类型…

  1. create table x
  2. (
  3. id serial not null,email citext not null unique
  4. );
  5.  
  6. insert into x(email)
  7. values('iSteve.jobs@apple.com'),('linus.Torvalds@linUX.com');
  8. insert into x(email)
  9. select n from generate_series(1,1000) as i(n);
  10. create index ix_x on x(email);
  11.  
  12. explain select * from x
  13. where email =
  14. ANY(ARRAY['ISteve.Jobs@Apple.com','Linus.Torvalds@Linux.com']::citext[]);

…即使您不在表达式上创建索引(例如,在yyy(下(字段))上创建索引zzz,也可以正确使用索引):

  1. memdb=# explain select * from x where email = ANY(ARRAY['ISteve.Jobs@Apple.com','Linus.Torvalds@Linux.com']::citext[]);
  2. QUERY PLAN
  3. --------------------------------------------------------------------------------------------------
  4. Bitmap Heap Scan on x (cost=8.52..12.75 rows=2 width=7)
  5. Recheck Cond: (email = ANY ('{ISteve.Jobs@Apple.com,Linus.Torvalds@Linux.com}'::citext[]))
  6. -> Bitmap Index Scan on ix_x (cost=0.00..8.52 rows=2 width=0)
  7. Index Cond: (email = ANY ('{ISteve.Jobs@Apple.com,Linus.Torvalds@Linux.com}'::citext[]))
  8. (4 rows)

猜你在找的Postgre SQL相关文章