Sql 的 Like 语句

default

示例

1
2
3
4
5
6
7
8
-- 左右均有 %
select * from data_table t where t.name like '%test%';

-- 左侧 %
select * from data_table t where t.name like '%test';

-- 右侧 %
select * from data_table t where t.name like 'test%';

PostgreSQL 和 MySQL 比较

情况 索引 PostgreSQL MySQL
左右均有 % BTREE 很慢,全表匹配 很慢,全表匹配
左侧 % BTREE 一般,稍慢于"右侧 %",利用了逆序扫描优化 很慢,全表匹配
右侧 % BTREE 一般,利用了正序扫描优化 一般,利用了正序扫描优化

“左侧 %“的语句中,PG 效率高很多,因为 PG 有利用到逆序扫描优化,而 MySQL 依旧是扫描全表。

Licensed under CC BY-NC-SA 4.0
Gear(夕照)的博客。记录开发、生活,以及一些不足为道的思考……