当前位置:网站首页>Advanced MySQL knowledge points (7)
Advanced MySQL knowledge points (7)
2022-06-12 04:38:00 【Zhaoliwen is a pig】
MySQL Advanced knowledge ( 7、 ... and )
List of articles
Intercept query analysis
1、 Slow log query
Concept
- MySQL The slow query log of is MySQL A type of logging provided , It is used to record in MySQL Statement with response time over threshold in , Specifically, the running time exceeds long_query_time It's worth it SQL, It will be recorded in the slow query log
- Specifically, the running time exceeds long_query_time It's worth it SQL, It will be recorded in the slow query log .long_query_time The default value is 10, It means running 10 Seconds or more
- It's up to him to see what SQL Beyond our maximum endurance time , Like one sql To perform more than 5 Second , We are slow SQL, I hope I can Collection over 5 Of a second sql, Before integration explain Conduct a comprehensive analysis
Use
By default ,MySQL Slow query log is not enabled in the database , We need to Manual To set this parameter
If it's not for tuning , It is generally not recommended to start this parameter , Because opening slow query log will bring some performance impact more or less . Slow query log supports writing log records to files
| SQL sentence | describe | remarks |
|---|---|---|
| SHOW VARIABLES LIKE ‘%slow_query_log%’ | Check whether the slow query log is on | By default slow_query_log The value of is OFF |
| set global slow_query_log=1 | Open slow query log | |
| SHOW VARIABLES LIKE ‘long_query_time%’ | View slow query set threshold | Company : second |
| set long_query_time=1 | Set the slow query threshold | Company : second |
Long running queries sql, You can open the slow query log to view
2、 Batch data script
Create table statement
--dept Departmental table
CREATE TABLE `dept` (
`id` INT(11) NOT NULL AUTO_INCREMENT, `deptName` VARCHAR(30) DEFAULT NULL, `address` VARCHAR(40) DEFAULT NULL, ceo INT NULL , PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-- emp The employee table
CREATE TABLE `emp` (
`id` INT(11) NOT NULL AUTO_INCREMENT, `empno` INT NOT NULL , `name` VARCHAR(20) DEFAULT NULL, `age` INT(3) DEFAULT NULL, `deptId` INT(11) DEFAULT NULL, PRIMARY KEY (`id`)
#CONSTRAINT `fk_dept_id` FOREIGN KEY (`deptId`) REFERENCES `t_dept` (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Set parameters
Before executing the create function , First of all, please ensure log_bin_trust_function_creators Parameter is 1, namely on On state . Otherwise, an error will be reported
-- Inquire about
SHOW VARIABLES LIKE 'log_bin_trust_function_creators';
-- Set up
SET GLOBAL log_bin_trust_function_creators=1;
Write random functions
Randomly generate strings
--DELIMITER It's used to change the end sign , Usually ends with a semicolon , But here is changed to $$ ending
DELIMITER $$
CREATE FUNCTION rand_string(n INT) RETURNS VARCHAR(255)
BEGIN
DECLARE chars_str VARCHAR(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ';
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 $$
If you want to Delete function , execute :
DROP FUNCTION rand_string;
Randomly generate department number
DELIMITER $$
CREATE FUNCTION rand_num (from_num INT ,to_num INT) RETURNS INT(11)
BEGIN
DECLARE i INT DEFAULT 0;
SET i = FLOOR(from_num +RAND()*(to_num -from_num+1)) ;
RETURN i;
END$$
If you want to Delete function , execute :
drop function rand_num;
Create stored procedure
To create emp A stored procedure that inserts data into a table
DELIMITER $$
CREATE PROCEDURE insert_emp( START INT , max_num INT )
BEGIN
DECLARE i INT DEFAULT 0;
#set autocommit =0 hold autocommit Set to 0
SET autocommit = 0;
REPEAT
SET i = i + 1;
INSERT INTO emp (empno, NAME ,age ,deptid ) VALUES ((START+i) ,rand_string(6) , rand_num(30,50),rand_num(1,10000));
UNTIL i = max_num
END REPEAT;
COMMIT;
END$$
-- Delete
-- DELIMITER ;
-- drop PROCEDURE insert_emp;
To create dept A stored procedure that inserts data into a table
-- Execute stored procedures , Go to dept Add random data to table
DELIMITER $$
CREATE PROCEDURE `insert_dept`( max_num INT )
BEGIN
DECLARE i INT DEFAULT 0;
SET autocommit = 0;
REPEAT
SET i = i + 1;
INSERT INTO dept ( deptname,address,ceo ) VALUES (rand_string(8),rand_string(10),rand_num(1,500000));
UNTIL i = max_num
END REPEAT;
COMMIT;
END$$
-- Delete
-- DELIMITER ;
-- drop PROCEDURE insert_dept;
Calling stored procedure
Add data to department table
-- Execute stored procedures , Go to dept Table to add 1 Ten thousand data
DELIMITER ;
CALL insert_dept(10000);
Add data to employee table
-- Execute stored procedures , Go to emp Table to add 50 Ten thousand data
DELIMITER ;
CALL insert_emp(100000,500000);
Batch delete all indexes on a table
Delete the stored procedure of the index
DELIMITER $$
CREATE PROCEDURE `proc_drop_index`(dbname VARCHAR(200),tablename VARCHAR(200))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE ct INT DEFAULT 0;
DECLARE _index VARCHAR(200) DEFAULT '';
DECLARE _cur CURSOR FOR SELECT index_name FROM information_schema.STATISTICS WHERE
table_schema=dbname AND table_name=tablename AND seq_in_index=1 AND index_name <>'PRIMARY' ;
DECLARE CONTINUE HANDLER FOR NOT FOUND set done=2 ;
OPEN _cur;
FETCH _cur INTO _index;
WHILE _index<>'' DO
SET @str = CONCAT("drop index ",_index," on ",tablename );
PREPARE sql_str FROM @str ;
EXECUTE sql_str;
DEALLOCATE PREPARE sql_str;
SET _index='';
FETCH _cur INTO _index;
END WHILE;
CLOSE _cur;
END$$
Execute stored procedures
CALL proc_drop_index("dbname","tablename");
边栏推荐
- @What happens if bean and @component are used on the same class?
- Daily practice (28): balance binary tree
- Question for the 3D printing lattice?
- Sustainable service business models
- Detailed explanation of Command Execution Vulnerability
- [efficient] the most powerful development tool, ctool, is a compilation tool
- kali_ Nat mode, bridging Internet / host only_ detailed
- 存储器的保护
- Labor
- D1 Nezha development board power on record
猜你喜欢

Mysql主从搭建与Django实现读写分离

分布式锁介绍

windows如何安装多个版本mysql,如何同时启动

How Windows installs multiple versions of MySQL and starts it at the same time

Based on Visual Studio code Net Maui cross platform mobile application development

QT compiling security video monitoring system 43- picture playback

2022 low voltage electrician test questions and simulation test

Operation of simulated examination platform for theoretical question bank of G2 utility boiler stoker in 2022

Legendary biological car-t has been approved by FDA, becoming the first domestic cell therapy product to successfully go to sea
![Work report of epidemic data analysis platform [1] data collection](/img/3d/b58c2d3f7fd6051e35d1cea535f858.png)
Work report of epidemic data analysis platform [1] data collection
随机推荐
疫情数据分析平台工作报告【8.5】额外的爬虫和绘图
LabVIEW关于TDMS和Binary存储速度
JWT learning and use
eBPF系列学习(4)了解libbpf、CO-RE (Compile Once – Run Everywhe) | 使用go开发ebpf程序(云原生利器cilium ebpf )
Report on the current market situation and future development trend of adhesive industry for radar and ultrasonic sensors in the world and China 2022 ~ 2028
Thousand word masterpiece "programming biography"
Install/Remove of the Service Denied!
WPF 数据绑定(四)
2022 examination questions and online simulation examination for safety management personnel of hazardous chemical business units
2.28 (defect filling) data type conversion exception handling part multi threading
Notes on relevant knowledge points such as original code / inverse code / complement code, size end, etc
What are the black box test case design methods in software testing methods?
Bearpi IOT serial port transceiver 1- normal mode
调用提醒事项
JWT學習與使用
Daily practice (28): balance binary tree
Install pycharm under Kali and create a shortcut access
kali_ Change_ Domestic source
疫情数据分析平台工作报告【42】CodeNet
spacy中en_core_web_sm安装问题