监控mysql的存储引擎

监控mysql

显示进程状态变量

mysql> show variables like '%thread%';
+----------------------------+---------------------------+
| Variable_name              | Value                     |
+----------------------------+---------------------------+
| innodb_file_io_threads     | 4                         |
| innodb_thread_concurrency  | 8                         |
| innodb_thread_sleep_delay  | 10000                     |
| max_delayed_threads        | 20                        |
| max_insert_delayed_threads | 20                        |
| myisam_repair_threads      | 1                         |
| pseudo_thread_id           | 2                         |
| thread_cache_size          | 0                         |
| thread_handling            | one-thread-per-connection |
| thread_stack               | 262144                    |
+----------------------------+---------------------------+

query cache

如果你使用的是myisam存储引擎的,query cache是一个最终的性能,他允许服务器在内存中缓存频繁使用的查询语句和查询结果,因此一个查询运行的越频繁,这个查询结果就越可能从缓存中得到,显然,从内存中读取数据比从硬盘上读取数据要快的多
通过have_query_cache变量查询query cache是否可用;

mysql> show variables like '%query_cache%';
+------------------------------+---------+
| Variable_name                | Value   |
+------------------------------+---------+
| have_query_cache             | YES     |
| query_cache_limit            | 1048576 |
| query_cache_min_res_unit     | 4096    |
| query_cache_size             | 0       |
| query_cache_type             | ON      |
| query_cache_wlock_invalidate | OFF     |
+------------------------------+---------+

have_query_cache 变量仅仅表示这个特性可用,query_cache_size 这个变量值为0,它将迅速关闭query cache ;
Query cache状态变量

mysql> show status like '%Qcache%';
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| Qcache_free_blocks      | 0     |
| Qcache_free_memory      | 0     |
| Qcache_hits             | 0     |
| Qcache_inserts          | 0     |
| Qcache_lowmem_prunes    | 0     |
| Qcache_not_cached       | 0     |
| Qcache_queries_in_cache | 0     |
| Qcache_total_blocks     | 0     |
+-------------------------+-------+
8 rows in set (0.00 sec)

可以定期使用flush query cache 这个命令重整 querycache
,这样不会删除内存中的结果,但是允许重新组织内存以更好地3使用内存;

mysql> flush query cache;
Query OK, 0 rows affected (0.00 sec)

mysql性能

使用explain

mysql> explain select * from student where id>'901'\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: student
         type: ALL
possible_keys: PRIMARY
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 5
        Extra: Using where
1 row in set (0.00 sec)

看到该查询已经检测到了索引,但key为NULL,没有使用索引。
删除范围查询看看结果:

mysql> explain select * from student where id='901'\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: student
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: const
         rows: 1
        Extra: 
1 row in set (0.00 sec)

使用analyze table

可以为myisam和Innodb表更新主健分布,但不是使用于所有的引擎。
分析表并更新主健分布

mysql> analyze table student;
+----------------+---------+----------+----------+
| Table          | Op      | Msg_type | Msg_text |
+----------------+---------+----------+----------+
| school.student | analyze | status   | OK       |
+----------------+---------+----------+----------+
1 row in set (0.00 sec)

通过show index查看索引的状态:

mysql> show index from student\G;
*************************** 1. row ***************************
       Table: student
  Non_unique: 0
    Key_name: PRIMARY
Seq_in_index: 1
 Column_name: id
   Collation: A
 Cardinality: 5
    Sub_part: NULL
      Packed: NULL
        Null: 
  Index_type: BTREE
     Comment: 
1 row in set (0.00 sec)

无论表何时发生重大的更新(例如批量加载数据),都应该执行这个命令,在这个操作过程中,系统必须要先给这个表设定一个锁。

使用optimize table

被频繁更新的表将很快变的支离破碎,并取决于存储引擎的不同,将会出现不同程度 的闲置空间和不理想的存储结构;
使用optimize table命令可以重构一个或多个表的数据结构;

mysql> optimize table student\G
*************************** 1. row ***************************
   Table: school.student
      Op: optimize
Msg_type: status
Msg_text: OK
1 row in set (0.00 sec)

无论表何时发生重大更新(例如大量删除和插入),都应该执行这个命令,这个操作用于优化数据元素排列结构,而且它比预期运行的时间长,这个操作适合在低负载时运行;

posted @ 2016-07-21 11:53  夏日花开  阅读(173)  评论(0编辑  收藏  举报