修改mysql配置
max_heap_table_size=4000M
innodb_flush_log_at_trx_commit=0
sync_binlog=500
创建测试数据库
create database test_db charset=utf8;
use test_db;
创建数据表
CREATE TABLE `test_innodb` (
`id` INT (11) NOT NULL AUTO_INCREMENT,
`user_id` VARCHAR (20) NOT NULL,
`group_id` INT (11) NOT NULL,
`create_time` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `index_user_id` (`user_id`) USING HASH
) ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
创建随机字符串函数
delimiter $$
CREATE FUNCTION rand_string(n int) RETURNS varchar(255)
begin
declare chars_str varchar(100)
default "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
declare return_str varchar(255) default "";
declare i int default 0;
while i < n do
set return_str=concat(return_str,substring(chars_str,floor(1+rand()*62),1));
set i= i+1;
end while;
return return_str;
end $$
delimiter ;
创建存储过程
delimiter $$
CREATE PROCEDURE `insert_data`(IN n int)
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i <= n ) DO
INSERT into test_innodb (user_id,group_id,create_time ) VALUEs (rand_string(20),FLOOR(RAND() * 100) ,now() );
set i=i+1;
END WHILE;
END $$
delimiter ;
调用存储过程, 插入数据
set @d=now();
call insert_data(1000000);
select timestampdiff(second,@d,now());
==============
测试ON DUPLICATE KEY UPDATE
delimiter $$ CREATE PROCEDURE `insert_update2_data`(IN n int) BEGIN DECLARE i INT DEFAULT 1; WHILE (i <= n ) DO INSERT into test_innodb (id,user_id,group_id,create_time ) VALUES (FLOOR(RAND() * 100000),CONCAT('a#',rand_string(10)),FLOOR(RAND() * 100) ,now() ) ON DUPLICATE KEY UPDATE user_id=CONCAT('u#',rand_string(10)), group_id=FLOOR(RAND() * 100),create_time=now() ; set i=i+1; END WHILE; END $$ delimiter ; INSERT into test_innodb (id,user_id,group_id,create_time ) VALUES (FLOOR(RAND() * 100000),CONCAT('a#',rand_string(10)),FLOOR(RAND() * 100) ,now() ) ON DUPLICATE KEY UPDATE user_id=CONCAT('u#',rand_string(10)), group_id=FLOOR(RAND() * 100),create_time=now() ; INSERT into test_innodb (id,user_id,group_id,create_time ) VALUES (11100000,CONCAT('a#',rand_string(10)),FLOOR(RAND() * 100) ,now()) INSERT into test_innodb (id,user_id,group_id,create_time ) VALUES (11100001,CONCAT('a#',rand_string(10)),FLOOR(RAND() * 100) ,now()); select now(); set @d=now(); call insert_update2_data(10000); select timestampdiff(second,@d,now()); select now(); SELECT * FROM test_innodb T WHERE T.user_id like '_#%';