数据库——MySQL某系统压测专用

-- ---------------
-- 数据库测试专用
-- ---------------

# 用于数据库压力测试、性能测试、安全检测等场景

use bbs;

set global log_bin_trust_function_creators = TRUE;

/*
指定长度随机字符串生成函数
 */
delimiter //
create function rand_string(length int)
    returns varchar(255)
begin
    -- 声明
    # 数据字典
    declare char_str varchar(100) default 'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ';
    # 拼接字符串
    declare return_str varchar(255) default '';
    -- 赋值
    # ① 初始化条件
    declare i int default 1;
    # ② 循环条件
    while i <= length
        do
            # ③ 循环体(字符串函数的应用)
            set return_str = concat(return_str, substring(char_str, floor(1 + rand() * 52), 1));
            # ④ 迭代条件
            set i = i + 1;
        end while;
    -- 使用
    return return_str;
end //
delimiter ;

# 测试
select rand_string(20);

/*
 指定范围随机整数生成函数
 */
delimiter //
create function rand_number(upper int, lower int)
    returns int
begin
    -- 声明
    declare num int default 0;
    -- 赋值
    # floor函数:返回小于或等于某个值的最大整数(这里返回随机数范围upper<=number<=(lower+upper-1))
    set num = floor(upper + rand() * lower);
    -- 使用
    return num;
end //
delimiter ;

# 测试
select rand_number(0, 2);

/*
实际应用:使用存储过程生成海量数据
*/
delimiter //
create procedure insert_tremendous_data(in start_id varchar(20), in max_num varchar(20))
begin
    -- 赋值
    # ① 初始化条件
    declare i int default 0;
    # 设置不自动提交
    set autocommit = 0;
    repeat
        /* ③ 循环体 */
        insert into bbs_article (art_id, type_id, user_id, art_title, art_abstract, art_content,
                                 art_content_html, art_cover, art_push_time, art_likes, art_comment,
                                 art_daily_heat, update_by)
        values ((start_id + i),
                (start_id + i),
                (start_id + i),
                rand_string(10),
                rand_string(100),
                rand_string(100),
                rand_string(100),
                concat('http:/', '/', rand_string(4), '/', rand_string(4), '.png'),
                current_timestamp(),
                rand_number(1, 9999),
                rand_number(1, 9999),
                rand_number(1, 9999),
                current_timestamp());
        /* ④ 迭代条件 */
        set i = i + 1;
    until
        # ② 循环条件
        i = max_num
        end repeat;
    # commit整体提交所有sql语句,提高效率
    commit;
end //
delimiter ;

/*
 测试结果:
    big_data_studio> call insert_tremendous_data(1000, 1)
    [2022-11-07 08:17:25] 在 229 ms 内完成
    big_data_studio> call insert_tremendous_data(1001, 100)
    [2022-11-07 08:17:41] 在 219 ms 内完成
    big_data_studio> call insert_tremendous_data(1101, 10000)
    [2022-11-07 08:19:07] 在 9 s 505 ms 内完成
    big_data_studio> call insert_tremendous_data(11101, 100000)
    [2022-11-07 08:21:02] 在 1 m 28 s 790 ms 内完成
 */
call insert_tremendous_data(1001, 5000);

drop procedure insert_tremendous_data;

# 公告测试数据
/*
insert into ow_notice (id, notice_title, notice_abstract, notice_content, content_html,
                       notice_cover, push_time, create_name, update_name, update_time, star)
values ((start_id + i),
        rand_string(10),
        rand_string(100),
        rand_string(100),
        rand_string(100),
        concat('http:/', '/', rand_string(4), '/', rand_string(4), '.png'),
        current_timestamp(),
        rand_string(4),
        rand_string(4),
        current_timestamp(),
        rand_number(1, 9999));
 */

# 公告类型关系表测试数据
/*
 insert into ow_notice_type (id, notice_id, type_id)
values ((start_id + i),
        (start_id + i),
        rand_number(1, 4));
 */

# 荣誉墙测试数据
/*
 insert into ow_honor_wall (id, hw_title, hw_picture, hw_introduction, hw_push_time,
                           create_name, update_name, update_time, star)
values ((start_id + i),
        rand_string(10),
        concat('http:/', '/', rand_string(4), '/', rand_string(4), '.png'),
        rand_string(15),
        current_timestamp(),
        rand_string(4),
        rand_string(4),
        current_timestamp(),
        rand_number(1, 9999));
 */

# 文章
/*
 insert into bbs_article (art_id, type_id, user_id, art_title, art_abstract, art_content,
                         art_content_html, art_cover, art_push_time, art_likes, art_comment,
                         art_daily_heat, update_by)
values ((start_id + i),
        (start_id + i),
        (start_id + i),
        rand_string(10),
        rand_string(100),
        rand_string(100),
        rand_string(100),
        concat('http:/', '/', rand_string(4), '/', rand_string(4), '.png'),
        current_timestamp(),
        rand_number(1, 9999),
        rand_number(1, 9999),
        rand_number(1, 9999),
        current_timestamp());
 */

你可能感兴趣的:(MySQL,数据库,mysql,android,笔记,经验分享,学习,开发语言)