MySQL 高级

mysql 常用的操作

Posted by lijian on January 20, 2019

查询数据库隔离级别

select @@tx_isolation;

显示数据库连接

show processlist;

explain extended 查看执行计划,注意这个语法会比 explain 多一个 filter 列

explain extended select * from yunying.org_students where org_id in( select org_id from yunying.org_info where org_id=3794) limit 1;

查看格式化后的sql , 或者是 经过优化器之后的sql,一般结合 explain extended 一起使用,同时执行

show WARNINGS;

下面是上面那个sql , show WARNINGS 结果,可以看到,子查询变为了 join 语句

/* select#1 */ select * from yunying.org_info join yunying.org_students where ((yunying.org_students.org_id = 3794)) limit 1

查看各种参数

show variables

查询大于10s的长事务

select * from information_schema.innodb_trx where TIME_TO_SEC(timediff(now(),trx_started))>10

对于 组合索引 myindex(a,b,c)

where a=’23’ # 使用索引 where a=’ds’ and c=’2’ # 使用索引 where b=’23’ and c=’2323’ # 不使用索引 where b=’23’ and a=’2323’ # 使用索引 where c=’2342’ and b=’23’ and a=’2323’ # 使用索引 where a like ‘2%’ and b=’23’ # 一定注意这个,这里使用索引是没有疑问的,但是用到哪个字段呢,根据mysql 版本来看,5.6 之前只会使用 a , 5.6 之后引入了索引下推的概念,也就是在普通索引树中也会去判断第二个字段,如果第二个字段不符合,就不会执行回表操作

结论:where 条件中的 a,b,c 顺序无要求,优化器会自动优化,但是索引却是B+,所以必须匹配左前缀

查询表索引使用情况

show INDEX from yunying.org_students;

上面查询的这个统计信息很可能是不准确,造成索引使用不准确,可以重新分析一下表,再次执行索引应该就使用准确了

analyze table yunying.org_students

force index 强制使用索引

select * from yunying.org_students force index(idx_name_mobile) where mobile=18783008726;