索引与查询使用的 collate 不一致导致无法使用索引

索引与表的collate 不一致的情况下,会导致表上的索引不可用,这时要想使用索引,必须在SQL 语句指定建索引所用的collate。

数据库默认collate :

test=# \l
                               List of databases
   Name    | Owner  | Encoding |   Collate   |    Ctype    | Access privileges
-----------+--------+----------+-------------+-------------+-------------------
 security  | system | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | system | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/system        +
           |        |          |             |             | system=CTc/system
 template1 | system | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/system        +
           |        |          |             |             | system=CTc/system
 test      | system | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)

例子1:使用 collate "C" :表使用collate "C" , 则索引及查询语句默认也使用 collate "C" 这可以使用索引。

test=# create table t1(id text collate "C");
CREATE TABLE
test=# insert into t1 select generate_series(1000001,2000000);
INSERT 0 1000000
test=# create index idx_1 on t1(id);

CREATE INDEX
test=#
test=# analyze t1;
ANALYZE
test=# explain analyze select * from t1 where id='12345';
                                                  QUERY PLAN
---------------------------------------------------------------------------------------------------------------
 Index Only Scan using idx_1 on t1  (cost=0.42..8.44 rows=1 width=8) (actual time=0.033..0.033 rows=0 loops=1)
   Index Cond: (id = '12345'::text)
   Heap Fetches: 0
 Planning Time: 0.214 ms
 Execution Time: 0.054 ms
(5 rows)

例子2:表使用 “C” ,索引使用 “POSIX” , 则SQL 默认使用 “C”。要想使用索引,SQL必须加 collate "POSIX";

test=# drop index idx_1;
DROP INDEX
test=#
test=# create index idx_1 on t1(id collate "POSIX");
CREATE INDEX
test=# explain analyze select * from t1 where id='12345';
                                                   QUERY PLAN
----------------------------------------------------------------------------------------------------------------
 Gather  (cost=1000.00..10633.43 rows=1 width=8) (actual time=42.802..42.948 rows=0 loops=1)
   Workers Planned: 2
   Workers Launched: 2
   ->  Parallel Seq Scan on t1  (cost=0.00..9633.33 rows=1 width=8) (actual time=34.985..34.987 rows=0 loops=3)
         Filter: (id = '12345'::text)
         Rows Removed by Filter: 333333
 Planning Time: 0.226 ms
 Execution Time: 42.973 ms
(8 rows)

test=#
test=# explain analyze select * from t1 where id='12345' collate "POSIX";
                                                  QUERY PLAN
---------------------------------------------------------------------------------------------------------------
 Index Only Scan using idx_1 on t1  (cost=0.42..8.44 rows=1 width=8) (actual time=0.093..0.094 rows=0 loops=1)
   Index Cond: (id = '12345'::text COLLATE "POSIX")
   Heap Fetches: 0
 Planning Time: 0.095 ms
 Execution Time: 0.116 ms
(5 rows)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值