mysql 查看进程 null_(全栈须知)7.SQL语句优化(三) 之MySQL数据库慢查询问题排查...

0.准备

show variables like '%log_output%';

-- log_output='FILE|TABLE' 默认是保存到文件中

show variables like '%slow_query_log%'; #查看当前开启状态

-- slow_query_log_file:/usr/local/mysql/var/cffycls-PC-slow.log

#在配置文件中修改或添加,并重启服务

slow_query_log 1 #开启慢查询日志 set GLOBAL slow_query_log=1

# 保存为table类型时,可以查询到信息字段类型:

DESC `mysql`.`slow_log`

field

type

null

key

default

extra

start_time

timestamp(6)

NO

CURRENT_TIMESTAMP(6)

DEFAULT_GENERATED on update CURRENT_TIMESTAMP(6)

user_host

mediumtext

NO

query_time

time(6)

NO

lock_time

time(6)

NO

rows_sent

int(11)

NO

rows_examined

int(11)

NO

db

varchar(512)

NO

last_insert_id

int(11)

NO

insert_id

int(11)

NO

server_id

int(10) unsigned

NO

sql_text

mediumblob

NO

thread_id

bigint(21) unsigned

NO

日志文件 [8.0.13默认没有开启slow_query_log=1时,保存的慢操作日志]

/usr/local/mysql/bin/mysqld, Version: 8.0.13 (Source distribution). started with:

Tcp port: 3306 Unix socket: /tmp/mysql.sock

Time Id Command Argument

# Time: 2019-08-26T00:06:31.481150Z

# User@Host: root[root] @ localhost [] Id: 174

# Query_time: 17.501530 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0

use optmize;

SET timestamp=1566777991;

call add_vote_record_memory(10000);

# Time: 2019-08-26T00:09:48.696806Z

# User@Host: root[root] @ localhost [] Id: 174

# Query_time: 126.785068 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0

SET timestamp=1566778188;

call add_vote_record_memory(80000);

# Time: 2019-08-26T00:42:43.720150Z

# User@Host: root[root] @ localhost [] Id: 174

# Query_time: 123.697077 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0

SET timestamp=1566780163;

CALL add_vote_record_memory(100000);

# Time: 2019-08-26T01:08:11.645078Z

# User@Host: root[root] @ localhost [] Id: 174

# Query_time: 1001.577474 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0

SET timestamp=1566781691;

CALL add_vote_record_memory(800000);

/usr/local/mysql/bin/mysqld, Version: 8.0.13 (Source distribution). started with:

Tcp port: 3306 Unix socket: /tmp/mysql.sock

Time Id Command Argument

root@cffycls-PC:/home/wwwroot/default# cat /usr/local/mysql/var/cffycls-PC-slow.log

/usr/local/mysql/bin/mysqld, Version: 8.0.13 (Source distribution). started with:

Tcp port: 3306 Unix socket: /tmp/mysql.sock

Time Id Command Argument

# Time: 2019-08-26T00:06:31.481150Z

# User@Host: root[root] @ localhost [] Id: 174

# Query_time: 17.501530 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0

use optmize;

SET timestamp=1566777991;

call add_vote_record_memory(10000);

# Time: 2019-08-26T00:09:48.696806Z

# User@Host: root[root] @ localhost [] Id: 174

# Query_time: 126.785068 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0

SET timestamp=1566778188;

call add_vote_record_memory(80000);

# Time: 2019-08-26T00:42:43.720150Z

# User@Host: root[root] @ localhost [] Id: 174

# Query_time: 123.697077 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0

SET timestamp=1566780163;

CALL add_vote_record_memory(100000);

# Time: 2019-08-26T01:08:11.645078Z

# User@Host: root[root] @ localhost [] Id: 174

# Query_time: 1001.577474 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0

SET timestamp=1566781691;

CALL add_vote_record_memory(800000);

/usr/local/mysql/bin/mysqld, Version: 8.0.13 (Source distribution). started with:

Tcp port: 3306 Unix socket: /tmp/mysql.sock

Time Id Command Argument

# Time: 2019-10-08T00:59:30.702335Z

# User@Host: root[root] @ [192.168.1.102] Id: 13

# Query_time: 17.960220 Lock_time: 0.000272 Rows_sent: 0 Rows_examined: 0

use optmize;

SET timestamp=1570496370;

call add_vote_record_memory(10000);

a.直接杀进程:select processlist

查看系统进程,笔记《2.mysql数据库状态》

查看当前进行操作的进程状态

ID

USER

HOST

DB

COMMAND

TIME

STATE

INFO

48

root

192.168.1.102:50410

test

Sleep

1167

NULL

49

root

192.168.1.102:50411

NULL

Sleep

1167

NULL

59

root

localhost

NULL

Query

0

executing

select * from information_schema.processlist

4

event_scheduler

localhost

NULL

Daemon

1293

Waiting on empty queue

NULL

44

root

192.168.1.102:50391

optmize

Sleep

1024

NULL

45

root

192.168.1.102:50394

information_schema

Sleep

225

NULL

46

root

192.168.1.102:50397

information_schema

Sleep

991

NULL

47

root

192.168.1.102:50398

NULL

Sleep

1166

NULL

select * from information_schema.processlist;

#查看当前正在进行的查询并按照已经执行时间倒排

select * from information_schema.processlist where info is not null order by time desc;

查看有多少哪些查询处于lock状态,或者已经执行了很长时间,把这些process用kill指令干掉。

mysql>kill 47;

b.分析慢的原因:explain慢日志分析

type列

依次从好到差:system,const,eq_ref,ref,fulltext,ref_or_null,unique_subquery,index_subquery,range,index_merge,index,ALL,除了all之外,其他的type都可以使用到索引,除了index_merge之外,其他的type只可以用到一个索引

A:system:表中只有一行数据或者是空表,且只能用于myisam和memory表。如果是Innodb引擎表,type列在这个情况通常都是all或者index

B:const:使用唯一索引或者主键,返回记录一定是1行记录的等值where条件时,通常type是const。其他数据库也叫做唯一索引扫描

C:eq_ref:出现在要连接过个表的查询计划中,驱动表只返回一行数据,且这行数据是第二个表的主键或者唯一索引,且必须为not null,唯一索引和主键是多列时,只有所有的列都用作比较时才会出现eq_ref

D:ref:不像eq_ref那样要求连接顺序,也没有主键和唯一索引的要求,只要使用相等条件检索时就可能出现,常见与辅助索引的等值查找。或者多列主键、唯一索引中,使用第一个列之外的列作为等值查找也会出现,总之,返回数据不唯一的等值查找就可能出现。

E:fulltext:全文索引检索,要注意,全文索引的优先级很高,若全文索引和普通索引同时存在时,mysql不管代价,优先选择使用全文索引

F:ref_or_null:与ref方法类似,只是增加了null值的比较。实际用的不多。

G:unique_subquery:用于where中的in形式子查询,子查询返回不重复值唯一值

H:index_subquery:用于in形式子查询使用到了辅助索引或者in常数列表,子查询可能返回重复值,可以使用索引将子查询去重。

I:range:索引范围扫描,常见于使用>,

J:index_merge:表示查询使用了两个以上的索引,最后取交集或者并集,常见and ,or的条件使用了不同的索引,官方排序这个在ref_or_null之后,但是实际上由于要读取所个索引,性能可能大部分时间都不如range

K:index:索引全表扫描,把索引从头到尾扫一遍,常见于使用索引列就可以处理不需要读取数据文件的查询、可以使用索引排序或者分组的查询。

L:all:这个就是全表扫描数据文件,然后再在server层进行过滤返回符合要求的记录。

extra列

这个列可以显示的信息非常多,有几十种,常用的有

A:distinct:在select部分使用了distinc关键字

B:no tables used:不带from字句的查询或者From dual查询

C:使用not in()形式子查询或not exists运算符的连接查询,这种叫做反连接。即,一般连接查询是先查询内表,再查询外表,反连接就是先查询外表,再查询内表。

D:using filesort:排序时无法使用到索引时,就会出现这个。常见于order by和group by语句中

E:using index:查询时不需要回表查询,直接通过索引就可以获取查询的数据。

F:using join buffer(block nested loop),using join buffer(batched key accss):5.6.x之后的版本优化关联查询的BNL,BKA特性。主要是减少内表的循环数量以及比较顺序地扫描查询。

G:using sort_union,using_union,using intersect,using sort_intersection:

using intersect:表示使用and的各个索引的条件时,该信息表示是从处理结果获取交集

using union:表示使用or连接各个使用索引的条件时,该信息表示从处理结果获取并集

using sort_union和using sort_intersection:与前面两个对应的类似,只是他们是出现在用and和or查询信息量大时,先查询主键,然后进行排序合并后,才能读取记录并返回。

H:using temporary:表示使用了临时表存储中间结果。临时表可以是内存临时表和磁盘临时表,执行计划中看不出来,需要查看status变量,used_tmp_table,used_tmp_disk_table才能看出来。

I:using where:表示存储引擎返回的记录并不是所有的都满足查询条件,需要在server层进行过滤。查询条件中分为限制条件和检查条件,5.6之前,存储引擎只能根据限制条件扫描数据并返回,然后server层根据检查条件进行过滤再返回真正符合查询的数据。5.6.x之后支持ICP特性,可以把检查条件也下推到存储引擎层,不符合检查条件和限制条件的数据,直接不读取,这样就大大减少了存储引擎扫描的记录数量。extra列显示using index condition

J:firstmatch(tb_name):5.6.x开始引入的优化子查询的新特性之一,常见于where字句含有in()类型的子查询。如果内表的数据量比较大,就可能出现这个

K:loosescan(m..n):5.6.x之后引入的优化子查询的新特性之一,在in()类型的子查询中,子查询返回的可能有重复记录时,就可能出现这个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值