当前位置:网站首页>【mysql学习笔记25】sql语句优化
【mysql学习笔记25】sql语句优化
2022-07-01 07:30:00 【yqs_281876918】
insert优化
1.多条单个insert语句可以合并成一条批量插入insert语句。一次插入肯定比多次插入效果好,因为每次插入都需要建立连接。
2.手动控制事务。因为mysql的事务默认是自动的,每执行一条sql语句都会开启一个事务。
3.主键顺序插入。这和mysql底层有关。
4.大批量数据导入不建议使用insert,而是load指令。
#客户端连接服务端时,加上参数–local-infile
mysql --local-infile -u root -p
#设置全局参数local_infile为1,开启从本地加载文件导入数据的开关
set global local_infile = 1;
#执行load指令将准备好的数据,加载到表结构中
load data local infile ‘/root/sql1.log’ into table ‘tb_user’ fields terminated by ‘,’ lines terminated by ‘\n’;
主键优化
1.在innodb引擎中,表数据是根据主键组织存放的,这种存储方式称为索引组织表(index organized table IOT)
主键乱序插入时容易发生页分裂,因此应避免乱序插入,或者直接使用自增主键,一劳永逸
拓展:页合并,当一页中的数据量低于50%时,mysql会尝试合并相邻页,称为页合并
2.在开发中应该尽量降低主键长度,因为二级索引存的都是主键,逐渐越长占空间越大
order by优化
using filesort:把数据放入缓存区,进行排序。
using index:直接利用索引返回排序结果。
mysql由于使用了B+Tree索引,容易按顺序遍历数据,索引当order by后面跟的字段有索引时,就不需要额外的排序,使得order by效率提高。
如果没有使用覆盖索引,那么使用索引排序是不现实的,因为缺少字段,所以需要进行filesort。
filesort的缓冲是是有大小限制的,如果数据超出了缓冲区就会在磁盘上排序,大大影响性能。
缓冲区大小是可以调整的。
group by优化
using temporary:使用临时表分组,效率低
using index:使用索引分组,效率高
设表user有(profession,age,status)联合索引
select profession from user group by profession;#using index
select age,count(age) from user group by age;#using index;using temporary
注:前面有一个using index是因为覆盖索引
select profession , age, count() from tb_user group by profession,age;#using index
select age, count() from tb_user where profession = ‘软件工程’ group by age;#using index
limit优化
select * from tb_sku limit 2000000,10;
替换为
select * from tb_sku t , (select id from tb_sku order by id limit 2000000,10) a where t.id = a.id;
count优化
myisam引擎使用单独变量记录了表的记录总数,所以count效率很高,前提是没有where
innodb引擎则需要全表扫描,才能统计数量
优化思路:自己额外开辟一个地方存储总数量
count(字段)<count(主键)<count(1)≈count(*)
update优化
更新时尽量根据有索引的字段更新,这样子是行锁,否则会变成表锁。
边栏推荐
- 【深圳IO】精确食品称(汇编语言的一些理解)
- 2022电工(中级)复训题库及答案
- Redisson uses the full solution - redisson official documents + comments (Part 2)
- [Shenzhen IO] precise Food Scale (some understanding of assembly language)
- JSP - paging
- C# Newtonsoft. Use of job in JSON
- Detailed explanation of weback5 basic configuration
- go-etcd
- 【编程强训2】排序子序列+倒置字符串
- 2022 electrician (intermediate) recurrent training question bank and answers
猜你喜欢
运维管理系统,人性化操作体验
H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
Challenges faced by operation and maintenance? Intelligent operation and maintenance management system to help you
下载Xshell和Xftp
Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
The programmer of Beipiao posted a post for help late at night: I am lonely when my girlfriend is gone
redisson使用全解——redisson官方文档+注释(中篇)
Is it suitable for girls to study product manager? What are the advantages?
2022年流动式起重机司机考试练习题及在线模拟考试
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
随机推荐
Paging in servlets and JSPS
Oracle创建自增id
Alibaba OSS postman invalid according to policy: policy condition failed: ["starts with", "key", "test/"]
The database is locked. Is there a solution
她就是那个「别人家的HR」|ONES 人物
Jax's deep learning and scientific computing
Is it safe and reliable for Huatai Securities to open an account? How to open Huatai Securities Account
1286_FreeRTOS的任务优先级设置实现分析
Is it safe to buy funds on the brokerage account
浏览器本地存储
【R语言】两个/N个数据合并merge函数
Félicitations pour l'inscription réussie de wuxinghe
LeetCode+ 71 - 75
How to draw a product architecture diagram?
赌上了绩效,赢了公司CTO,我要搭DevOps平台!
[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched
如何让两融交易更极速
2022 tea master (intermediate) recurrent training question bank and answers
weback5基础配置详解
【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字