文章目录

  • 学习资料
  • SQL语句
  • 查看系统性能
  • 查看表
  • 查看索引



SQL语句

查看系统性能

-- 查看数据库最大连接数
show variables like '%max_connections%';

-- 查看目前正在使用的连接数量
show global status like 'Max_used_connections';

-- 查看会话连接
show processlist;

-- 查看
show status like 'last_query_cost';

-- 查看MySQL本次启动后的运行时间(单位:秒)
show status like 'uptime';

--查看select语句的执行数
show [global] status like 'com_select';

--查看insert语句的执行数
show [global] status like 'com_insert';

--查看update语句的执行数
show [global] status like 'com_update';

--查看delete语句的执行数
show [global] status like 'com_delete';

--查看试图连接到MySQL(不管是否连接成功)的连接数
show status like 'connections';

--查看线程缓存内的线程的数量。
show status like 'threads_cached';

--查看当前打开的连接的数量。
show status like 'threads_connected';

--查看当前打开的连接的数量。
show status like 'threads_connected';

--查看创建用来处理连接的线程数。如果Threads_created较大,你可能要增加thread_cache_size值。
show status like 'threads_created';

--查看激活的(非睡眠状态)线程数。
show status like 'threads_running';


--查看立即获得的表的锁的次数。
show status like 'table_locks_immediate';

-- 查看不能立即获得的表的锁的次数。如果该值较高,并且有性能问题,你应首先优化查询,然后拆分表或使用复制。
show status like 'table_locks_waited';

-- 查看创建时间超过slow_launch_time秒的线程数。
show status like 'slow_launch_threads';

-- 查看查询时间超过long_query_time秒的查询的个数。
show status like 'slow_queries';


-- QPS(每秒Query量)
-- QPS = Questions(or Queries) / seconds
 show global  status like 'Question%';

-- TPS(每秒事务量)
-- TPS = (Com_commit + Com_rollback) / seconds

 show global status like 'Com_commit';
 show global status like 'Com_rollback';

-- key Buffer 命中率
show global  status like   'key%';

key_buffer_read_hits = (1-key_reads / key_read_requests) * 100%
key_buffer_write_hits = (1-key_writes / key_write_requests) * 100%


-- InnoDB Buffer命中率
 show status like 'innodb_buffer_pool_read%';

innodb_buffer_read_hits = (1 - innodb_buffer_pool_reads / innodb_buffer_pool_read_requests) * 100%


-- Query Cache命中率
 show status like 'Qcache%';

Query_cache_hits = (Qcahce_hits / (Qcache_hits + Qcache_inserts )) * 100%;


-- Table Cache状态量
 show global  status like 'open%';
比较 open_tables  与 opend_tables 值

-- Thread Cache 命中率
show global status like 'Thread%';
show global status like 'Connections';

Thread_cache_hits = (1 - Threads_created / connections ) * 100%
 

-- 锁定状态
mysql> show global  status like '%lock%';

Table_locks_waited/Table_locks_immediate=0.3% 如果这个比值比较大的话,说明表锁造成的阻塞比较严重
Innodb_row_lock_waits innodb行锁,太大可能是间隙锁造成的


-- Tmp Table 状况(临时表状况)
show status like 'Create_tmp%';

Created_tmp_disk_tables/Created_tmp_tables比值最好不要超过10%,如果Created_tmp_tables值比较大,
可能是排序句子过多或者是连接句子不够优化


-- Binlog Cache 使用状况
 show status like 'Binlog_cache%';

如果Binlog_cache_disk_use值不为0 ,可能需要调大 binlog_cache_size大小


-- Innodb_log_waits 量
 show status like 'innodb_log_waits';

Innodb_log_waits值不等于0的话,表明 innodb log buffer 因为空间不足而等待

查看表

-- 显示所有表
SHOW TABLES;

-- 查看表结构
SHOW CREATE TABLE 表名;
DESC 表名;

查看索引

SHOW INDEX FROM 表名;

1.Table 表的名称。
2.Non_unique 如果索引不能包括重复词,则为0。如果可以,则为1。
3.Key_name 索引的名称。
4.Seq_in_index 索引中的列序列号,从1开始。
5.Column_name 列名称。
6.Collation 列以什么方式存储在索引中。在MySQL中,有值‘A’(升序)或NULL(无分类)。
7.Cardinality 索引中唯一值的数目的估计值。通过运行ANALYZE TABLE或myisamchk -a可以更新。基数根据被存储为整数的统计数据来计数,所以即使对于小型表,该值也没有必要是精确的。基数越大,当进行联合时,MySQL使用该索引的机会就越大。
8.Sub_part 如果列只是被部分地编入索引,则为被编入索引的字符的数目。如果整列被编入索引,则为NULL。
9.Packed 指示关键字如何被压缩。如果没有被压缩,则为NULL。
10.Null 如果列含有NULL,则含有YES。如果没有,则该列含有NO。
11.Index_type 用过的索引方法(BTREE, FULLTEXT, HASH, RTREE)。
12.Comment 多种评注。