作者
digoal
日期
2021-12-20
标签
PostgreSQL , 热门问题
- 问题说明(现象、环境)
- 分析原因
- 结论和解决办法
10、为什么有的索引不支持字符串前置查询? (patten, lc_collate)
https://www.bilibili.com/video/BV1G3411v7Ts/
collate <> C
时, 默认index ops不支持like 前缀、~
模糊前缀索引扫描.
解决办法:
1、才有非默认ops
2、创建索引时指定collate=C
例子:
create unlogged table a (id int, info text); insert into a select generate_series(1,1000000), md5(random()::text); select * from a where info like 'xx%'; select * from a where info ~ '^xx';
复制
数据库collate <> C
.
\l collate <> C create index idx_a_1 on a (info); db1=# set enable_seqscan=off; SET db1=# explain select * from a where info like 'xx%'; QUERY PLAN ------------------------------------------------------------------------- Seq Scan on a (cost=10000000000.00..10000020834.00 rows=5000 width=36) Filter: (info ~~ 'xx%'::text) (2 rows) db1=# create index idx_a_2 on a (info text_pattern_ops); CREATE INDEX db1=# explain select * from a where info like 'xx%'; QUERY PLAN -------------------------------------------------------------------- Index Scan using idx_a_2 on a (cost=0.42..2.65 rows=100 width=37) Index Cond: ((info ~>=~ 'xx'::text) AND (info ~<~ 'xy'::text)) Filter: (info ~~ 'xx%'::text) (3 rows) drop index idx_a_2; create index idx_a_3 on a (info collate "C"); db1=# explain select * from a where info like 'xx%'; QUERY PLAN -------------------------------------------------------------------- Index Scan using idx_a_3 on a (cost=0.42..2.65 rows=100 width=37) Index Cond: ((info >= 'xx'::text) AND (info < 'xy'::text)) Filter: (info ~~ 'xx%'::text) (3 rows) db1=# explain select * from a where info ~ '^xx'; QUERY PLAN -------------------------------------------------------------------- Index Scan using idx_a_3 on a (cost=0.42..2.65 rows=100 width=37) Index Cond: ((info >= 'xx'::text) AND (info < 'xy'::text)) Filter: (info ~ '^xx'::text) (3 rows) db1=# explain select * from a where info like 'xx%' collate "zh_CN"; QUERY PLAN -------------------------------------------------------------------- Index Scan using idx_a_3 on a (cost=0.42..2.65 rows=100 width=37) Index Cond: ((info >= 'xx'::text) AND (info < 'xy'::text)) Filter: (info ~~ 'xx%'::text COLLATE "zh_CN") (3 rows) db1=# explain select * from a where info like 'xx%' collate "C"; QUERY PLAN -------------------------------------------------------------------- Index Scan using idx_a_3 on a (cost=0.42..2.65 rows=100 width=37) Index Cond: ((info >= 'xx'::text) AND (info < 'xy'::text)) Filter: (info ~~ 'xx%'::text COLLATE "C") (3 rows)
复制
数据库collate = C
.
\l collate=C create index idx_a_1 on a (info); postgres=# explain select * from a where info like 'xx%'; QUERY PLAN -------------------------------------------------------------------- Index Scan using idx_a_1 on a (cost=0.42..2.65 rows=100 width=37) Index Cond: ((info >= 'xx'::text) AND (info < 'xy'::text)) Filter: (info ~~ 'xx%'::text) (3 rows)
复制
期望 PostgreSQL 增加什么功能?
PolarDB for PostgreSQL云原生分布式开源数据库
PostgreSQL 解决方案集合
德哥 / digoal's github - 公益是一辈子的事.
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
玩一玩系列——玩玩pg_mooncake(PostgreSQL的高性能列存新贵)
小满未满、
512次阅读
2025-03-03 17:18:03
王炸!OGG 23ai 终于支持从PostgreSQL备库抽取数据了
曹海峰
389次阅读
2025-03-09 12:54:06
玩一玩系列——玩玩login_hook(一款即将停止维护的PostgreSQL登录插件)
小满未满、
371次阅读
2025-03-08 18:19:28
明明想执行的SQL是DELETE、UPDATE,但为什么看到的是SELECT(FDW的实现原理解析)
小满未满、
346次阅读
2025-03-19 23:11:26
PostgreSQL初/中/高级认证考试(3.15)通过考生公示
开源软件联盟PostgreSQL分会
297次阅读
2025-03-20 09:50:36
IvorySQL 4.4 发布 - 基于 PostgreSQL 17.4,增强平台支持
通讯员
182次阅读
2025-03-20 15:31:04
套壳论
梧桐
179次阅读
2025-03-09 10:58:17
命名不规范,事后泪两行
xiongcc
167次阅读
2025-03-13 14:26:08
版本发布| IvorySQL 4.4 发布
IvorySQL开源数据库社区
110次阅读
2025-03-13 09:52:33
宝藏PEV,助力你成为SQL优化高手
xiongcc
101次阅读
2025-03-09 23:34:23
热门文章
阿里巴巴的使命、愿景、核心价值观
2021-01-04 67556浏览
MacOS 关闭和开启虚拟内存(swap)
2022-01-20 17424浏览
[珍藏级] PostgreSQL ssl 证书配置 - 防止中间攻击者 - 以及如何使用证书无密码登录配置cert
2020-06-19 16421浏览
PostgreSQL md5hash插件 - 128bit 存储,压缩空间、提升效率
2019-11-08 14320浏览
产品与运营-OKR的设计、总结、复盘、规划、组织保障和考核例子
2022-01-20 13037浏览
最新文章
PostgreSQL 15 preview - PostgreSQL 15 pg_stat_statements 增加对temp file blocks io timing的统计, 增加JIT的统计.
2022-01-20 265浏览
德说-第92期, 怎么解决躺平|不想奋斗?
2022-01-20 296浏览
PostgreSQL 增量物化视图插件 - pg_ivm incremental materialized view maintenance
2022-01-20 1363浏览
PostgreSQL 15 preview - ARM多核适配 性能提升 - Use ISB as a spin-delay instruction on ARM64
2022-01-20 758浏览
PostgreSQL 15 preview - recovery(包括崩溃恢复、逻辑流复制、物理流复制、归档恢复) 加速, 支持异步prefetch 预读接下来要恢复的wal record相关的data block到shared buffer
2022-01-20 1189浏览