了解 mysql 数据库的一些运行状态
show status;
show status like ‘uptime‘;
show status like ‘com_select‘;
show status like ‘com_update‘;
show session status like ‘com_update‘; # 当前回话, 默认
show global status like ‘com_update‘; # 从 mysql 启动开始
show status like ‘connections‘;
show global status like ‘slow_queries‘;
show variables like ‘long_query_time‘; # 慢查询
set long_query_time=1; # 为了方便, 将慢查询的时间修改为 1
构建慢查询
create database tmp_db;
use tmp_db;
drop table if exists dept;
create table dept (
deptno mediumint unsigned not null default 0,
dname varchar(20) not null default ‘‘,
loc varchar(13) not null default ‘‘
) engine = myisam charset utf8;
drop table if exists emp;
create table emp(
empno mediumint unsigned not null default 0 comment ‘编号‘,
ename varchar(20) not null default ‘‘ comment ‘名字‘,
job varchar(9) not null default ‘‘ comment ‘工作‘,
mgr mediumint unsigned not null default 0 comment ‘上级编号‘,
hiredate date not null comment ‘入职时间‘,
sal decimal(7,2) not null comment ‘薪水‘,
comm decimal(7,2) not null comment ‘红利‘,
deptno mediumint unsigned not null default 0 comment ‘部门编号‘
) engine=myisam charset utf8;
drop table salgrade;
create table salgrade(
grade mediumint unsigned not null default 0,
losal decimal(7,2) not null,
hisal decimal(7,2) not null
) engine=myisam charset utf8;
insert into salgrade(grade, losal, hisal) values
(1, 700, 2000),
(2, 1201, 1400),
(3, 1401, 2000),
(4, 2001, 3000),
(5, 3001, 9000);
delimiter $$
drop function if exists rand_string;
create function rand_string(n int) returns varchar(100)
begin
declare chars_str varchar(100) default "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
declare return_str varchar(255) default ‘‘;
declare i int default 0;
while i < n do
set return_str = concat(return_str, substring(chars_str, floor(1 + rand() * 52), 1));
set i = i + 1;
end while;
return return_str;
end
$$
delimiter ;
drop function if exists rand_num;
delimiter $$
create function rand_num() returns int(5)
begin
return floor(10 + rand() * 500);
end
$$
delimiter ;
drop Procedure if exists insert_emp;
delimiter $$
Create Procedure insert_emp(max_num int(5))
begin
declare i int default 0;
declare here int;
set autocommit = 0;
set here = (select max(empno) from emp);
if isnull(here) then
set here = 0;
end if;
while i < max_num do
set i = i + 1;
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (i+here, rand_string(8), ‘saleman‘, 1, current_date(), 2000, 400, rand_num());
end while;
commit;
set autocommit = 1;
end
$$
delimiter ;
call insert_emp(10000);
call insert_emp(40000);