茂展的分享博客

mysql优化后续总结

mysql优化后续总结

show status like ‘innodb_row_lock%’ 查看行级锁的状态

show status like ‘table%’ 查看表级锁的状态

explain

id select_key table type key possible_key key_len ref rows extra

执行顺序

id相同,从上往下执行
id不同,从大向小执行
type: all->index->range->ref->eq_ref->const->system
possible_key: 可能用到的索引
key: 实际用到的索引
ref: const,实际引用
extra: using index using where using filesort(效率偏差,最左匹配原则断层) using temporty(从上往下)

写sql的时候注意点

  1. 不对索引建立操作
  2. != <>
  3. 适当选择使用 exists in
  4. 索引最左匹配
  5. order by 、group by和where一样,根据最左匹配原则合理创建索引
  6. 字符类型一定要加双引号

索引操作

创建索引

create index 索引名 on 表名(索引列1,..)
alter table 表名 add index 索引名(索引列1,..)

删除索引

drop index 索引名 on 表名

查找所有的索引

show index from 表名

执行过程详细查看

接着我们需要详细查看执行情况

show variables like ‘profiling’

默认关闭

set global profiling = 1;打开
然后 使用 show profiles 查看所有的语句执行

然后根据某个语句执行id

show profile cpu,block io 或者(all) for query id(上面的id);
可以获得哪个执行的秒数情况,用来排查

其实我们还可以使用mysql全局监测

set global general_log = 1;默认关闭,是自带数据库mysql中的
select * from mysql.general_log;获得以往执行情况

mysql锁

表级索 行级索 inndb
表级索 myisam

mysql5.5以后默认innodb

以 innodb为例:

首先 设置读锁
lock table 表名 read;

不可以对表进行写,但是可以读,只能读当前表
其他session可以读,但是呢,写的话会阻塞

unlock tables; 释放所有的锁

设置写锁

lock table 表名 write;

不能写也不能读

unlock tables;

------本文结束感谢阅读------
🐶 您的支持将鼓励我继续创作 🐶