索引与优化like查询
2023-03-14 22:31:16 时间
1. like %keyword 索引失效,使用全表扫描。但可以通过翻转函数+like前模糊查询+建立翻转函数索引=走翻转函数索引,不走全表扫描。
2. like keyword% 索引有效。
3. like %keyword% 索引失效,也无法使用反向索引。
====================================================================
1. 使用下面的函数来进行模糊查询,如果出现的位置〉0,表示包含该字符串。
查询效率比like要高。
如果: table.field like ‘%AAA%’ 可以改为 locate (‘AAA’ , table.field) > 0
LOCATE(substr,str)
POSITION(substr IN str)
返回子串substr在字符串str第一个出现的位置,如果substr不是在str里面,返回0。
使用instr
select count(*) from table t where instr(t.column,’xx’)> 0
这种查询效果很好,速度很快。
from Policy_Data_All c, Item_Data_All i
where c.c_ply_no = i.c_ply_no
and i.C_LCN_NO like ’%245’
2. like keyword% 索引有效。
3. like %keyword% 索引失效,也无法使用反向索引。
====================================================================
1. 使用下面的函数来进行模糊查询,如果出现的位置〉0,表示包含该字符串。
查询效率比like要高。
如果: table.field like ‘%AAA%’ 可以改为 locate (‘AAA’ , table.field) > 0
LOCATE(substr,str)
POSITION(substr IN str)
返回子串substr在字符串str第一个出现的位置,如果substr不是在str里面,返回0。
使用instr
select count(*) from table t where instr(t.column,’xx’)> 0
这种查询效果很好,速度很快。
2. 查询%xx的记录
select count(c.c_ply_no) as COUNT
select count(c.c_ply_no) as COUNT
from Policy_Data_All c, Item_Data_All i
where c.c_ply_no = i.c_ply_no
and i.C_LCN_NO like ’%245’
在执行的时候,执行计划显示,消耗值,io值,cpu值均非常大,原因是like后面前模糊查询导致索引失效,进行全表扫描
解决方法:这种只有前模糊的sql可以改造如下写法
select count(c.c_ply_no) as COUNT
from Policy_Data_All c, Item_Data_All i
where c.c_ply_no = i.c_ply_no
and reverse(i.C_LCN_NO) like reverse(‘%245’)
使用翻转函数+like前模糊查询+建立翻转函数索引=走翻转函数索引,不走全扫描。有效降低消耗值,io值,cpu值这三个指标,尤其是io值的降低。
本文来源于"阿里中间件团队播客",原文发表时间" 2012-04-10"
相关文章
- AWS IoT SiteWise – 现已正式发布
- 使用 EBS Direct API 从任何块存储创建快照
- 新增功能 – 在 AWS Outposts 上创建 Amazon RDS 数据库实例
- 深度解析:AWS Fargate 数据平面
- 如何用 Glue ETL 输出分区数据
- Amazon DocumentDB(兼容 MongoDB)推出基于角色的访问控制
- 利用云上灾备管理工具迁移和保护 EC2 实例
- Amazon RDS Proxy – 现已全面推出
- 使用基于 GTID 的复制通过回退选项迁移至 Amazon Aurora MySQL
- 利用 Amazon Neptune T3 实例将构建图形应用程序的成本降低最多 76%
- 使用 Amazon Neptune 绘制投资依存关系图
- 使用全自动工具将 Neo4j 图形数据库迁移到 Amazon Neptune
- 在 Amazon DocumentDB(兼容 MongoDB)中使用 $dateFromString 和 executionStats
- Amazon DocumentDB(兼容MongoDB)教程第三部分——使用 Robo 3T
- 利用 Amazon CloudWatch 指标做出更好的 Amazon RDS 决策
- 众多的数据库类型,你该怎么选择?
- Amazon DocumentDB 入门(兼容 MongoDB);第 2 部分 – 使用 AWS Cloud9
- Amazon DocumentDB 入门(兼容 MongoDB);第 1 部分 – 使用 Amazon EC2
- 在 Amazon Aurora Global Database 中使用写入转发构建全球分布式 MySQL 应用程序
- Amazon Redshift Spectrum – EB 级的 S3 数据就地查询