MySQL is playing around with 1000w data

100w data 320MB 320s
1000w data 3.2G 3200s
[approximately]

#创建部门表 手动改为自增id
create table dept(
id int UNSIGNED PRIMARY key,
deptno MEDIUMINT UNSIGNED not null default 0,
dname varchar(20) not null default "",
ioc varchar(13) not null default ""
)engine=innodb default charset=GBK;

#创建员工表 手动改自增id
create table emp2(
id int UNSIGNED PRIMARY key,
empno MEDIUMINT UNSIGNED not null default 0,
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=innodb default charset=GBK;

#开了慢查询 记得开这个
show VARIABLES like'log_bin_trust_function_creators'; 
set GLOBAL log_bin_trust_function_creators=1;

#随机字符串a-z A-Z
DELIMITER $$
create function rand_string(n int) returns varchar(255)
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 $$
create function rand_num() returns int(5)
begin
	declare i int default 0;
	set i=floor(100+rand()*10);
return i;
end $$

#结束符号改为$$
#创建存储过程 插入员工的
delimiter $$
create procedure insert_emp0(in start int(10), in max_num int(10) )
begin
declare i int default 0;
set autocommit=0;
repeat
set i=i+1;
insert into emp2(empno, ename, job, mgr, hiredate ,sal, comm,deptno) values((start+i), rand_string(6),'salesman',0001,CURRENT_DATE,2000,400,rand_num());

until i=max_num
end repeat;
commit;
end $$


#结束符号改为$$
#创建存储过程 插入部门的
delimiter $$
create procedure insert_dept1(in start int(10), in max_num int(10) )
begin
declare i int default 0;
set autocommit=0;
repeat
set i=i+1;
insert into dept(deptno, dname,ioc) values((start+i), rand_string(10), rand_string(8));
until i=max_num
end repeat;
commit;
end $$

#插入10个部门
call insert_dept1(1,10);

select * from dept 

#康康有多少条记录了
select count(*) from emp2;

#50w一次插入 一共20次
call insert_emp0(10000000,500000);

#
select * from emp2 where ename like 'A%';

Start of the test:
This sentence took 10s

select * from emp2 where ename like 'A%';

It took 1 minute to build a single-value index.


create index index_ename on emp2(ename);

create index index_ename on emp2(ename)
OK
time: 57.663s

Again, the results are not displayed. Feeling slow?


Try individual


select * from emp2 where empno=100000; 

Took 10s

create index eno_idx on emp2(empno)
OK
time: 37.075s

Seconds out. . . 0.02+
more than 100 times the speed.
This is the result I want


Next, another example from Kang:

select * from emp2 where empno<100000; 

10s before the index is built, it will be output in 0.4s after the index is built.

Before indexing:
Insert picture description here

After indexing:
Insert picture description here


Here is another equivalent query of ename:

select id from emp2 where ename='uBzTVU';

There is no index 7s, there is an index in seconds, this time it is the desired index again, yes.


Even this fuzzy query comes out in seconds: select id from emp2 where ename like'uBzTV%';


== I checked and got a result. The reason is that when the like is fuzzy query, if the keyword input is more consistent, it will be effective, if it is not consistent, it will be invalid or worse ==


To test and find:

#这条 秒出
select id from emp2 where ename like 'uAB%';

#这条则有问题
select id from emp2 where ename like 'A%';

#然后再缩减一个字母B看看结果,30s,更慢了
select id from emp2 where ename like 'uA%';


So get a conclusion:

The keywords in fuzzy query are related to the length of the attribute, the more the better. Too little will cause the index to fail.

It is best to use equivalent query, and no problem is found for the time being.


Slow query log is turned on:

It is closed by default and there is no file.
After opening, a .log file will be created

#显示两个变量,一个是是否开启,另一个是文件存储的位置
show  VARIABLES like '%slow_query_log%';

set GLOBAL slow_query_log=1;

show  GLOBAL VARIABLES like '%slow_query_log%';

Look at the defined long time (default 10s) and modify it to 3s, and then start to query Kangkang

show  VARIABLES like '%long_query_time%';

set long_query_time=3;

create index ino on emp2(empno);

select * from emp2 where empno<100000;

select * from emp2 where ename like 'abdc%';

The result is ideal, and it will be recorded if it should be recorded.

Guess you like

Origin blog.csdn.net/BOWWOB/article/details/113801434