mysql 海量随机查询_Mysql优化之:构建海量表,定位慢查询

为了模拟这个过程,我们需要构建一个海量表(8000000),而且每条数据不一样,这样的测试才有意义

一、构建海量表过程

(1)、创建数据库

create database testdb

(2)、选择数据库

set names utf8

use testdb

(3)、创建表

SQL

~~~

create table dept(/*部门表*/

deptno mediumint unsigned not null default 0,

dname varchar(20) not null default "",

loc varchar(13) not null default ""

) engine=myisam default charset=utf8;

create table emp(/*雇员表*/

ename varchar(20) not null default "",

job varchar(9) not null default "",

mgr mediumint unsigned not null default 0,

hiredate date not null,

sal decimal(7,2) not null,

comm decimal(7,2) not null,

deptno mediumint unsigned not null default 0

) engine=myisam default charset=utf8;

create table salgrade(/*工资级别表*/

grade mediumint unsigned not null default 0,

losal decimal(17,2) not null,

hisal decimal(17,2) not null

) engine=myisam default charset =utf8;

#测试数据

insert into salgrade values(1,700,1200);

insert into salgrade values(2,1201,1400);

insert into salgrade values(3,1401,2000);

insert into salgrade values(4,2001,3000);

insert into salgrade values(5,3001,9999);

#设定新的命令结束符

delimiter $$

#删除之前创建的函数

drop function rand_string$$

创建一个函数

create function rand_string(n int)

returns varchar(255)#该函数会返回一个字符串

begin

#定义了一个变量 chars_str,类型varchar(100)

declare chars_str varchar(100) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';

declare return_str varchar(255) default '';

declare i int default 0;

while i

#concat函数:连接函数mysql函数

set return_str = concat(return_str,substring(chars_str,floor(1+rand()*62),1));

set i = i+1;

end while;

return return_str;

end $$

#随机产生部门编号

create function rand_num()

returns int(5)

begin

declare i int default 0;

set i = floor(10+rand()*500);

return i;

end $$

#创建存储过程

create procedure insert_emp(in start int(10),in max_num int(10))

begin

declare i int default 0;

set autocommit = 0;#默认不提交SQL语句

repeat #循环语句

set i = i + 1;

insert into emp values((start+i),rand_string(6),'SALESMAN',0001,curdate(),2000,4000,rand_num());

until i = max_num

end repeat;

commit;#整体提交所有SQL语句

end$$

#恢复定界符

delimiter ;

#调用存储过程

call insert_emp(1,8000000);

~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值