mysql快速生成批量测试数据

mysql快速生成批量测试数据

参考资料:

https://blog.csdn.net/oahz4699092zhao/article/details/53332148

-- 创建一个临时内存表
set global log_bin_trust_function_creators=1;
DROP TABLE IF EXISTS `vote_recordss_memory`;
CREATE TABLE `vote_recordss_memory` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `user_id` varchar(20) NOT NULL DEFAULT '',
    `vote_num` int(10) unsigned NOT NULL DEFAULT '0',
    `group_id` int(10) unsigned NOT NULL DEFAULT '0',
    `status` tinyint(2) unsigned NOT NULL DEFAULT '1',
    `create_time` datetime NOT NULL DEFAULT '1971-01-01 01:01:01',
    PRIMARY KEY (`id`),
    KEY `index_user_id` (`user_id`) USING HASH
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-- 创建一个普通表,用作模拟大数据的测试用例
DROP TABLE IF EXISTS `vote_records`;
CREATE TABLE `vote_records` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `user_id` varchar(20) NOT NULL DEFAULT '' COMMENT '用户Id',
    `vote_num` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '投票数',
    `group_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户组id 0-未激活用户 1-普通用户 2-vip用户 3-管理员用户',
    `status` tinyint(2) unsigned NOT NULL DEFAULT '1' COMMENT '状态 1-正常 2-已删除',
    `create_time` datetime  NOT NULL DEFAULT '1971-01-01 01:01:01' COMMENT '创建时间',
    PRIMARY KEY (`id`),
    KEY `index_user_id` (`user_id`) USING HASH COMMENT '用户ID哈希索引'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='投票记录表';
-- 为了数据的随机性和真实性,我们需要创建一个可生成长度为n的随机字符串的函数。
-- 创建生成长度为n的随机字符串的函数
DELIMITER // -- 修改MySQL delimiter:'//'
DROP FUNCTION IF EXISTS `rand_strings` //
SET NAMES utf8 //
CREATE FUNCTION `rand_strings` (n INT) RETURNS VARCHAR(255) CHARSET 'utf8'
BEGIN 
    DECLARE char_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(char_str, FLOOR(1 + RAND()*62), 1));
        SET i = i+1;
    END WHILE;
    RETURN return_str;
END //
-- 为了操作方便,我们再创建一个插入数据的存储过程
-- 创建插入数据的存储过程
DROP PROCEDURE IF EXISTS `insert_vote_recordss_memory` //
CREATE PROCEDURE `insert_vote_recordss_memory`(IN n INT)
BEGIN
    DECLARE i INT DEFAULT 1;
    DECLARE vote_num INT DEFAULT 0;
    DECLARE group_id INT DEFAULT 0;
    DECLARE status TINYINT DEFAULT 1;
    WHILE i < n DO
        SET vote_num = FLOOR(1 + RAND() * 10000);
        SET group_id = FLOOR(0 + RAND()*3);
        SET status = FLOOR(1 + RAND()*2);
        INSERT INTO `vote_recordss_memory` VALUES (NULL, rand_strings(20), vote_num, group_id, status, NOW());
        SET i = i + 1;
    END WHILE;
END //
DELIMITER ;  -- 改回默认的 MySQL delimiter:';'
-- 开始执行存储过程,等待生成数据(100W条)
-- 调用存储过程 生成100W条数据
CALL insert_vote_recordss_memory(10000000);
-- 查询内存表已生成记录(为了下步测试,目前仅生成了x条)
SELECT count(*) FROM `vote_recordss_memory`;
-- 把数据从内存表插入到普通表中(100w条数据ys就插入完了)
INSERT INTO vote_records SELECT * FROM `vote_recordss_memory`;
-- 查询普通表已的生成记录
SELECT count(*) FROM `vote_records`;
# 实时监控mysql的工具
https://blog.csdn.net/u013820054/article/details/54022456

---------+-------mysql-status-------+-----threads-----+-----slow-----+---bytes---+---------locks---------- time | QPS TPS ins upd del| run con cre cac| sql tmp Dtmp| recv send| lockI lockW openT openF ---------+--------------------------+-----------------+--------------+-----------+------------------------ 23:15:03 | 0 3738 3738 0 0| 3 3 0 0| 0 1 0| 0K 8K| 1 0 121 27 23:15:04 | 0 3781 3781 0 0| 3 3 0 0| 0 1 0| 0K 8K| 1 0 121 27 23:15:05 | 0 3733 3733 0 0| 3 3 0 0| 0 1 0| 0K 8K| 1 0 121 27 23:15:06 | 0 3737 3737 0 0| 3 3 0 0| 0 1 0| 0K 8K| 1 0 121 27 23:15:07 | 0 3576 3576 0 0| 3 3 0 0| 0 1 0| 0K 8K| 1 0 121 27 23:15:08 | 0 3589 3589 0 0| 2 3 1 1| 1 1 0| 0K 8K| 1 0 121 27 23:15:09 | 0 3548 3548 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:10 | 0 3791 3791 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:11 | 0 3861 3861 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:12 | 0 3892 3892 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:13 | 0 3857 3857 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:14 | 0 3900 3900 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:15 | 0 3701 3701 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:16 | 0 3856 3856 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:17 | 0 3166 3166 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:18 | 0 3865 3865 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:19 | 1 3853 3853 0 0| 3 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:20 | 0 2864 2864 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:21 | 0 2927 2927 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27 23:15:22 | 0 3820 3820 0 0| 2 3 0 1| 0 1 0| 0K 8K| 1 0 121 27

猜你喜欢

转载自www.cnblogs.com/bjx2020/p/9727898.html
今日推荐