You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

107 lines
3.5 KiB

--测试表
create table test_enf_demo
(
id bigint not null comment '主键'
primary key,
enf_text varchar(255) null comment '文本',
enf_content longtext null comment '长文本',
tenant_id varchar(20) default '000000' null comment '租户编号',
dept_id bigint null comment '部门id',
user_id bigint null comment '用户id',
order_num int default 0 null comment '排序号',
test_key varchar(255) null comment 'key键',
value varchar(255) null comment '',
version int default 0 null comment '版本',
create_dept varchar(100) null comment '创建部门',
bureau_dept_id varchar(100) null comment '执法部门ID',
create_time datetime null comment '创建时间',
create_by bigint null comment '创建人',
update_time datetime null comment '更新时间',
update_by bigint null comment '更新人',
del_flag int default 0 null comment '删除标志'
) comment 'enf测试单表';
-- 为 test_enf_demo 表生成 10000 条测试数据
-- 使用存储过程批量插入
-- 为 test_enf_demo 表生成 100000 条测试数据
-- bureau_dept_id 从 efcode_virtual_unit 表的 sys_dept_id 中随机选取
DROP PROCEDURE IF EXISTS generate_test_enf_demo_data;
DELIMITER $$
CREATE PROCEDURE generate_test_enf_demo_data()
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE batch_size INT DEFAULT 1000;
DECLARE current_batch INT DEFAULT 0;
DECLARE v_dept_count INT DEFAULT 0;
-- 创建临时表缓存 sys_dept_id 列表,避免每次子查询
DROP TEMPORARY TABLE IF EXISTS tmp_dept_ids;
CREATE TEMPORARY TABLE tmp_dept_ids (
row_num INT AUTO_INCREMENT PRIMARY KEY,
sys_dept_id VARCHAR(100)
);
INSERT INTO tmp_dept_ids (sys_dept_id)
SELECT sys_dept_id FROM efcode_virtual_unit WHERE sys_dept_id IS NOT NULL;
SELECT COUNT(*) INTO v_dept_count FROM tmp_dept_ids;
-- 关闭自动提交,提升插入性能
SET autocommit = 0;
WHILE i <= 100000 DO
INSERT INTO test_enf_demo (
id, enf_text, enf_content, tenant_id, dept_id, user_id,
order_num, test_key, value, version,
create_dept, bureau_dept_id, create_time, create_by, update_time, update_by, del_flag
) VALUES (
i,
CONCAT('文本_', i),
CONCAT('这是第', i, '条测试长文本内容,用于测试数据填充。'),
'000000',
100 + FLOOR(RAND() * 50),
1000 + FLOOR(RAND() * 500),
i,
CONCAT('KEY_', LPAD(i, 5, '0')),
CONCAT('VAL_', LPAD(i, 5, '0')),
0,
CONCAT('部门_', FLOOR(RAND() * 20)),
(SELECT sys_dept_id FROM tmp_dept_ids WHERE row_num = 1 + FLOOR(RAND() * v_dept_count) LIMIT 1),
DATE_ADD('2024-01-01', INTERVAL FLOOR(RAND() * 730) DAY),
1,
NOW(),
1,
0
);
-- 每 1000 条提交一次
SET current_batch = current_batch + 1;
IF current_batch >= batch_size THEN
COMMIT;
SET current_batch = 0;
END IF;
SET i = i + 1;
END WHILE;
-- 提交剩余数据
COMMIT;
SET autocommit = 1;
-- 清理临时表
DROP TEMPORARY TABLE IF EXISTS tmp_dept_ids;
END$$
DELIMITER ;
-- 执行存储过程
CALL generate_test_enf_demo_data();
-- 清理存储过程
DROP PROCEDURE IF EXISTS generate_test_enf_demo_data;
-- 验证数据量
SELECT COUNT(*) AS total_count FROM test_enf_demo;