当前位置: 首页 > news >正文

mysql 查询优化 、索引失效

查询优化

物理查询优化

通过索引和表连接方式等技术来进行优化,这里重点需要掌握索引的使用

逻辑查询优化

通过SQL 等价变换 提升查询效率,直白一点就是说,换一种查询写法执行效率可能更高

索引失效

  1. 计算、函数、类型转换(自动或手动)导致索引失效

select sql_no_cache * from student where left(sutdent_name,3)=‘abc’ type: all
优化
select sql_no_cache * from student where student_name like ‘abc%’ tyoe: range

假设student_name 数据类型 varchar(5),且student_name 有普通索引
select sql_no_cache * from student where student_name =123 type:all
优化
select sql_no_cache * from student where student_name =‘123’ type:index

create index idx_sno on student(stuno);
select sql_no_cache * from student where stuno+1=2023008; type:all
优化
select sql_no_cahe * from student where stuno=2023007 type: index

联合索引(复合索引)
对于多列索引,过滤条件要使用索引必须按照索引建立的顺序,依次满足,一旦跳过某个字段,索引后面的字段都无法被使用。
如果查询条件中没有使用这些字段中第一个字段时,多列(联合)索引不会被使用。

范围条件右边的列索引失效
create index idx_age_classId_name on student(age,classId,name);
select sql_no_cache * from student where student.age=30 and student.classId>20 and student.name=‘abc’; ## name 索引就失效了
应用开发中范围查询,例如 金额查询、日期查询往往都是范围查询。应将查询条件放置where 语句最后(创建的联合索引中,务必把访问涉及到的字段写在最后) sql 语句中where 顺序 并不影响mysql 优化策略的。 联合索引的顺序影响的。

is null 可以使用索引 is not null 不能使用索引
not like 页不能使用索引,导致全部扫描
<>,!= 不等于 不能使用索引
like % 开头 索引失效 stuname like ‘%123’ , 不建议 左模糊,全模糊
OR 前后存在非索引的列,索引失效
最后在设计数据表的时候将字段设置为 not null 约束,
比如你可以将int 类型的字段,默认值设置为0.
将字符类型的默认值设置为空字符串(‘’)

总结

index(a,b,c)
在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/156738.html

相关文章:

  • 支付宝pc支付(springboot版),简单配置即可实现支付
  • 【Redis专题】Redis持久化、主从与哨兵架构详解
  • 【vue2第十三章】自定义指令 自定义v-loading指令
  • 数据结构--6.3查找算法(静态、动态)(插值查找)
  • Spring Boot日志基础使用 设置日志级别
  • Playwright for Python:断言
  • websocket--技术文档--spring后台+vue基本使用
  • day01-ES6新特性以及ReactJS入门
  • MySQL5.7慢查询实践
  • MySQL数据库的增删改查(进阶)
  • 韶音骨传导耳机好不好用,韶音的骨传导耳机怎么样
  • Nginx从安装到使用,反向代理,负载均衡
  • freertos之资源管理
  • 1.创建项目(wpf视觉项目)
  • 使用element-ui导航,进入对应的三级页面菜单保持点击状态
  • golang字符串转64位整数
  • 创作纪念日-我的第1024天
  • 【线上问题】很抱歉,如果没有 JavaScript 支持,将不能正常工作
  • 便捷、快速、稳定、高性能!以 GPU 实例演示 Alibaba Cloud Linux 3 对 AI 生态的支持 | 龙蜥技术
  • 创新科技改变城市:智慧城市建设全景展望
  • Kotlin 环境下解决属性初始化问题
  • Java复习-20-接口(3)- 代理设计模式
  • 如何远程访问Linux MeterSphere一站式开源持续测试平台
  • LinuxUbuntu安装OpenWAF
  • LeetCode 剑指offer 09.用两个栈实现队列
  • 第三方软件检测机构有哪些资质,2023年软件测评公司推荐
  • Unity的GPUSkinning进一步介绍
  • Mysql redolog
  • 【设计模式】Head First 设计模式——桥模式 C++实现
  • CESM2代码下载