首页 技术 正文
技术 2022年11月20日
0 收藏 527 点赞 4,544 浏览 1908 个字

老李分享:MySql的insert语句的性能优化方案

 

性能优化一直是测试人员比较感兴趣的内容,poptest在培训学员的时候也加大了性能测试调优的方面的内容,而性能优化需要经验的积累,经验的积累依靠项目实战,poptest培养测试开发工程师的性能测试的能力的时候,就不断的添加实战项目来帮助学员快速积累经验,能在工作中尽快上手。

下面是数据库方面调优的一个小例子,你也完全可以通过掌握基础知识,完成简单的sql语句级别的性能调优。Poptest是国内唯一一家在测试开发工程师培养的机构,很专注自动化测试,性能测试,安全性测试,移动端的自动化测试的人员培养。

MySql批量插入:
1. 一条SQL语句插入多条数据。
2. 在事务中进行插入处理。
3. 数据有序插入。(根据索引顺序插入)

原理:
1. SQL语句有长度限制,在进行数据合并在同一SQL中务必不能超过SQL长度限制,通过max_allowed_packet配置修改,默认是1M,测试时修改为8M。
2. 事务需要控制大小,事务太大可能会影响执行的效率。MySQL有innodb_log_buffer_size配置项,超过这个值会把innodb的数据刷到磁盘中,效率会有所下降。所以比较好的做法是,在数据达到这个这个值前进行事务提交。

1. 一条SQL语句插入多条数据

INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`)    VALUES ('0''userid_0''content_0', 0);INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`)    VALUES ('1''userid_1''content_1', 1);改成:INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`)    VALUES ('0''userid_0''content_0', 0), ('1''userid_1''content_1', 1); 

2. 在事务中进行插入处理。

START TRANSACTION;INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`)    VALUES ('0''userid_0''content_0', 0);INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`)    VALUES ('1''userid_1''content_1', 1);...COMMIT; 

3. 数据有序插入

INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`)    VALUES ('1''userid_1''content_1', 1);INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`)    VALUES ('0''userid_0''content_0', 0);INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`)    VALUES ('2''userid_2''content_2',2); 改成:INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`)    VALUES ('0''userid_0''content_0', 0);INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`)    VALUES ('1''userid_1''content_1', 1);INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`)    VALUES ('2''userid_2''content_2',2);     在性能测试的工作中发生瓶颈的80%是数据库,数据库的80%性能问题集中在sql语句上。所以学好sql语句是很重要的,poptest的测试开发工程师培训中很注重数据库方面的实践,为性能测试中的性能调优打好基础。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,367
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,858