当前位置:网站首页>SQL优化的N种方法
SQL优化的N种方法
2022-07-26 22:49:00 【识时务者-HJJ】
文章目录
- SQL优化的N种方法
- 1.SQL语句中IN包含的值不应过多:
- 2.SELECT语句务必指明字段名称:
- 3.只查询一条数据的时候,使用limit 1
- 4.避免在where子句中对字段进行null值判断
- 5.避免在where子句中对字段进行表达式操作:
- 6.对于联合索引来说,要遵守最左前缀法则:
- 7.尽量使用inner join,避免left join:
- 8.注意范围查询语句:
- 9.不建议使用%前缀模糊查询:
- 10.在 where 子句中使用 or 来连接条件,如果or连接的条件有一方没有索引,将导致引擎放弃使用索引而进行全表扫描
- 11.应尽量避免在where子句中对字段进行函数操作,这将导致引擎放弃使用索引而进行全表扫描
- 12.字符串类型的字段 查询的时候如果不加引号'' ,会导致自动进行隐式转换,然后索引失效
- 我的学习论坛
SQL优化的N种方法
1.SQL语句中IN包含的值不应过多:
例如:select id from handsome where number in(1,2,3) 对于连续的数值,能用between就不要用in了。
2.SELECT语句务必指明字段名称:
禁止用 * 来查询 ,禁止用 * 来查询 ,禁止用 * 来查询 ,重要的事情说三遍, 查找哪个字段,就写具体的字段。
3.只查询一条数据的时候,使用limit 1
【这个很有用】
4.避免在where子句中对字段进行null值判断
5.避免在where子句中对字段进行表达式操作:
select id from user WHERE id*100=500;
上面的sql对字段就行了算术运算,这会造成引擎放弃使用索引,建议改成:
select id from user WHERE id=500/100;
6.对于联合索引来说,要遵守最左前缀法则:
例如组合索引(id,name,sex) 使用的时候,可以id 或者id,name 。禁止直接name,或者sex会导致联合索引失败
注意: id, name,sex 这三个字段填写顺序不会有影响, mysql会自动优化成最左匹配的顺序
前三条sql都能命中索引,中间两条由于不符合最左匹配原则,索引失效
最后一条sql 由于有最左索引id 所以索引部分成功,部分失效. id字段索引使用成功
explain select * from `user_test` where uid=10 ;
explain select * from `user_test` where uid=10 and name='识时务者';
explain select * from `user_test` where uid=10 and name='识时务者' and phone='13047967256';
explain select * from `user_test` where name='识时务者' and phone='13047967256';
explain select * from `user_test` where name='识时务者';
explain select * from `user_test` where uid=10 and phone='13047967256';
7.尽量使用inner join,避免left join:
如果连接方式是inner join,在没有其他过滤条件的情况下MySQL会自动选择小表作为驱动表,但是left join在驱动表的选择上遵循的是左边驱动右边的原则,即left join左边的表名为驱动表。
8.注意范围查询语句:
对于联合索引来说,如果存在范围查询,比如between、>、<等条件时,会造成后面的索引字段失效。
解决办法: 业务允许的情况下,使用 >= 或者<= 这样不影响索引的使用
9.不建议使用%前缀模糊查询:
例如 : LIKE“%name”或者LIKE“%name%”,这种查询会导致索引失效而进行全表扫描。但是可以使用LIKE “name%”。
10.在 where 子句中使用 or 来连接条件,如果or连接的条件有一方没有索引,将导致引擎放弃使用索引而进行全表扫描
解决办法: 将or连接的双方都建立索引,就可以使用
11.应尽量避免在where子句中对字段进行函数操作,这将导致引擎放弃使用索引而进行全表扫描
12.字符串类型的字段 查询的时候如果不加引号’’ ,会导致自动进行隐式转换,然后索引失效
我的学习论坛
HandsomeForum:用Java编写的学习论坛,打造我们自己的圈子!(http://huangjunjie.vip:66)
文章链接:http://huangjunjie.vip:66/blog/read/zmkyghlz0rf8i3bes1
边栏推荐
- ACM mode input and output exercise
- 初识C语言(1)
- C语言——关系运算符和逻辑运算符、if语句、switch语句、分支结构的嵌套
- Brief introduction of VLAN principle and specific experimental configuration
- C语言——数据类型、基本数据类型的取值范围
- Lora光照传感器节点数据采集
- TCP的三次握手与四次断开
- Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
- 第三讲--GPIO输入输出库函数使用以及相关例程
- 动态路由ofps协议配置
猜你喜欢

(title + detailed idea + annotated code) codeforces round 805 (Div. 3) F Equate Multisets

Introduction to STM32 lesson 1

(前缀和/思维)Codeforces Round #806 (Div. 4)F. Yet Another Problem About Pairs Satisfying an Inequality

Experiment of total connection and star topology of mGRE

关于在VS2022或者高级版本运行环境下遇到fopen,strerror等不安全的问题

定时器中断实验

HCIA(网络初级综合实验练习)

OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)

OSPF的重发布及路由策略

初识C语言(2)
随机推荐
OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
CF 1333C Eugene and an array
OSPF协议知识汇总
(the most detailed in History) codeforces round 805 (Div. 3) e Split Into Two Sets
OSPF静态大实验
6.28 Dahua written examination
[explain C language in detail] takes you to play with loop structure (for_while_do while)
Lora通信应用开发
Solution: various error reports and pit stepping and pit avoidance records encountered in the alchemist cultivation plan pytoch+deeplearning (II)
第五讲—按键控制LED
数字集成电路:CMOS反相器(一)静态特性
mgre的全连和星型拓扑实验
Dynamic routing rip protocol experiment
RISC-V工具链编译笔记
NB-IOT接入云平台
Text to image paper intensive reading ssa-gan: text to image generation with semantic spatial aware Gan
C语言——关系运算符和逻辑运算符、if语句、switch语句、分支结构的嵌套
Is index reproduction text generation image is score quantitative experiment whole process reproduction inception score quantitative evaluation experiment step on the pit and avoid the pit process
Golang中的错误处理
npm报错, Error: EPERM: operation not permitted, mkdir