MySQL - and function using slow query log

Slow query log

A journaling MySQL slow query log records provided by MySQL, which is used to record the response time exceeds the threshold statement in MySQL, refers specifically to
run longer than the SQL long_query_time value, it will be recorded to the slow query log
long_query_time default It is 10, meaning that more than 10 seconds to run the statement.
By him to see that SQL is beyond our patience maximum time value, such as a SQL execution for more than 5 seconds, even if we are slow SQL, hoping to receive
set over five seconds of SQL, explain to conduct a comprehensive analysis prior to bonding.

use

By default, MySQL database does not turn on slow query log, we need to set the parameters manually
, of course, if not tuning required, generally do not recommend activating this feature, because the slow query log will open more or less bring some performance impact . Slow
query log file written to support logging

Slow query log tool

s: indicates how sorted
c: Visits
l: lock time
r: return records
t: Time Query
al: Average lock time
ar: Average number of records returned
t: is the return to the previous data of how many

Returns the record set up to give 10 SQL:

mysqldumpslow -s r -t 10 MySQL所在目录/slow_log.txt

Get most visited 10 SQL:

mysqldumpslow -s c -t 10 MySQL所在目录/slow_log.txt

Function uses

MySQL functions do not need to use rote, ensure that read, when to use the cover of the can.

To build the table

Sector Table

create table dept(
id int primary key auto_increment,
deptno mediumint not null,
dname varchar(20) not null,
loc varchar(13) not null
)engine=innodb default charset=gbk;

Employees table

create table emp(
id int primary key auto_increment,
empno mediumint not null,
ename varchar(20) not null,
job varchar(9) not null,
mgr mediumint not null,
hiredate DATE not null,
sal decimal(7,2) not null,
comm decimal(7,2) not null,
deptno mediumint not null
)engine=innodb default charset=gbk;

Creating function

Create a function, if error: this function has none of DETERMINISTIC ... View parameters

set global log_bin_trust_function_creators=1;

Generating a random string functions:

DELIMITER $$
CREATE FUNCTION rand_string(n INT) RETURNS VARCHAR(255)
BEGIN
DECLARE chars_str VARCHAR(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLM
NOPQRSTUVWXYZ';
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 $$

Random number function generating department:

DELIMITER $$
CREATE FUNCTION rand_num() RETURNS INT(5)
BEGIN
DECLARE i INT DEFAULT 0;
SET i = FLOOR(100+RAND()*10);
RETURN i;
END $$

Create a stored procedure

Insert data

DELIMITER $$
CREATE PROCEDURE insert_emp(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 emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) VALUES ((START+i),
rand_string(6),'SALESMAN',0001,CURDATE(),2000,400,rand_num());
UNTIL i = max_num
END REPEAT;
COMMIT;
END $$
DELIMITER $$
CREATE PROCEDURE insert_dept(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,loc) VALUES ((START + i),rand_string(10),rand_str

Call a stored procedure

delimiter ;
call insert_dept(100,10);
delimiter ;
call insert_emp(100001,500000);
发布了29 篇原创文章 · 获赞 3 · 访问量 2309

Guess you like

Origin blog.csdn.net/weixin_45735361/article/details/104115011