引用自https://www.cnblogs.com/qmfsun/p/4881919.html
在mysql數據庫中制作千萬級測試表
前言:
最近准備深入的學一下mysql,包括各種引擎的特性、性能優化、分表分庫等。為了方便測試性能、分表等工作,就需要先建立一張比較大的數據表。我這里准備先建一張千萬記錄用戶表。
步驟:
1
創建數據表(MYISAM方式存儲插入速度比innodb方式快很多)
數據表描述
數據量:
1
千萬
字段類型:
id :編號
uname:用戶名
ucreatetime: 創建時間
age:年齡
CREATE TABLE usertb(
id serial,
uname varchar(
20
) ,
ucreatetime datetime ,
age
int
(
11
))
ENGINE=MYISAM
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=
1
ROW_FORMAT=COMPACT;
2
創建插入數據存儲過程
delimiter $$
SET AUTOCOMMIT =
0
$$
create procedure test1()
begin
declare v_cnt decimal (
10
)
default
0
;
dd:loop
insert into usertb values
(
null
,
'用戶1'
,
'2010-01-01 00:00:00'
,
20
),
(
null
,
'用戶2'
,
'2010-01-01 00:00:00'
,
20
),
(
null
,
'用戶3'
,
'2010-01-01 00:00:00'
,
20
),
(
null
,
'用戶4'
,
'2010-01-01 00:00:00'
,
20
),
(
null
,
'用戶5'
,
'2011-01-01 00:00:00'
,
20
),
(
null
,
'用戶6'
,
'2011-01-01 00:00:00'
,
20
),
(
null
,
'用戶7'
,
'2011-01-01 00:00:00'
,
20
),
(
null
,
'用戶8'
,
'2012-01-01 00:00:00'
,
20
),
(
null
,
'用戶9'
,
'2012-01-01 00:00:00'
,
20
),
(
null
,
'用戶0'
,
'2012-01-01 00:00:00'
,
20
)
;
commit;
set v_cnt = v_cnt+
10
;
if
v_cnt =
10000000
then leave dd;
end
if
;
end loop dd ;
end;$$
delimiter ;
3
執行存儲過程
call test1;
耗時:用i5的筆記本執行也只需要
95
秒的時間
4
根據需要修改engineer (非必要步驟,如果不需要轉換無需操作)
alter table usertb engine=innodb;
耗時:用i5的筆記本執行也只需要
200
秒的時間