转载

MySQL大量数据入库的性能比较(分区)

测试程序还用之前的
http://blog.itpub.net/29254281/viewspace-1841299/

这次测试,使用的是家里的电脑,性能比单位工作的电脑配置要好一些.

MySQL配置
innodb_buffer_pool_size=512m
innodb_flush_log_at_trx_commit =0
sync_binlog=0
innodb_support_xa=0
log_bin=master

版本 5.6.14

每次测试,Insert 200w记录.

1.使用Load File接口,普通表,4个索引,每100个记录提交一次

  1. create table chat_message(
  2.     id bigint primary key auto_increment,
  3.     src_userid bigint not null,
  4.     target_userid bigint not null,
  5.     message varchar(200),
  6.     ts timestamp not null default current_timestamp,
  7.     s1 int,
  8.     s2 int,
  9.     s3 int,
  10.     s4 int
  11. );
  12. create index inx_1 on chat_message(src_userid,target_userid,s1,ts);
  13. create index inx_2 on chat_message(src_userid,target_userid,s2,ts);
  14. create index inx_3 on chat_message(src_userid,target_userid,s3,ts);
  15. create index inx_4 on chat_message(src_userid,target_userid,s4,ts);
测试结果
每秒 10638 Insert

2.使用Load File接口,使用日期的范围分区

  1. create table chat_message(
  2.     id bigint auto_increment,
  3.     src_userid bigint not null,
  4.     target_userid bigint not null,
  5.     message varchar(200),
  6.     ts timestamp not null default current_timestamp,
  7.     s1 int,
  8.     s2 int,
  9.     s3 int,
  10.     s4 int,
  11.     primary key (id,ts)
  12. )
  13. partition by range(UNIX_TIMESTAMP(ts))
  14. (
  15.     partition p1 VALUES LESS THAN(UNIX_TIMESTAMP('2015-10-01 00:00:00')),
  16.     partition p2 VALUES LESS THAN(UNIX_TIMESTAMP('2015-11-01 00:00:00')),
  17.     partition p3 VALUES LESS THAN(UNIX_TIMESTAMP('2015-12-01 00:00:00')),
  18.     partition p4 VALUES LESS THAN(UNIX_TIMESTAMP('2016-01-01 00:00:00')),
  19.     partition p5 VALUES LESS THAN(UNIX_TIMESTAMP('2016-02-01 00:00:00')),
  20.     partition p6 VALUES LESS THAN(UNIX_TIMESTAMP('2016-03-01 00:00:00')),
  21.     partition p7 VALUES LESS THAN(UNIX_TIMESTAMP('2016-04-01 00:00:00')),
  22.     partition p8 VALUES LESS THAN(UNIX_TIMESTAMP('2016-05-01 00:00:00')),
  23.     partition p9 VALUES LESS THAN(UNIX_TIMESTAMP('2016-06-01 00:00:00')),
  24.     partition p10 VALUES LESS THAN(UNIX_TIMESTAMP('2016-07-01 00:00:00')),
  25.     partition p11 VALUES LESS THAN(UNIX_TIMESTAMP('2016-08-01 00:00:00')),
  26.     partition p12 VALUES LESS THAN(UNIX_TIMESTAMP('2016-09-01 00:00:00'))
  27. );

  28. create index inx_1 on chat_message(src_userid,target_userid,s1,ts);
  29. create index inx_2 on chat_message(src_userid,target_userid,s2,ts);
  30. create index inx_3 on chat_message(src_userid,target_userid,s3,ts);
  31. create index inx_4 on chat_message(src_userid,target_userid,s4,ts);
测试结果
每秒 10989 Insert

3.在日期范围分区基础上,增加4个子分区

  1. create table chat_message(
  2.     id bigint auto_increment,
  3.     src_userid bigint not null,
  4.     target_userid bigint not null,
  5.     message varchar(200),
  6.     ts timestamp not null default current_timestamp,
  7.     s1 int,
  8.     s2 int,
  9.     s3 int,
  10.     s4 int,
  11.     primary key (id,ts,src_userid)
  12. )
  13. partition by range(UNIX_TIMESTAMP(ts))
  14. subpartition by key(src_userid)
  15. subpartitions 4(
  16.     partition p201506 VALUES LESS THAN(UNIX_TIMESTAMP('2015-10-01 00:00:00')),
  17.     partition p201507 VALUES LESS THAN(UNIX_TIMESTAMP('2015-11-01 00:00:00')),
  18.     partition p201508 VALUES LESS THAN(UNIX_TIMESTAMP('2015-12-01 00:00:00'))
  19. );

  20. create index inx_1 on chat_message(src_userid,target_userid,s1,ts);
  21. create index inx_2 on chat_message(src_userid,target_userid,s2,ts);
  22. create index inx_3 on chat_message(src_userid,target_userid,s3,ts);
  23. create index inx_4 on chat_message(src_userid,target_userid,s4,ts);

测试结果
每秒 8810 Insert 

3.在日期范围分区基础上,增加16个子分区

每秒 6688 Insert

4.在日期范围分区基础上,增加64个子分区


每秒 8368 Insert



结论:
1.在日期的范围分区上,再增加Hash分区,显著降低每秒Insert数量
2.随着数据量的增加,每秒Insert数量显著下降. 比如表中已经有200w数据,再增加200w数据,每秒Insert从1w左右直接掉到1k左右.




正文到此结束
Loading...