当前位置:网站首页>【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优化
更新时尽量根据有索引的字段更新,这样子是行锁,否则会变成表锁。
边栏推荐
- Autosar 学习记录(1) – EcuM_Init
- Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
- MySQL and redis consistency solution
- 【微服务|openfeign】Feign的日志记录
- 北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......
- 【R语言】两个/N个数据合并merge函数
- Mysql与Redis一致性解决方案
- 【技能】创建.bat快速打开网页
- Todolist classic case ①
- Redisson utilise la solution complète - redisson Documents officiels + commentaires (Partie 1)
猜你喜欢

关于图灵测试和中文屋Chinese room的理解

2022制冷与空调设备运行操作国家题库模拟考试平台操作

Warm congratulations on the successful listing of five elements hehe liquor

Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
![[network planning] (I) hub, bridge, switch, router and other concepts](/img/7b/fcef37496517c854ac1dbfb35fa3f4.png)
[network planning] (I) hub, bridge, switch, router and other concepts

【编程强训2】排序子序列+倒置字符串

她就是那个「别人家的HR」|ONES 人物

Illusory and simple screen raindrop post-processing effect

atguigu----脚手架--02-使用脚手架(2)

浏览器本地存储
随机推荐
Eigen matrix operation Library
Summary of the concept and advantages of 5g massive MIMO
We found a huge hole in MySQL: do not judge the number of rows affected by update!!!
C language implementation [Sanzi chess game] (step analysis and implementation source code)
华为ModelArts训练Alexnet模型
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation
[programming training] delete public characters (hash mapping) + team competition (greedy)
Inftnews | from "avalanche" to Baidu "xirang", 16 major events of the meta universe in 30 years
Is it suitable for girls to study product manager? What are the advantages?
Vscode automatically formats code according to eslint specification
Reply and explanation on issues related to "online training of network security education in 2022"
她就是那个「别人家的HR」|ONES 人物
Cadence OrCAD Capture “网络名”相同,但是未连接或连接错误的解放方案之nodename的用法
[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
论文学习——水文时间序列相似性查询的分析与研究
C language implementation [minesweeping game] full version (implementation source code)
Custom events of components ②
Paging in servlets and JSPS
ctfshow-web352,353(SSRF)
Is it reliable to open an account on the compass with your mobile phone? Is there any potential safety hazard