当前位置:网站首页>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");
边栏推荐
- one billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven
- PostgreSQL age XID maintenance prevents the database from being read-only
- [FPGA chaos] implementation of FPGA based chaotic system Verilog
- Work report of epidemic data analysis platform [1] data collection
- Solid programming concepts
- EnterpriseTECH STAR Question
- 疫情数据分析平台工作报告【4】跨域相关
- How to construct a search string?
- Unable to resolve dependency tree
- Epidemic data analysis platform work report [3] website deployment
猜你喜欢

路灯照明物联网技术方案,ESP32-S3芯片通信应用,智能WiFi远程控制

New year news of osdu open underground data space Forum

Kill session? This cross domain authentication solution is really elegant!
![[software tool] [original] tutorial on using VOC dataset class alias batch modification tool](/img/25/31d771c9770bb7f455f35e38672170.png)
[software tool] [original] tutorial on using VOC dataset class alias batch modification tool

如何制作数据集并基于yolov5训练成模型并部署

QT compile 45 graphic report of security video monitoring system

疫情数据分析平台工作报告【6.5】疫情地图

Esp32c3 remote serial port

InnoDB data storage structure – MySQL

Redis learning notes (continuously updating)
随机推荐
leetcode797. 所有可能的路径(中等)
Install pycharm under Kali and create a shortcut access
SQL Safe Backup显示器和缩放字体的支持
Summary of common interview questions in redis
L1-064 AI core code valued at 100 million (20 points)
How Windows installs multiple versions of MySQL and starts it at the same time
2022 electrician (elementary) operation certificate examination question bank and online simulation examination
疫情数据分析平台工作报告【3】网站部署
[fpga+gps receiver] detailed design introduction of dual frequency GPS receiver based on FPGA
Gao Xiang slam14 notes on three Lie groups and Lie algebra
QT compile 45 graphic report of security video monitoring system
Smart panel WiFi linkage technology, esp32 wireless chip module, Internet of things WiFi communication application
Betteland introduces milk products of non animal origin, which will be launched in the U.S. market in the near future
Redis learning notes (continuously updating)
Kill session? This cross domain authentication solution is really elegant!
CCF access control system
Based on Visual Studio code Net Maui cross platform mobile application development
kali_ Nat mode, bridging Internet / host only_ detailed
C# Task. Waitall method
New year news of osdu open underground data space Forum