数据库学习——07—Mysql高级(分析SQL步骤 、排序优化、慢查询日志使用、批量插入数据和创建函数、Show Profile进行SQL分析)

1、分析SQL
优化顺序:

  1. 分析:观察,复现问题,看看生产的慢SQL情况
  2. 开启慢查询日志,设置阈值,比如超出5s的就是慢SQL,并抓取出来
  3. explain + 慢SQL 进行分析
  4. show profile
  5. 进行SQL数据库服务器的参数调优

总结
1.慢查询的开启并捕获
2.explain+慢SQL分析
3.show profile查询SQL在MySQL服务器里面的执行细节
4.SQL数据库服务器的参数调优

遵守原则:永远小表驱动大表
循环5000for i in range(5):              (与数据库连接5)          ---优先选择这种方式
    for j in range(1000):       (进行1000次的操作)
        pass

for i in range(1000):          (与数据库连接1000)
    for j in range(5):          (进行5次的操作)
        pass

order by子句,尽量使用index方式排序,避免使用filesort方式排序(即在Extra项目中是Using index而不是Using filesort)

2、排序优化

  1. 建表,插入测试数据
create table tbla(
    age int,
    birth timestamp not null
);
insert into tbla(age,birth) values(22,now());
insert into tbla(age,birth) values(23,now());
insert into tbla(age,birth) values(24,now());
  1. 建立索引
create index idx_tbla_agebrith on tbla(age,birth);
  1. 分析
    MySQL支持两种方式的排序,filesort和index,index效率高,MySQL扫描索引本身完成排序。filesort方式效率较低.

order by 满足两种情况下,会使用index方式排序
1.order by 语句使用索引最左前列
2.使用where子句与order by子句条件组合满足索引最左前列法则(索引优化口诀)

filesort有两种算法-双路排序和单路排序(了解)
双路排序,MySQL4.1之前是使用双路排序,字面意思就是两次扫描磁盘,最终得到数据,读取行指针和order by列,对他们进行排序,然后扫描已经排序好的列表,按照列表中的值重新从列表中读取对应的数据输出
单路排序,从磁盘读取查询需要的所有列,按照order by列在buffer对他们进行排序,然后扫描排序后的列表进行输出,它的效率更快一些,避免了第二次读取数据,并且把随机IO变成了顺序IO,但是它会使用更多的空间

 mysql> create table tbla(                                                                                                                                                 
    -> age int,                                                                                                                                                           
    -> birth timestamp not null                                                                                                                                           
    -> );                                                                                                                                                                 
Query OK, 0 rows affected (0.13 sec)                                                                                                                                      
                                                                                                                                                                          
mysql> insert into tbla(age,birth) values(22,now());                                                                                                                      
Query OK, 1 row affected (0.03 sec)                                                                                                                                       
                                                                                                                                                                          
mysql> insert into tbla(age,birth) values(23,now());                                                                                                                      
Query OK, 1 row affected (0.03 sec)                                                                                                                                       
                                                                                                                                                                          
mysql> insert into tbla(age,birth) values(24,now());                                                                                                                      
Query OK, 1 row affected (0.03 sec)                                                                                                                                       
                                                                                                                                                                          
mysql> create index idx_tbla_agebrith on tbla(age,birth);                                                                                                                 
Query OK, 3 rows affected (0.29 sec)                                                                                                                                      
Records: 3  Duplicates: 0  Warnings: 0                                                                                                                                    
                                                                                                                                                                          
mysql> explain select * from tbla where age = 30 order by birth;                                                                                                          
+----+-------------+-------+------------+------+-------------------+-------------------+---------+-------+------+----------+--------------------------+                   
| id | select_type | table | partitions | type | possible_keys     | key               | key_len | ref   | rows | filtered | Extra                    |                   
+----+-------------+-------+------------+------+-------------------+-------------------+---------+-------+------+----------+--------------------------+                   
|  1 | SIMPLE      | tbla  | NULL       | ref  | idx_tbla_agebrith | idx_tbla_agebrith | 5       | const |    1 |   100.00 | Using where; Using index |                   
+----+-------------+-------+------------+------+-------------------+-------------------+---------+-------+------+----------+--------------------------+                   
1 row in set, 1 warning (0.00 sec)                                                                                                                                        
                                                                                                                                                                          
mysql> explain select * from tbla where age = 30 order by age;                                                                                                            
+----+-------------+-------+------------+------+-------------------+-------------------+---------+-------+------+----------+-------------+                                
| id | select_type | table | partitions | type | possible_keys     | key               | key_len | ref   | rows | filtered | Extra       |                                
+----+-------------+-------+------------+------+-------------------+-------------------+---------+-------+------+----------+-------------+                                
|  1 | SIMPLE      | tbla  | NULL       | ref  | idx_tbla_agebrith | idx_tbla_agebrith | 5       | const |    1 |   100.00 | Using index |                                
+----+-------------+-------+------------+------+-------------------+-------------------+---------+-------+------+----------+-------------+                                
1 row in set, 1 warning (0.00 sec)                                                                                                                                        
                                                                                                                                                                          
mysql> explain select * from tbla where brith > '2019-01-01 00:00:00' order by age;                                                                                       
ERROR 1054 (42S22): Unknown column 'brith' in 'where clause'                                                                                                              
mysql> explain select * from tbla where birth > '2019-01-01 00:00:00' order by age;                                                                                       
+----+-------------+-------+------------+-------+---------------+-------------------+---------+------+------+----------+--------------------------+                       
| id | select_type | table | partitions | type  | possible_keys | key               | key_len | ref  | rows | filtered | Extra                    |                       
+----+-------------+-------+------------+-------+---------------+-------------------+---------+------+------+----------+--------------------------+                       
|  1 | SIMPLE      | tbla  | NULL       | index | NULL          | idx_tbla_agebrith | 9       | NULL |    3 |    33.33 | Using where; Using index |                       
+----+-------------+-------+------------+-------+---------------+-------------------+---------+------+------+----------+--------------------------+                       
1 row in set, 1 warning (0.00 sec)                                                                                                                                        
                                                                                                                                                                          
mysql> explain select * from tbla where birth > '2019-01-01 00:00:00' order by birth;                                                                                     
+----+-------------+-------+------------+-------+---------------+-------------------+---------+------+------+----------+------------------------------------------+       
| id | select_type | table | partitions | type  | possible_keys | key               | key_len | ref  | rows | filtered | Extra                                    |       
+----+-------------+-------+------------+-------+---------------+-------------------+---------+------+------+----------+------------------------------------------+       
|  1 | SIMPLE      | tbla  | NULL       | index | NULL          | idx_tbla_agebrith | 9       | NULL |    3 |    33.33 | Using where; Using index; Using filesort |       
+----+-------------+-------+------------+-------+---------------+-------------------+---------+------+------+----------+------------------------------------------+       
1 row in set, 1 warning (0.00 sec)                                                                                                                                        
                                                                                                                                                                          
mysql> explain select * from tbla where age > 30 order by age;                                                                                                            
+----+-------------+-------+------------+-------+-------------------+-------------------+---------+------+------+----------+--------------------------+                   
| id | select_type | table | partitions | type  | possible_keys     | key               | key_len | ref  | rows | filtered | Extra                    |                   
+----+-------------+-------+------------+-------+-------------------+-------------------+---------+------+------+----------+--------------------------+                   
|  1 | SIMPLE      | tbla  | NULL       | range | idx_tbla_agebrith | idx_tbla_agebrith | 5       | NULL |    1 |   100.00 | Using where; Using index |                   
+----+-------------+-------+------------+-------+-------------------+-------------------+---------+------+------+----------+--------------------------+                   
1 row in set, 1 warning (0.00 sec)                                                                                                                                        
                                                                                                                                                                          
mysql> explain select * from tbla where age > 30 order by birth;                                                                                                          
+----+-------------+-------+------------+-------+-------------------+-------------------+---------+------+------+----------+------------------------------------------+   
| id | select_type | table | partitions | type  | possible_keys     | key               | key_len | ref  | rows | filtered | Extra                                    |   
+----+-------------+-------+------------+-------+-------------------+-------------------+---------+------+------+----------+------------------------------------------+   
|  1 | SIMPLE      | tbla  | NULL       | range | idx_tbla_agebrith | idx_tbla_agebrith | 5       | NULL |    1 |   100.00 | Using where; Using index; Using filesort |   
+----+-------------+-------+------------+-------+-------------------+-------------------+---------+------+------+----------+------------------------------------------+   
1 row in set, 1 warning (0.00 sec)                                                                                                                                        
 
 
 '''
练习题
索引 a_b_c(a,b,c)

索引语句                                                  是否会有Using filesort,即文件内排序情况(需要优化)
order by a,b                                                a             不会出现Using filesort
order by a,b,c                                              a、b、c        不会出现Using filesort
order by a desc,b desc,c desc                               a、b、c        不会出现Using filesort

where a = const order by b,c                                a、b、c        不会出现Using filesort
where a = const and b = const order by c                    a、b、c        不会出现Using filesort
where a = const and b > const order by b,c                  a、b、c        不会出现Using filesort      --注意

order by a asc,b desc,c desc                                a            会出现Using filesort
where g = const order by b,c                                无           会出现Using filesort     
where a = const order by c                                  a            会出现Using filesort
where a = const order by a,d                                a            会出现Using filesort
'''
                                                                                                                                                                                                                                                                                                                                          

3、慢查询日志
– MySQL的慢查询日志MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阙值的语句,具体指运行时间超过long_query_time值得SQL,则会被记录到慢查询日志中
– 具体指运行时间超过long_query_time值得SQL,则会被记录到慢查询日志中。long_query_time的默认值为10,意思是运行10秒以上的语句。
– 由他来查看那些SQL超出了我们的最大忍耐时间值,比如一条SQL执行超过5秒钟,我们就算是慢SQL,希望能收集超过5秒的SQL,结合之前explain进行全面分析
默认情况下,MySQL数据库没有开启慢查询日志,需要我们手动来设置这个参数
– 当然如果不是调优需要的话,一般不建议启动该参数,因为开启慢查询日志会或多或少带来一定的性能影响。慢查询日志支持将日志记录写入文件

mysql> show variables like '%slow_query_log%';
+---------------------+-------------------------------------------------------------------------------+
| Variable_name       | Value                                                                         |
+---------------------+-------------------------------------------------------------------------------+
| slow_query_log      | OFF                                                                           |
| slow_query_log_file | G:\phpstudy\phpstudy_pro\Extensions\MySQL8.0.12\data\AFOJE-901131137-slow.log |
+---------------------+-------------------------------------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)

mysql> set slow_query_log = ON;
ERROR 1229 (HY000): Variable 'slow_query_log' is a GLOBAL variable and should be set with SET GLOBAL
mysql> set global slow_query_log = ON;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%slow_query_log%';
+---------------------+-------------------------------------------------------------------------------+
| Variable_name       | Value                                                                         |
+---------------------+-------------------------------------------------------------------------------+
| slow_query_log      | ON                                                                            |
| slow_query_log_file | G:\phpstudy\phpstudy_pro\Extensions\MySQL8.0.12\data\AFOJE-901131137-slow.log |
+---------------------+-------------------------------------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)

mysql> --上面的操作只是临时修改参数,重启SQL会恢复默认值

如果需要用就修改参数需要在下面的界面中修改:(不建议使用,打开慢查询日志会影响运行性能,只会在检测时候使用
数据库学习——07—Mysql高级(分析SQL步骤 、排序优化、慢查询日志使用、批量插入数据和创建函数、Show Profile进行SQL分析)_第1张图片
上图中的设置还处于被注释的状态,修改完成需要重启服务器,然后的状态就是慢查询日志一直处于开启状态,此时采用的只是暂时开启,重启之后恢复默认值的状态,慢查询日志的保存地址也没有进行修改。

slow_query_log = 1
slow_query_log_file = G:/phpstudy/PHPTutorial/MySQL/slow_log.txt
mysql> show variables like '%slow_query_log%';
+---------------------+-------------------------------------------------------------------------------+
| Variable_name       | Value                                                                         |
+---------------------+-------------------------------------------------------------------------------+
| slow_query_log      | ON                                                                            |
| slow_query_log_file | G:\phpstudy\phpstudy_pro\Extensions\MySQL8.0.12\data\AFOJE-901131137-slow.log |
+---------------------+-------------------------------------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)

mysql> show variables like 'long_query_time%';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set, 1 warning (0.00 sec)

mysql> set global long_query_time=3;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'long_query_time%';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set, 1 warning (0.00 sec)

mysql> --设置完long_query_time的值需要重启服务器才行
    ->

重启服务器继续查询

C:\Users\Administrator
λ mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use `mysql`;
Database changed
mysql> show variables like 'long_query_time%';
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 3.000000 |
+-----------------+----------+
1 row in set, 1 warning (0.00 sec)

mysql> select sleep(5);
+----------+
| sleep(5) |
+----------+
|        0 |
+----------+
1 row in set (5.01 sec)

mysql> show variables like '%slow_query_log%';
+---------------------+-------------------------------------------------------------------------------+
| Variable_name       | Value                                                                         |
+---------------------+-------------------------------------------------------------------------------+
| slow_query_log      | ON                                                                            |
| slow_query_log_file | G:\phpstudy\phpstudy_pro\Extensions\MySQL8.0.12\data\AFOJE-901131137-slow.log |
+---------------------+-------------------------------------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)                                                                      
                                                                                                           

数据库学习——07—Mysql高级(分析SQL步骤 、排序优化、慢查询日志使用、批量插入数据和创建函数、Show Profile进行SQL分析)_第2张图片
慢查询日志工具

s:表示按照何种方式排序
c:访问次数
l:锁定时间
r:返回记录
t:查询时间
al:平均锁定时间
ar:平均返回记录数
t:即为返回前面多少条的数据

举例:

得到返回记录集最多的10SQL
mysqldumpslow -s r -t 10 D:/phpStudy/PHPTutorial/MySQL/slow_log.txt

得到访问次数最多的10SQL
mysqldumpslow -s c -t 10 D:/phpStudy/PHPTutorial/MySQL/slow_log.txt

4、
批量插入数据
函数和存储过程

部门表

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;

员工表

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;

创建函数
创建函数,假如报错:this function has none of DETERMINISTIC… 查看参数(重要)

set global log_bin_trust_function_creators=1; --假如下面的函数创建过程报错,需要先执行这句之后再创建函数

随机产生字符串函数

DELIMITER $$                --$$ 是分隔符   下面的函数主体语句类似于C语言
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));  --CONCAT 拼接函数
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_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_string(8));
UNTIL i = max_num
END REPEAT;
COMMIT;                                      --统一提交插入的值
END $$

调用存储过程执行部分

delimiter ; 
call insert_dept(100,10);    --调用存储过程
    
delimiter ; 
call insert_emp(100001,500000);   --调用存储过程

以上在SQLyog软件界面中操作会有如下效果(F9执行):
数据库学习——07—Mysql高级(分析SQL步骤 、排序优化、慢查询日志使用、批量插入数据和创建函数、Show Profile进行SQL分析)_第3张图片
或者在cmder直接操作:
数据库学习——07—Mysql高级(分析SQL步骤 、排序优化、慢查询日志使用、批量插入数据和创建函数、Show Profile进行SQL分析)_第4张图片
创建函数之后可以在列表左边看到函数名称
数据库学习——07—Mysql高级(分析SQL步骤 、排序优化、慢查询日志使用、批量插入数据和创建函数、Show Profile进行SQL分析)_第5张图片
数据库学习——07—Mysql高级(分析SQL步骤 、排序优化、慢查询日志使用、批量插入数据和创建函数、Show Profile进行SQL分析)_第6张图片

5、Show Profile进行SQL分析
数据库学习——07—Mysql高级(分析SQL步骤 、排序优化、慢查询日志使用、批量插入数据和创建函数、Show Profile进行SQL分析)_第7张图片
SQL中通用的查询语句是

show variables like 'XXXX';    --分号内是想要查询的名称

SQL中通用的设置数值语句是

set global XXXX ='...';    --分号内是想要设置的的数值

如下图所示:
数据库学习——07—Mysql高级(分析SQL步骤 、排序优化、慢查询日志使用、批量插入数据和创建函数、Show Profile进行SQL分析)_第8张图片
Show Profile分析步骤
1.是否支持,看看当前MySQL版本是否支持
2.开启功能,默认是关闭,使用前需要开启

type
all --------------------显示所有的开销信息
block io ------------ 显示块IO相关开销
cpu ------------------显示CPU相关开销信息
ipc -------------------显示发送和接收相关开销信息
memory ------------显示内存相关开销信息
page faults --------显示页面错误相关开销信息

参数注意
converting HEAP to MyISAM----------查询结果太大,内存都不够用了往磁盘上搬
Creating tmp table----------------------- 创建临时表
Copying to tmp table on disk ----------把内存中临时表复制到磁盘,危险
locked

mysql> select * from emp group by id%10 limit 15000;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'python_01.emp.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
mysql> select @@global.sql_model;
ERROR 1193 (HY000): Unknown system variable 'sql_model'
mysql> select @@global.sql_mode;
+-----------------------------------------------------------------------------------------------------------------------+
| @@global.sql_mode                                                                                                     |
+-----------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> set @@global.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)

mysql> ---需要重启SQL服务器

上面是解决出现的错误: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated…

mysql> show variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling     | OFF   |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)

mysql> set profiling = on;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling     | ON    |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)

mysql> show profiles;
+----------+------------+---------------------------------+
| Query_ID | Duration   | Query                           |
+----------+------------+---------------------------------+
|        1 | 0.00167500 | show variables like 'profiling' |
+----------+------------+---------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select * from students;
+----+------+------+--------+--------+--------+------------+-----------+
| id | name | age  | high   | gender | cls_id | birth      | is_delete |
+----+------+------+--------+--------+--------+------------+-----------+
|  1 ||   23 | 175.88 ||      1 | NULL       |         0 |
|  3 | 2    |   18 | 188.00 ||      1 | 1997-01-01 |         0 |
|  4 | 3    |    4 | 178.00 ||      1 | 1997-01-01 |         0 |
|  5 | 4    |   20 | 168.00 ||      1 | 1997-01-01 |         0 |
|  6 | 5    |   19 | 178.00 ||      1 | 1997-01-01 |         1 |
|  7 | 小乔 |   18 |   NULL ||   NULL | 1997-01-01 |         0 |
|  8 | 周瑜 |   21 |   NULL ||   NULL | 1997-01-01 |         0 |
|  9 | 空格 |   22 |   NULL ||   NULL | 1997-01-01 |         0 |
+----+------+------+--------+--------+--------+------------+-----------+
8 rows in set (0.00 sec)

mysql> select * from students group by age limit 3;
+----+------+------+--------+--------+--------+------------+-----------+
| id | name | age  | high   | gender | cls_id | birth      | is_delete |
+----+------+------+--------+--------+--------+------------+-----------+
|  1 ||   23 | 175.88 ||      1 | NULL       |         0 |
|  3 | 2    |   18 | 188.00 ||      1 | 1997-01-01 |         0 |
|  4 | 3    |    4 | 178.00 ||      1 | 1997-01-01 |         0 |
+----+------+------+--------+--------+--------+------------+-----------+
3 rows in set (0.00 sec)

mysql> select * from emp group by id%10 limit 15000;
+----+--------+--------+----------+-----+------------+---------+--------+--------+
| id | empno  | ename  | job      | mgr | hiredate   | sal     | comm   | deptno |
+----+--------+--------+----------+-----+------------+---------+--------+--------+
|  1 | 100002 | FmuoiX | SALESMAN |   1 | 2019-12-19 | 2000.00 | 400.00 |    103 |
|  2 | 100003 | KBEscg | SALESMAN |   1 | 2019-12-19 | 2000.00 | 400.00 |    104 |
|  3 | 100004 | aIosYO | SALESMAN |   1 | 2019-12-19 | 2000.00 | 400.00 |    109 |
|  4 | 100005 | zEvqtU | SALESMAN |   1 | 2019-12-19 | 2000.00 | 400.00 |    103 |
|  5 | 100006 | ZWJJgJ | SALESMAN |   1 | 2019-12-19 | 2000.00 | 400.00 |    100 |
|  6 | 100007 | ryocvT | SALESMAN |   1 | 2019-12-19 | 2000.00 | 400.00 |    101 |
|  7 | 100008 | lFugwP | SALESMAN |   1 | 2019-12-19 | 2000.00 | 400.00 |    107 |
|  8 | 100009 | kQAfZJ | SALESMAN |   1 | 2019-12-19 | 2000.00 | 400.00 |    101 |
|  9 | 100010 | NwVmyE | SALESMAN |   1 | 2019-12-19 | 2000.00 | 400.00 |    105 |
| 10 | 100011 | ieThea | SALESMAN |   1 | 2019-12-19 | 2000.00 | 400.00 |    107 |
+----+--------+--------+----------+-----+------------+---------+--------+--------+
10 rows in set (0.46 sec)

mysql> show profiles;
+----------+------------+----------------------------------------------+
| Query_ID | Duration   | Query                                        |
+----------+------------+----------------------------------------------+
|        1 | 0.00167500 | show variables like 'profiling'              |
|        2 | 0.00022675 | select * from students                       |
|        3 | 0.00027575 | select * from students group by age limit 3  |
|        4 | 0.45913750 | select * from emp group by id%10 limit 15000 |
+----------+------------+----------------------------------------------+
4 rows in set, 1 warning (0.00 sec)

mysql> show profile cpu,block io for query 2;
+----------------------+----------+----------+------------+--------------+---------------+
| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting             | 0.000059 | 0.000000 |   0.000000 |         NULL |          NULL |
| checking permissions | 0.000005 | 0.000000 |   0.000000 |         NULL |          NULL |
| Opening tables       | 0.000032 | 0.000000 |   0.000000 |         NULL |          NULL |
| init                 | 0.000003 | 0.000000 |   0.000000 |         NULL |          NULL |
| System lock          | 0.000008 | 0.000000 |   0.000000 |         NULL |          NULL |
| optimizing           | 0.000003 | 0.000000 |   0.000000 |         NULL |          NULL |
| statistics           | 0.000009 | 0.000000 |   0.000000 |         NULL |          NULL |
| preparing            | 0.000005 | 0.000000 |   0.000000 |         NULL |          NULL |
| executing            | 0.000002 | 0.000000 |   0.000000 |         NULL |          NULL |
| Sending data         | 0.000055 | 0.000000 |   0.000000 |         NULL |          NULL |
| end                  | 0.000002 | 0.000000 |   0.000000 |         NULL |          NULL |
| query end            | 0.000004 | 0.000000 |   0.000000 |         NULL |          NULL |
| closing tables       | 0.000005 | 0.000000 |   0.000000 |         NULL |          NULL |
| freeing items        | 0.000030 | 0.000000 |   0.000000 |         NULL |          NULL |
| cleaning up          | 0.000007 | 0.000000 |   0.000000 |         NULL |          NULL |
+----------------------+----------+----------+------------+--------------+---------------+
15 rows in set, 1 warning (0.00 sec)

mysql> show profile cpu,block io for query 3;
+----------------------+----------+----------+------------+--------------+---------------+
| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting             | 0.000058 | 0.000000 |   0.000000 |         NULL |          NULL |
| checking permissions | 0.000004 | 0.000000 |   0.000000 |         NULL |          NULL |
| Opening tables       | 0.000026 | 0.000000 |   0.000000 |         NULL |          NULL |
| init                 | 0.000004 | 0.000000 |   0.000000 |         NULL |          NULL |
| System lock          | 0.000005 | 0.000000 |   0.000000 |         NULL |          NULL |
| optimizing           | 0.000002 | 0.000000 |   0.000000 |         NULL |          NULL |
| statistics           | 0.000009 | 0.000000 |   0.000000 |         NULL |          NULL |
| preparing            | 0.000007 | 0.000000 |   0.000000 |         NULL |          NULL |
| Creating tmp table   | 0.000033 | 0.000000 |   0.000000 |         NULL |          NULL |
| executing            | 0.000002 | 0.000000 |   0.000000 |         NULL |          NULL |
| Sending data         | 0.000065 | 0.000000 |   0.000000 |         NULL |          NULL |
| end                  | 0.000003 | 0.000000 |   0.000000 |         NULL |          NULL |
| query end            | 0.000004 | 0.000000 |   0.000000 |         NULL |          NULL |
| removing tmp table   | 0.000008 | 0.000000 |   0.000000 |         NULL |          NULL |
| query end            | 0.000002 | 0.000000 |   0.000000 |         NULL |          NULL |
| closing tables       | 0.000005 | 0.000000 |   0.000000 |         NULL |          NULL |
| freeing items        | 0.000035 | 0.000000 |   0.000000 |         NULL |          NULL |
| cleaning up          | 0.000007 | 0.000000 |   0.000000 |         NULL |          NULL |
+----------------------+----------+----------+------------+--------------+---------------+
18 rows in set, 1 warning (0.00 sec)

mysql> show profile cpu,block io for query 4;
+----------------------+----------+----------+------------+--------------+---------------+
| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting             | 0.000060 | 0.000000 |   0.000000 |         NULL |          NULL |
| checking permissions | 0.000004 | 0.000000 |   0.000000 |         NULL |          NULL |
| Opening tables       | 0.000033 | 0.000000 |   0.000000 |         NULL |          NULL |
| init                 | 0.000005 | 0.000000 |   0.000000 |         NULL |          NULL |
| System lock          | 0.000007 | 0.000000 |   0.000000 |         NULL |          NULL |
| optimizing           | 0.000003 | 0.000000 |   0.000000 |         NULL |          NULL |
| statistics           | 0.000023 | 0.000000 |   0.000000 |         NULL |          NULL |
| preparing            | 0.000009 | 0.000000 |   0.000000 |         NULL |          NULL |
| Creating tmp table   | 0.000030 | 0.000000 |   0.000000 |         NULL |          NULL |
| executing            | 0.000003 | 0.000000 |   0.000000 |         NULL |          NULL |
| Sending data         | 0.458857 | 0.468750 |   0.000000 |         NULL |          NULL |
| end                  | 0.000012 | 0.000000 |   0.000000 |         NULL |          NULL |
| query end            | 0.000008 | 0.000000 |   0.000000 |         NULL |          NULL |
| removing tmp table   | 0.000008 | 0.000000 |   0.000000 |         NULL |          NULL |
| query end            | 0.000003 | 0.000000 |   0.000000 |         NULL |          NULL |
| closing tables       | 0.000006 | 0.000000 |   0.000000 |         NULL |          NULL |
| freeing items        | 0.000058 | 0.000000 |   0.000000 |         NULL |          NULL |
| cleaning up          | 0.000011 | 0.000000 |   0.000000 |         NULL |          NULL |
+----------------------+----------+----------+------------+--------------+---------------+
18 rows in set, 1 warning (0.00 sec)

mysql> explain select * from emp group by id%10 limit 15000;
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-----------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra           |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-----------------+
|  1 | SIMPLE      | emp   | NULL       | ALL  | PRIMARY       | NULL | NULL    | NULL | 498316 |   100.00 | Using temporary |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-----------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from students group by age limit 3;
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-----------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra           |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-----------------+
|  1 | SIMPLE      | students | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    8 |   100.00 | Using temporary |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-----------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from students;
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------+
|  1 | SIMPLE      | students | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    8 |   100.00 | NULL  |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

全局查询日志
(生产环境不要打开,会记录每一条sql语句,数据量大,无意义)
(测试环境中复现SQL语句问题才使用)

开启命令
set global general_log = 1;SQL语句写到表中
set global log_output = 'TABLE';

你所编写的SQL语句,会记录到MySQL库里的genral_log表
select * from mysql.general_log;

你可能感兴趣的:(数据库的学习,python,mysql)