Mysql优化笔记

sql优化调整:

explain select ...: 执行计划
执行结果分析:
select_type: 查询类型
	SIMPLE:不包含子查询
	PRIMARY:复杂sql最外层的类型
type:遍历类型
	ALL:遍历全表匹配行
	index: 只遍历索引树
	range:索引范围扫描
key: 是否实际使用索引:
	NULL,则没有使用索引

是消耗IO多,还是CPU计算多?

set profiling = 1; 开始sql语句分析
show profiles; 查看执行sql时间
show profile for query 1; 查看第一个sql执行的各个耗时详情
show profile cpu for query 1; 查看第一个sql执行的cpu耗时详情
show profile all for query 1; 查看第一个sql执行的耗时详情
SHOW profile CPU,BLOCK IO FOR query 2;   一般查询cpu和io详情
	type: 
	   ALL                --显示所有的开销信息 
	 | BLOCK IO          --显示块IO相关开销 
	 | CONTEXT SWITCHES  --上下文切换相关开销 
	 | CPU                --显示CPU相关开销信息 
	 | IPC                --显示发送和接收相关开销信息 
	 | MEMORY            --显示内存相关开销信息 
	 | PAGE FAULTS        --显示页面错误相关开销信息 
	 | SOURCE            --显示和Source_function,Source_file,Source_line相关的开销信息 
	 | SWAPS              --显示交换次数相关开销的信息
	
	+----------------------+----------+----------+------------+
	starting:开始
	checking permissions:检查权限
	Opening tables:打开表
	init : 初始化
	System lock :系统锁
	optimizing : 优化    
	statistics : 统计  *
	preparing :准备    
	executing :执行    *
	Sending data :发送数据  *
	Sorting result :排序  *
	end :结束
	query end :查询 结束
	closing tables : 关闭表 /去除TMP 表
	freeing items : 释放物品
	cleaning up :清理
	+----------------------+----------+----------+------------+
	
show variables like "%version%" 查看数据库版本信息
show variables like "%profil%"; 查看sql分析开发是否开启
set profiling = 0; 测试完成需要关闭

一般简易的流程:
(1)开启profile分析
(2)执行sql
(3)查看sql分析结果
(4)SHOW profile CPU,BLOCK IO io FOR query 1; //查看指定sql的CPU、IO消耗
(5)关闭profile分析
参考:https://segmentfault.com/a/1190000016351095

查询数据量和索引量
select 
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
order by data_length desc, index_length desc;

查看当前线程处理情况:
	show processlist 或 show full processlist
杀死某个进程:
	kill 183665
查看未睡眠的进程:
	select id, db, user, host, command, time, state, info
		from information_schema.processlist
		where command != 'Sleep'
		order by time desc

参考: https://blog.csdn.net/ty_hf/article/details/54895026	

  

posted @ 2019-10-17 11:04  外科手术医生  阅读(146)  评论(0编辑  收藏  举报