mysql基本优化

文件打开数sql

show status like "%Open_files%"缓存

Open_files 133服务器

show VARIABLES like "%open_files_limit%"并发

open_files_limit5000函数

比较合适的设置:Open_files / open_files_limit * 100% <= 75%
正常高并发

 

数据表大数据

打开数open_tables线程

show status like "%Open_tables%"排序

Open_tables1775索引

show VARIABLES like "%open%"

table_open_cache2000

Open_tables 表示打开表的数量,Opened_tables表示打开过的表数量,若是Opened_tables数量过大,说明配置中 table_cache(5.1.3以后这个值叫作table_open_cache)值可能过小,咱们查询一下服务器table_cache值 

Open_tables / Opened_tables * 100% =? 理想值 (>= 85%) Open_tables / table_open_cache* 100% = 88.75% 理想值 (<= 95%)

 

临时表tmp_table

show status like "%created%"

Created_tmp_tables16

Created_tmp_disk_tables0

Created_tmp_files1108

 

每次建立临时表,Created_tmp_tables增长,若是是在磁盘上建立临时表,Created_tmp_disk_tables也增长,Created_tmp_files表示MySQL服务建立的临时文件文件数: Created_tmp_disk_tables / Created_tmp_tables * 100% = 0(理想值<= 25%)

show variables where Variable_name in ('tmp_table_size', 'max_heap_table_size');

 

表锁状况

show status like "%table_locks%"

Table_locks_immediate53849179

Table_locks_waited0

Table_locks_immediate 表示当即释放表锁数,Table_locks_waited表示须要等待的表锁数,若是 Table_locks_immediate / Table_locks_waited > 5000,最好采用InnoDB引擎,由于InnoDB是行锁而MyISAM是表锁,对于高并发写入的应用InnoDB效果会好些.

 

表扫描状况

show status like "%handler_read_rnd%"

Handler_read_rnd_next3490

show status like "%com_select%"

Com_select16

 计算表扫描率:
表扫描率 = Handler_read_rnd_next / Com_select
若是表扫描率超过4000,说明进行了太多表扫描,颇有可能索引没有建好,增长read_buffer_size值会有一些好处,但最好不要超过8MB。 

key_buffer_size

show status like "%KEY%"

Key_reads1124

Key_read_requests2310665800

Key_blocks_unused6673

Key_blocks_used2791

show VARIABLES like "%key_buffer_size%"

key_buffer_size8388608

 

一共有2310665800个索引读取请求,有1124个请求在内存中没有找到直接从硬盘读取索引,计算索引未命中缓存的几率: key_cache_miss_rate = Key_reads / Key_read_requests * 100% =?  是否须要适当加大key_buffer_size

Key_blocks_unused表示未使用的缓存簇(blocks)数,Key_blocks_used表示曾经用到的最大的blocks数 Key_blocks_used / (Key_blocks_unused + Key_blocks_used) * 100% ≈ 29.4% (理想值 ≈ 80%)

 

排序使用状况sort_buffer

show status like "%sort_merge_passes%"

Sort_merge_passes0

 

show VARIABLES like "%sort_buffer%"

sort_buffer_size2097152

Sort_merge_passes 包括两步。MySQL 首先会尝试在内存中作排序,使用的内存大小由系统变量 Sort_buffer_size 决定,若是它的大小不够把全部的记录都读到内存中,MySQL 就会把每次在内存中排序的结果存到临时文件中,等 MySQL 找到全部记录以后,再把临时文件中的记录作一次排序。这再次排序就会增长 Sort_merge_passes。实际上,MySQL 会用另外一个临时文件来存再次排序的结果,因此一般会看到 Sort_merge_passes 增长的数值是建临时文件数的两倍。由于用到了临时文件,因此速度可能会比较慢,增长 Sort_buffer_size 会减小 Sort_merge_passes 和 建立临时文件的次数。但盲目的增长 Sort_buffer_size 并不必定能提升速度 

 

 

查询缓存

show status like "%qcache%"

Qcache_free_blocks6

Qcache_free_memory594864

Qcache_hits956995

Qcache_inserts1930901

Qcache_lowmem_prunes990019

Qcache_not_cached9188815

Qcache_queries_in_cache85

Qcache_total_blocks206

Qcache_free_blocks:缓存中相邻内存块的个数。数目大说明可能有碎片。FLUSH QUERY CACHE会对缓存中的碎片进行整理,从而获得一个空闲块。 Qcache_free_memory:缓存中的空闲内存。 Qcache_hits:每次查询在缓存中命中时就增大 Qcache_inserts:每次插入一个查询时就增大。命中次数除以插入次数就是不中比率。 Qcache_lowmem_prunes:缓存出现内存不足而且必需要进行清理以便为更多查询提供空间的次数。这个数字最好长时间来看;若是这个数字在不断增加,就表示可能碎片很是严重,或者内存不多。(上面的 free_blocks和free_memory能够告诉您属于哪一种状况) Qcache_not_cached:不适合进行缓存的查询的数量,一般是因为这些查询不是 SELECT 语句或者用了now()之类的函数。 Qcache_queries_in_cache:当前缓存的查询(和响应)的数量。 Qcache_total_blocks:缓存中块的数量。 

 show variables like 'query_cache%';

 query_cache_limit 1048576

query_cache_min_res_unit 4096
query_cache_size 1048576
query_cache_type ON
query_cache_wlock_invalidate OFF

各字段的解释:
query_cache_limit:超过此大小的查询将不缓存 query_cache_min_res_unit:缓存块的最小大小 query_cache_size:查询缓存大小 query_cache_type:缓存类型,决定缓存什么样的查询,示例中表示不缓存 select sql_no_cache 查询 query_cache_wlock_invalidate:当有其余客户端正在对MyISAM表进行写操做时,若是查询在query cache中,是否返回cache结果仍是等写操做完成再读表获取结果。
query_cache_min_res_unit的配置是一柄”双刃剑”,默认是4KB,设置值大对大数据查询有好处,但若是你的查询都是小数据查询,就容易形成内存碎片和浪费。
查询缓存碎片率 = Qcache_free_blocks / Qcache_total_blocks * 100%
若是查询缓存碎片率超过20%,能够用FLUSH QUERY CACHE整理缓存碎片,或者试试减少query_cache_min_res_unit,若是你的查询都是小数据量的话。
查询缓存利用率 = (query_cache_size – Qcache_free_memory) / query_cache_size * 100%
查询缓存利用率在25%如下的话说明query_cache_size设置的过大,可适当减少;查询缓存利用率在80%以上并且Qcache_lowmem_prunes > 50的话说明query_cache_size可能有点小,要不就是碎片太多。
查询缓存命中率 = (Qcache_hits – Qcache_inserts) / Qcache_hits * 100%
示例服务器 查询缓存碎片率 = 20.46%,查询缓存利用率 = 62.26%,查询缓存命中率 = 1.94%,命中率不好,可能写操做比较频繁吧,并且可能有些碎片

 

链接connections

show  status like '%max_used_connections%';

Max_used_connections66

 

show variables like '%max_connections%';

max_connections151

 

max_used_connections / max_connections * 100% = 99.6% (理想值 ≈ 85%)

 

线程thread

show  status like '%thread%';

Threads_cached5

Threads_created1174

 

show variables like '%thread_cache%';

thread_cache_size9

thread_cache_size,当客户端断开以后,服务器处理此客户的线程将会缓存起来以响应下一个客户而不是销毁(前提是缓存数未达上限)。Threads_created表示建立过的线程数,若是发现Threads_created值过大的话,代表 MySQL服务器一直在建立线程,这也是比较耗资源,能够适当增长配置文件中thread_cache_size值

相关文章
相关标签/搜索