前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL数据insert测试

MySQL数据insert测试

原创
作者头像
胖五斤
修改2022-12-25 20:49:54
1.4K0
修改2022-12-25 20:49:54
举报

测试环境

本地Mac安装的MySQL(8.0.30)服务,性能数据仅作为参考,但对于不同索引情况下的结果,还是能看出有区别。

测试方式

通过存储过程来进行数据insert:

代码语言:txt
复制
delimiter ;;

create procedure idata()

begin

  declare i int;

  set i=1;

  while(i<=500000)do

    insert into t values(i, i, i);

    set i=i+1;

  end while;

end;;

delimiter ;



call idata();

测试情况

1、没主键、没索引

代码语言:txt
复制
drop table if exists t;

create table t (a int, b int, c int);



mysql> call idata();

Query OK, 1 row affected (2 min 58.78 sec)



mysql> select count(\*) from t;

+----------+

| count(\*) |

+----------+

|   500000 |

+----------+

1 row in set (0.02 sec)



mysql> create index ta on t(a);

Query OK, 0 rows affected (1.07 sec)

Records: 0  Duplicates: 0  Warnings: 0

2、没主键、有普通索引

代码语言:txt
复制
drop table if exists t;

create table t (a int, b int, c int);

create index ta on t(a);



mysql> call idata();

Query OK, 1 row affected (3 min 31.30 sec)

3、没主键、有唯一索引

代码语言:txt
复制
drop table if exists t;

create table t (a int, b int, c int);

create unique index ta on t(a);



mysql> call idata();

Query OK, 1 row affected (3 min 49.46 sec)

4、有主键、没索引

代码语言:txt
复制
drop table if exists t;

create table t (a int, b int, c int, primary key(a));



mysql> call idata();

Query OK, 1 row affected (3 min 56.93 sec)

5、有主键、有普通索引

代码语言:txt
复制
drop table if exists t;

create table t (a int, b int, c int, primary key(a));

create index tb on t(b);



mysql> call idata();

Query OK, 1 row affected (4 min 14.70 sec)

6、有主键,有唯一索引

代码语言:txt
复制
drop table if exists t;

create table t (a int, b int, c int, primary key(a));

create unique index tb on t(b);



mysql> call idata();

Query OK, 1 row affected (4 min 30.83 sec)

7、日志刷盘也有影响

代码语言:txt
复制
set global sync\_binlog=0;

set global innodb\_flush\_log\_at\_trx\_commit=0;

设置后提升明显。

有主键,有唯一索引情况下:

代码语言:txt
复制
mysql> call idata();

Query OK, 1 row affected (43.26 sec)

8、手动commit

代码语言:txt
复制
set global sync\_binlog=1;

set global innodb\_flush\_log\_at\_trx\_commit=1;

set autocommit = 0;后再手动触发commit;

代码语言:txt
复制
mysql> call idata();

Query OK, 1 row affected (20.82 sec)



mysql> commit;

Query OK, 0 rows affected (0.28 sec)
代码语言:txt
复制
drop table if exists t;

create table t (a int, b int, c int, primary key(a));

create unique index tb on t(b);

set global sync\_binlog=0;

set global innodb\_flush\_log\_at\_trx\_commit=0;

set autocommit = 0;



mysql> call idata();

Query OK, 1 row affected (21.78 sec)



mysql> commit;

Query OK, 0 rows affected (0.31 sec)

9、alter table t disable keys;

代码语言:txt
复制
mysql> call idata();

Query OK, 1 row affected (4 min 8.54 sec)

测试小结

场景

idata耗时

没主键、没索引

2 min 58.78 sec;idata后创建索引耗时1.07 sec

没主键、有普通索引

3 min 31.30 sec

没主键、有唯一索引

3 min 49.46 sec

有主键、没索引

3 min 56.93 sec

有主键、有普通索引

4 min 14.70 sec

有主键,有唯一索引

4 min 30.83 sec

日志刷盘:不要同步刷

43.26 sec

手动commit

20.82 sec

disable keys

4 min 8.54 sec

唯一索引 比 普通索引 “慢”:

  1. 在插入数据时,change buffer,普通索引直接写入即可, 唯一索引还需要将数据读出,对比是否有冲突。
  2. 建议由业务来保证唯一性。
  3. 归档数据库表,可以改为普通索引,提高写入效率,因为此时数据不会再变化,肯定是唯一的。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 测试环境
  • 测试方式
  • 测试情况
  • 测试小结
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档