• mysql系列---【如何利用存储过程,造10万条测试数据?】


    背景:测试的时候,没有数据,自己一个一个造,很是麻烦,再搞个java接口,似乎也不太方便,这里就可以尝试一下下面这种做法,利用mysql的存储过程来造数据。

    示例代码:

    delimiter $$ 
    drop procedure if exists generate;
    CREATE DEFINER=`root`@`localhost` PROCEDURE `generate`( IN num INT )
    BEGIN
        DECLARE chars VARCHAR ( 100 ) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        DECLARE name VARCHAR ( 10 ) DEFAULT '';
        DECLARE hobby VARCHAR ( 25 ) DEFAULT '';
        DECLARE id INT UNSIGNED;
        DECLARE len INT;
        
        SET id = 1;
        DELETE FROM person;
        WHILE id <= num DO 
            SET len = FLOOR( 1 + RAND()* 10 ); 
            SET fname = ''; 
            WHILE len > 0 DO
                SET name = CONCAT(name,substring( chars, FLOOR( 1 + RAND()* 62 ), 1 ));
                SET len = len - 1;
            END WHILE;
            SET len = FLOOR( 1+RAND ()* 25 );
            SET hobby = '';
            WHILE len > 0 DO
                SET hobby = CONCAT(hobby,SUBSTR( chars, FLOOR( 1 + RAND()* 62 ), 1 ));
                SET len = len - 1;
            END WHILE;
            INSERT INTO person VALUES (id,name,hobby,FLOOR( RAND()* 100 ),FLOOR( RAND()* 2 ));
            SET id = id + 1;
        END WHILE;
    END $$
    -- 停掉事务 
    set autocommit = 0; 
    -- 调用存储过程 
    call generate(1000000); -- 重启事务 
    set autocommit = 1;
    愿你走出半生,归来仍是少年!
  • 相关阅读:
    Spring Cloud云架构
    Spring Cloud云架构
    Spring Cloud云架构
    Spring Cloud云架构
    Spring Cloud Consul
    Spring Cloud Eureka
    构建Spring Cloud微服务分布式云架构
    数据库三范式
    redis3.0.0 集群安装详细步骤
    sql优化的几种方法
  • 原文地址:https://www.cnblogs.com/hujunwei/p/13900016.html
Copyright © 2020-2023  润新知