常用sql的小实例是否走索引

sql语句中一些平时容易忽略的一些点

1、对字段进行表达式操作,这将导致引擎放弃使用索引而进行全表扫描
select * from a where amount*10>11000 (amount 是索引列)
select * from a where amount>11000/10 (amount)
哪一个语句效率高? 第二句

2、a 表有一个组合索引 name sex age(平时组合索引注意不到的点,首字段原则,即只要sql语句中有组合索引的第一个字段即可,顺序不影响,以下是几种情况以及会不会走索引)
select * from a where sex=‘男’ and age=15;会走索引吗? 不会
select * from a where sex=‘男’ 会走索引吗? 不会
select * from a where age=15会走索引吗? 不会
selec * from a where name=‘张三’ and sex=‘男’ 会走索引吗?
selec * from a where name=‘张三’ and age=15 会走索引吗?

3、 a 表有索引1 name ,索引2 age(以下是一些导致索引失效的sql场景)
select * from a where age<>20; 会走索引吗?
不会,where后面字段使用<>进行判断,索引失效
select * from a where name is null 会走索引吗?
不会,使用is null,is not null,对字段进行判断
select * from a where age=10 or age=20会走索引吗?
不会,使用or连接词对字段进行判断
select * from a where age=10
union all
select * from a where age=20会走索引吗?
会,使用联表查询替换or关键字,不对索引造成影响
select * from a where name like ‘%张%’ 会走索引吗?
不会,使用前置模糊查询,导致索引失效
select * from a where name like ‘张%’ 会走索引吗?
会,使用后置模糊查询,索引正常使用
select * from a where age in (10,20,30) 会走索引吗?
不会,使用in,not in对字段进行判断 索引失效

4 、a 表结构如下:
crate table a(
id 唯一编号
name 名称
age 年龄
sex 性别
address 地址
major 专业
)
以下语句那一个效率高 第2句(oracle的字段解析从右向左)
(这里是oracle数据库的常识性问题,且容易忽略的一点)oracle数据库sql语句的字段解析顺序是从右至左,所以从右至左的字段标识度越高,他的查询效率越高

select * from a where name=‘张三’ and id=12345 and age=20 and sex=‘男’;
select * from a where sex=‘男’ and age=20 and name=‘张三’ and id=12345;

以上是本人自己平时容易犯的错误,希望对有需要的小伙伴,有些帮助

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值