參考:
http://blog.itpub.net/29254281/viewspace-1686302/
準(zhǔn)備環(huán)境
1.創(chuàng)建數(shù)字輔助表
create table nums(id int not null primary key);
delimiter $$
create procedure pFastCreateNums(cnt int)
begin
declare s int default 1;
truncate table nums;
insert into nums select s;
while s*2<=cnt do
insert into nums select id+s from nums;
set s=s*2;
end while;
end $$
delimiter ;
call pFastCreateNums(1000000);
數(shù)字輔助表的行數(shù)決定最后能生成的表行數(shù)的最大值.
2.創(chuàng)建生成隨機(jī)字符的函數(shù)
-
DROP FUNCTION IF EXISTS rand_string;
-
delimiter //
-
CREATE FUNCTION rand_string(l_num int UNSIGNED,l_type tinyint UNSIGNED)
-
RETURNS varchar(2000)
-
BEGIN
-
-- Function : rand_string
-
-- Author : dbachina#dbachina.com
-
-- Date : 2010/5/30
-
-- l_num : The length of random string
-
-- l_type: The string type
-
-- 1.0-9
-
-- 2.a-z
-
-- 3.A-Z
-
-- 4.a-zA-Z
-
-- 5.0-9a-zA-Z
-
-- :
-
-- MySQL> select rand_string(12,5) random_string;
-
-- +---------------+
-
-- | random_string |
-
-- +---------------+
-
-- | 3KzGJCUJUplw |
-
-- +---------------+
-
-- 1 row in set (0.00 sec)
-
DECLARE i int UNSIGNED DEFAULT 0;
-
DECLARE v_chars varchar(64) DEFAULT '0123456789';
-
DECLARE result varchar (2000) DEFAULT '';
-
-
IF l_type = 1 THEN
-
SET v_chars = '0123456789';
-
ELSEIF l_type = 2 THEN
-
SET v_chars = 'abcdefghijklmnopqrstuvwxyz';
-
ELSEIF l_type = 3 THEN
-
SET v_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
-
ELSEIF l_type = 4 THEN
-
SET v_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
-
ELSEIF l_type = 5 THEN
-
SET v_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
-
ELSE
-
SET v_chars = '0123456789';
-
END IF;
-
-
WHILE i < l_num DO
-
SET result = concat( result,substr(v_chars,ceil(rand()*(length(v_chars)-1)),1) );
-
SET i = i + 1;
-
END WHILE;
-
RETURN result;
-
END;
-
//
-
delimiter ;
準(zhǔn)備實(shí)驗(yàn)表.
先創(chuàng)建一些帶有外鍵約束的表.數(shù)據(jù)庫(kù)名稱是 probe
-
CREATE TABLE `t_jvm_info` (
-
`id` bigint(20) NOT NULL AUTO_INCREMENT,
-
`app_name` varchar(32) NOT NULL COMMENT '應(yīng)用名稱',
-
`host_name` varchar(32) NOT NULL COMMENT '主機(jī)名稱',
-
`collect_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '采集時(shí)間',
-
`version` varchar(32) NOT NULL DEFAULT '' COMMENT 'jvm版本',
-
`vendor` varchar(32) NOT NULL DEFAULT '' COMMENT '廠商',
-
`java_home` varchar(64) NOT NULL DEFAULT '' COMMENT '客戶端javahome路徑',
-
`loaded_class_count` int(11) NOT NULL DEFAULT '-1' COMMENT '已經(jīng)加載的類數(shù)量',
-
`unloaded_class_count` int(11) NOT NULL DEFAULT '-1' COMMENT '已經(jīng)卸載的類數(shù)量',
-
`total_loaded_class_count` int(11) NOT NULL DEFAULT '-1' COMMENT '累計(jì)加載的類數(shù)量',
-
`heap_init` float NOT NULL DEFAULT '-1' COMMENT '堆內(nèi)存初始大小',
-
`heap_committed` bigint(20) NOT NULL DEFAULT '-1' COMMENT 'os分配給jvm的堆內(nèi)存',
-
`heap_max` bigint(20) NOT NULL DEFAULT '-1' COMMENT '堆內(nèi)存上限',
-
`heap_used` bigint(20) NOT NULL DEFAULT '-1' COMMENT '已經(jīng)使用的堆內(nèi)存大小',
-
`non_heap_init` bigint(20) NOT NULL DEFAULT '-1' COMMENT '非堆內(nèi)存初始大小',
-
`non_heap_committed` bigint(20) NOT NULL DEFAULT '-1' COMMENT 'os分配給jvm的非堆內(nèi)存',
-
`non_heap_max` bigint(20) NOT NULL DEFAULT '-1' COMMENT '非堆內(nèi)存上限',
-
`non_heap_used` bigint(20) NOT NULL DEFAULT '-1' COMMENT '已經(jīng)使用的非堆內(nèi)存大小',
-
`current_thread_count` int(11) NOT NULL DEFAULT '-1' COMMENT '當(dāng)前jvm線程總數(shù)',
-
`total_started_thread_count` int(11) NOT NULL DEFAULT '-1' COMMENT '累計(jì)啟動(dòng)過(guò)的線程總數(shù)',
-
`peak_thread_count` int(11) NOT NULL DEFAULT '-1' COMMENT '線程數(shù)量最大值',
-
`daemon_thread_count` int(11) NOT NULL DEFAULT '-1' COMMENT 'daemon線程數(shù)量',
-
PRIMARY KEY (`id`),
-
KEY `app_name` (`app_name`,`host_name`,`collect_time`),
-
KEY `host_name` (`host_name`,`collect_time`)
-
) ENGINE=InnoDB AUTO_INCREMENT=2001 DEFAULT CHARSET=utf8mb4 COMMENT='jvm采集信息表';
-
-
CREATE TABLE `t_jvm_gc_info` (
-
`id` bigint(20) NOT NULL AUTO_INCREMENT,
-
`t_jvm_info_id` bigint(20) NOT NULL COMMENT 'jvm采集信息表id',
-
`name` varchar(32) NOT NULL COMMENT 'gc類型名稱',
-
`gctime` bigint(20) NOT NULL DEFAULT '-1' COMMENT 'gc時(shí)間',
-
`gccount` bigint(20) NOT NULL DEFAULT '-1' COMMENT 'gc次數(shù)',
-
PRIMARY KEY (`id`),
-
KEY `t_jvm_info_id` (`t_jvm_info_id`),
-
CONSTRAINT `t_jvm_gc_info_ibfk_1` FOREIGN KEY (`t_jvm_info_id`) REFERENCES `t_jvm_info` (`id`)
-
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8mb4 COMMENT='jvm gc采集信息表';
創(chuàng)建可以自動(dòng)生成數(shù)據(jù)的存儲(chǔ)過(guò)程
-
drop procedure if exists auto_fill ;
-
delimiter $$
-
create procedure auto_fill(pDb varchar(32),pTableList varchar(1024))
-
begin
-
declare done int default 0;
-
declare v_dbName varchar(128);
-
declare v_fullTableName varchar(128);
-
declare v_tableName varchar(128);
-
declare v_rowCount int;
-
declare cur_test CURSOR for select dbName,fullTableName,tableName,rowCount from tmp_table_info;
-
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
-
-
-- 臨時(shí)表,用于保存拆分參數(shù)之后的結(jié)果.主要信息就是數(shù)據(jù)庫(kù)名稱和表名稱
-
drop table if exists tmp_table_info;
-
create temporary table tmp_table_info
-
select pDb dbName,
-
concat(pDb,'.',substring_index ( value_str,',',1 )) fullTableName ,
-
substring_index ( value_str,',',1 ) tableName,
-
substring_index ( value_str,',',-1 ) rowCount
-
from (
-
select substring_index(substring_index(pTableList,';',b.id),';',-1) value_str
-
from
-
nums b
-
where b.id <= (length(pTableList) - length(replace(pTableList,';',''))+1)
-
) t1;
-
-
-- 禁用外鍵
-
SET FOREIGN_KEY_CHECKS=0;
-
open cur_test;
-
repeat
-
fetch cur_test into v_dbName,v_fullTableName,v_tableName,v_rowCount;
-
if done!=1 then
-
-
set @sql=concat('insert ignore into ',v_dbName,'.',v_tableName,' select ');
-
select
-
@sql:=concat(@sql,
-
case
-
when extra='auto_increment' then concat('id,')
-
when data_type='int' then if(rowCount is null,'round(rand()*2147483647),',concat('round(rand()*',rowCount,'),'))
-
when data_type='bigint' then if(rowCount is null,'round(rand()*9223372036854775807),',concat('round(rand()*',rowCount,'),'))
-
when data_type='smallint' then 'round(rand()*32767),'
-
when data_type='tinyint' then 'round(rand()*127 ),'
-
when data_type='varchar' then concat('rand_string(',CHARACTER_MAXIMUM_LENGTH,',5),')
-
when data_type='date' then 'now()-interval round(90*rand()) day,'
-
when data_type='datetime' then 'now()-interval round(90*rand()) day,'
-
when data_type='timestamp' then 'now()-interval round(90*rand()) day,'
-
when data_type in('double','float') then 'round(rand()*32767,5),'
-
when data_type like '%text%' then concat('rand_string(2048,5),')
-
end
-
) s
-
from (
-
select
-
k.referenced_table_name,
-
k.referenced_column_name,
-
c.table_schema,
-
c.table_name,
-
c.column_name,
-
c.data_type,
-
c.CHARACTER_MAXIMUM_LENGTH,
-
c.extra,
-
t.rowCount
-
from information_schema.columns c
-
left join information_schema.KEY_COLUMN_USAGE k on(
-
c.table_schema=k.table_schema and
-
c.table_name=k.table_name and
-
c.column_name=k.column_name and
-
k.constraint_name
-
in (select constraint_name from information_schema.REFERENTIAL_CONSTRAINTS)
-
)
-
left join tmp_table_info t on(t.dbName=k.table_schema and t.tableName=k.table_name)
-
where (c.table_schema,c.table_name) =(v_dbName,v_tableName)
-
order by c.ORDINAL_POSITION
-
) t2
-
;
-
set @sql=left(@sql,char_length(@sql)-1);
-
select nullif ('please stand by...',@sql:=concat(@sql,' from nums where id<=',v_rowCount,';')) info;
-
prepare statement from @sql;
-
execute statement;
-
commit;
-
end if;
-
until done end repeat;
-
close cur_test;
-
-
-- 恢復(fù)外鍵
-
SET FOREIGN_KEY_CHECKS=1;
-
-
-
end ;
-
$$
-
delimiter ;
執(zhí)行存儲(chǔ)過(guò)程填充數(shù)據(jù)
call auto_fill('probe','t_jvm_gc_info,100000;t_jvm_info,2000');
過(guò)程第一個(gè)參數(shù)是 數(shù)據(jù)庫(kù)名稱
第二個(gè)參數(shù)是 表名和行數(shù)的字符串列表.
測(cè)試數(shù)據(jù)生成自行刪除外鍵約束即可
文章標(biāo)題:MySQL用隨機(jī)數(shù)據(jù)填充外鍵表
URL鏈接:http://www.rwnh.cn/article0/jsdooo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、軟件開發(fā)、手機(jī)網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、品牌網(wǎng)站建設(shè)
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源:
創(chuàng)新互聯(lián)