大批量插入数据,却不会优化怎么办

1、大批量插入数据优化
(1)对于myisam存储引擎的表,可以使用:disable keys 和 enable keys 用来打开或者关闭 myisam 表非唯一索引的更新。
alter table tbl_name disable keys;
loading the data
alter table tbl_name enable keys;
(2)对于innodb引擎,有以下几种优化措施:
① 导入的数据按照主键的顺序保存:这是因为innodb引擎表示按照主键顺序保存的,如果能将插入的数据提前按照排序好自然能省去很多时间。
比如bulk_insert.txt文件是以表user主键的顺序存储的,导入的时间为15.23秒
mysql》 load data infile ‘mysql/bulk_insert.txt’ into table user;
query ok, 126732 rows affected (15.23 sec)
records: 126732 deleted: 0 skipped: 0 warnings: 0
没有按照主键排序的话,时间为:26.54秒
mysql》 load data infile ‘mysql/bulk_insert.txt’ into table user;
query ok, 126732 rows affected (26.54 sec)
records: 126732 deleted: 0 skipped: 0 warnings: 0
② 导入数据前执行set unique_checks=0,关闭唯一性校验,带导入之后再打开设置为1:校验会消耗时间,在数据量大的情况下需要考虑。
③ 导入前设置set autocommit=0,关闭自动提交,导入后结束再设置为1:这是因为自动提交会消耗部分时间与资源,虽然消耗不是很大,但是在数据量大的情况下还是得考虑。
2、insert的优化
(1)尽量使用多个值表的 insert 语句,这种方式将大大缩减客户端与数据库之间的连接、关闭等消耗。(同一客户的情况下),即:
insert into tablename values(1,2),(1,3),(1,4)
实验:插入8条数据到user表中(使用navicat客户端工具)
insert into user values(1,‘test’,replace(uuid(),‘-’,‘’));
insert into user values(2,‘test’,replace(uuid(),‘-’,‘’));
insert into user values(3,‘test’,replace(uuid(),‘-’,‘’));
insert into user values(4,‘test’,replace(uuid(),‘-’,‘’));
insert into user values(5,‘test’,replace(uuid(),‘-’,‘’));
insert into user values(6,‘test’,replace(uuid(),‘-’,‘’));
insert into user values(7,‘test’,replace(uuid(),‘-’,‘’));
insert into user values(8,‘test’,replace(uuid(),‘-’,‘’));
得到反馈:
[sql] insert into user values(1,‘test’,replace(uuid(),‘-’,‘’));
受影响的行: 1
时间: 0.033s
[sql]
insert into user values(2,‘test’,replace(uuid(),‘-’,‘’));
受影响的行: 1
时间: 0.034s
[sql]
insert into user values(3,‘test’,replace(uuid(),‘-’,‘’));
受影响的行: 1
时间: 0.056s
[sql]
insert into user values(4,‘test’,replace(uuid(),‘-’,‘’));
受影响的行: 1
时间: 0.008s
[sql]
insert into user values(5,‘test’,replace(uuid(),‘-’,‘’));
受影响的行: 1
时间: 0.008s
[sql]
insert into user values(6,‘test’,replace(uuid(),‘-’,‘’));
受影响的行: 1
时间: 0.024s
[sql]
insert into user values(7,‘test’,replace(uuid(),‘-’,‘’));
受影响的行: 1
时间: 0.004s
[sql]
insert into user values(8,‘test’,replace(uuid(),‘-’,‘’));
受影响的行: 1
时间: 0.004s
总共的时间为0.171秒,接下来使用多值表形式:
insert into user values
(9,‘test’,replace(uuid(),‘-’,‘’)),
(10,‘test’,replace(uuid(),‘-’,‘’)),
(11,‘test’,replace(uuid(),‘-’,‘’)),
(12,‘test’,replace(uuid(),‘-’,‘’)),
(13,‘test’,replace(uuid(),‘-’,‘’)),
(14,‘test’,replace(uuid(),‘-’,‘’)),
(15,‘test’,replace(uuid(),‘-’,‘’)),
(16,‘test’,replace(uuid(),‘-’,‘’));
得到反馈:
[sql] insert into user values
(9,‘test’,replace(uuid(),‘-’,‘’)),
(10,‘test’,replace(uuid(),‘-’,‘’)),
(11,‘test’,replace(uuid(),‘-’,‘’)),
(12,‘test’,replace(uuid(),‘-’,‘’)),
(13,‘test’,replace(uuid(),‘-’,‘’)),
(14,‘test’,replace(uuid(),‘-’,‘’)),
(15,‘test’,replace(uuid(),‘-’,‘’)),
(16,‘test’,replace(uuid(),‘-’,‘’));
受影响的行: 8
时间: 0.038s
得到时间为0.038,这样一来可以很明显节约时间优化sql
(2)如果在不同客户端插入很多行,可使用insert delayed语句得到更高的速度,dellayed含义是让insert语句马上执行,其实数据都被放在内存的队列中。并没有真正写入磁盘。low_priority刚好相反。
(3)将索引文件和数据文件分在不同的磁盘上存放(innodb引擎是在同一个表空间的)。
(4)如果批量插入,则可以增加bluk_insert_buffer_size变量值提供速度(只对myisam有用)
(5)当从一个文本文件装载一个表时,使用load data infile,通常比insert语句快20倍。
3、group by的优化
在默认情况下,mysql中的group by语句会对其后出现的字段进行默认排序(非主键情况),就好比我们使用order by col1,col2,col3…所以我们在后面跟上具有相同列(与group by后出现的col1,col2,col3…相同)order by子句并没有影响该sql的实际执行性能。
那么就会有这样的情况出现,我们对查询到的结果是否已经排序不在乎时,可以使用order by null禁止排序达到优化目的。下面使用explain命令分析sql。java知音公众号内回复“面试题聚合”,送你一份面试题宝典
在user_1中执行select id, sum(money) form user_1 group by name时,会默认排序(注意group by后的column是非index才会体现group by的排序,如果是primary key,那之前说过了innodb默认是按照主键index排好序的)
mysql》 select*from user_1;
+----+----------+-------+
| id | name | money |
+----+----------+-------+
| 1 | zhangsan | 32 |
| 2 | lisi | 65 |
| 3 | wangwu | 44 |
| 4 | lijian | 100 |
+----+----------+-------+
4 rows in set
不禁止排序,即不使用order by null时:有明显的using filesort。
当使用order by null禁止排序后,using filesort不存在
4、order by 的优化
mysql可以使用一个索引来满足order by 子句的排序,而不需要额外的排序,但是需要满足以下几个条件:
(1)where 条件和oredr by 使用相同的索引:即key_part1与key_part2是复合索引,where中使用复合索引中的key_part1
select*from user where key_part1=1 order by key_part1 desc, key_part2 desc;
(2)而且order by顺序和索引顺序相同:
select*from user order by key_part1, key_part2;
(3)并且要么都是升序要么都是降序:
select*from user order by key_part1 desc, key_part2 desc;
但以下几种情况则不使用索引:
(1)order by中混合asc和desc:
select*from user order by key_part1 desc, key_part2 asc;
(2)查询行的关键字与order by所使用的不相同,即where 后的字段与order by 后的字段是不一样的
select*from user where key2 = ‘xxx’ order by key1;
(3)order by对不同的关键字使用,即order by后的关键字不相同
select*from user order by key1, key2;
5、or的优化
当mysql使用or查询时,如果要利用索引的话,必须每个条件列都使独立索引,而不是复合索引(多列索引),才能保证使用到查询的时候使用到索引。
比如我们新建一张用户信息表user_info
mysql》 select*from user_info;
+---------+--------+----------+-----------+
| user_id | idcard | name | address |
+---------+--------+----------+-----------+
| 1 | 111111 | zhangsan | kunming |
| 2 | 222222 | lisi | beijing |
| 3 | 333333 | wangwu | shanghai |
| 4 | 444444 | lijian | guangzhou |
+---------+--------+----------+-----------+
4 rows in set
之后创建ind_name_id(user_id, name)复合索引、id_index(id_index)独立索引,idcard主键索引三个索引。
mysql》 show index from user_info;
+-----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| table | non_unique | key_name | seq_in_index | column_name | collation | cardinality | sub_part | packed | null | index_type | comment | index_comment |
+-----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user_info | 0 | primary | 1 | idcard | a | 4 | null | null | | btree | | |
| user_info | 1 | ind_name_id | 1 | user_id | a | 4 | null | null | | btree | | |
| user_info | 1 | ind_name_id | 2 | name | a | 4 | null | null | yes | btree | | |
| user_info | 1 | id_index | 1 | user_id | a | 4 | null | null | | btree | | |
+-----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set
测试一:or连接两个有单独索引的字段,整个sql查询才会用到索引(index_merge),并且我们知道or实际上是把每个结果最后union一起的。
mysql》 explain select*from user_info where user_id=1 or idcard=‘222222’;
+----+-------------+-----------+------------+-------------+------------------------------+---------------------+---------+------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
+----+-------------+-----------+------------+-------------+------------------------------+---------------------+---------+------+------+----------+----------------------------------------------------+
| 1 | simple | user_info | null | index_merge | primary,ind_name_id,id_index | ind_name_id,primary | 4,62 | null | 2 | 100 | using sort_union(ind_name_id,primary); using where |
+----+-------------+-----------+------------+-------------+------------------------------+---------------------+---------+------+------+----------+----------------------------------------------------+
1 row in set
测试二:or使用复合索引的字段name,与没有索引的address,整个sql都是all全表扫描的
mysql》 explain select*from user_info where name=‘zhangsan’ or address=‘beijing’;
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | simple | user_info | null | all | null | null | null | null | 4 | 43.75 | using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set
交换or位置并且使用另外的复合索引的列,也是all全表扫描:
mysql》 explain select*from user_info where address=‘beijing’ or user_id=1;
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
| 1 | simple | user_info | null | all | ind_name_id,id_index | null | null | null | 4 | 43.75 | using where |
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
1 row in set
6、优化嵌套查询
使用嵌套查询有时候可以使用更有效的join连接代替,这是因为mysql中不需要在内存中创建临时表完成select子查询与主查询两部分查询工作。但是并不是所有的时候都成立,最好是在on关键字后面的列有索引的话,效果会更好!
比如在表major中major_id是有索引的:
select * from student u left join major m on u.major_id=m.major_id where m.major_id is null;
而通过嵌套查询时,在内存中创建临时表完成select子查询与主查询两部分查询工作,会有一定的消耗
select * from student u where major_id not in (select major_id from major);
7、使用sql提示
sql提示(sql hint)是优化数据库的一个重要手段,就是往sql语句中加入一些人为的提示来达到优化目的。下面是一些常用的sql提示:
(1)use index:使用use index是希望mysql去参考索引列表,就可以让mysql不需要考虑其他可用索引,其实也就是possible_keys属性下参考的索引值
mysql》 explain select* from user_info use index(id_index,ind_name_id) where user_id》0;
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
| 1 | simple | user_info | null | all | ind_name_id,id_index | null | null | null | 4 | 100 | using where |
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
1 row in set
mysql》 explain select* from user_info use index(id_index) where user_id》0;
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | simple | user_info | null | all | id_index | null | null | null | 4 | 100 | using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set
(2)ignore index忽略索引
我们使用user_id判断,用不到其他索引时,可以忽略索引。即与use index相反,从possible_keys中减去不需要的索引,但是实际环境中很少使用。
mysql》 explain select* from user_info ignore index(primary,ind_name_id,id_index) where user_id》0;
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | simple | user_info | null | all | null | null | null | null | 4 | 33.33 | using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set
(3)force index强制索引
比如where user_id 》 0,但是user_id在表中都是大于0的,自然就会进行all全表搜索,但是使用force index虽然执行效率不是最高(where user_id 》 0条件决定的)但mysql还是使用索引。
mysql》 explain select* from user_info where user_id》0;
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
| 1 | simple | user_info | null | all | ind_name_id,id_index | null | null | null | 4 | 100 | using where |
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
1 row in set
之后强制使用独立索引id_index(user_id):
mysql》 explain select* from user_info force index(id_index) where user_id》0;
+----+-------------+-----------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
+----+-------------+-----------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
| 1 | simple | user_info | null | range | id_index | id_index | 4 | null | 4 | 100 | using index condition |
+----+-------------+-----------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
1 row in set
总结
(1)很多时候数据库的性能是由于不合适(是指效率不高,可能会导致锁表等)的sql语句造成,本篇博文只是介绍简单的sql优化
(2)其中有些优化在真正开发中是用不到的,但是一旦出问题性能下降的时候需要去一一分析。

云计算会带来哪些变化 云计算有哪些应用
华为平板M5 10.8英寸与iPad争雌雄
汽车制动系统组成和原理
索尼发布两款旗舰OLED电视机,搭载新一代图像处理芯片,重新定义全新“画谛系列”
AIGC技术的采用是是否代表人工智能创意生产时代已来临?
大批量插入数据,却不会优化怎么办
强大蒸水洗,华帝飓拢吸油烟机常用常新
印制电路板之前需要做什么准备
LED驱动器的类别及其照明散热控制浅谈
村田制作所入选“供应商参与度领先企业” 博世达成新能源汽车电池合作
L211PCB怎么布线
BUFF传送连的使用调试
华为Mate 50 RS保时捷的配件价格公布
USB PD规范即将商用 可提供100W电力
关于航空气象数据库系统通信分系统的解析
编写一个以C语言为基础的DSP程序
12Vdc的荧光灯驱动电路-12VDC Fluorescen
USB3.0接口保护ESD应用
华为Mate60Pro发布开售,华为麒麟芯片回归
Nordic Semiconductor助力低功耗蓝牙传感器信标 在工业和家居应用中收集实时温度和湿度数据