您好,欢迎访问一九零五行业门户网

经过show variables like xxx 详解mysql运行时参数

通过show variables like xxx 详解mysql运行时参数 本文参考以下网页: 1.http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.htm 2.http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html 3.http://www.ibm.com/developerwork
通过show variables like xxx 详解mysql运行时参数
本文参考以下网页:
1.http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.htm
2.http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html
3.http://www.ibm.com/developerworks/cn/linux/l-tune-lamp-3.html
4.http://www.day32.com/mysql/tuning-primer.sh 具体数值主要参考此工具
?
1, 查看mysql服务器配置信息?
java代码mysql>?show?variables;
2, 查看mysql服务器运行的各种状态值?
java代码mysql>?show?global?status;
3, 慢查询?
java代码mysql>?show?variables?like?'%slow%';+------------------+-------+|?variable_name|?value?|+------------------+-------+|?log_slow_queries?|?off?||?slow_launch_time?|?2?|+------------------+-------+mysql>?show?global?status?like?'%slow%';+---------------------+-------+|?variable_name?|?value?|+---------------------+-------+|?slow_launch_threads?|?0?||?slow_queries|?279?|+---------------------+-------+
配置中关闭了记录慢查询(最好是打开,方便优化),超过2秒即为慢查询,一共有279条慢查询?
4, 连接数?
java代码mysql>?show?variables?like?'max_connections';+-----------------+-------+|?variable_name?|?value?|+-----------------+-------+|?max_connections?|?500?|+-----------------+-------+mysql>?show?global?status?like?'max_used_connections';+----------------------+-------+|?variable_name|?value?|+----------------------+-------+|?max_used_connections?|?498?|+----------------------+-------+
设置的最大连接数是500,而响应的连接数是498?
max_used_connections / max_connections * 100% = 99.6% (理想值 ≈ 85%)?
5, key_buffer_size?
key_buffer_size是对myisam表性能影响最大的一个参数, 不过数据库中多为innodb?
java代码mysql>?show?variables?like?'key_buffer_size';+-----------------+----------+|?variable_name?|?value|+-----------------+----------+|?key_buffer_size?|?67108864?|+-----------------+----------+mysql>?show?global?status?like?'key_read%';+-------------------+----------+|?variable_name?|?value|+-------------------+----------+|?key_read_requests?|?25629497?||?key_reads?|?66071|+-------------------+----------+
一共有25629497个索引读取请求,有66071个请求在内存中没有找到直接从硬盘读取索引,计算索引未命中缓存的概率:?
key_cache_miss_rate = key_reads / key_read_requests * 100% =0.27%?
需要适当加大key_buffer_size?
java代码mysql>?show?global?status?like?'key_blocks_u%';+-------------------+-------+|?variable_name?|?value?|+-------------------+-------+|?key_blocks_unused?|?10285?||?key_blocks_used?|?47705?|+-------------------+-------+
key_blocks_unused表示未使用的缓存簇(blocks)数,key_blocks_used表示曾经用到的最大的blocks数?
key_blocks_used / (key_blocks_unused + key_blocks_used) * 100% ≈ 18% (理想值 ≈ 80%)?
6, 临时表?
java代码mysql>?show?global?status?like?'created_tmp%';+-------------------------+---------+|?variable_name?|?value?|+-------------------------+---------+|?created_tmp_disk_tables?|?4184337?||?created_tmp_files?|?4124||?created_tmp_tables|?4215028?|+-------------------------+---------+
每次创建临时表,created_tmp_tables增加,如果是在磁盘上创建临时表,created_tmp_disk_tables也增加,created_tmp_files表示mysql服务创建的临时文件文件数:?
created_tmp_disk_tables / created_tmp_tables * 100% = 99% (理想值
java代码mysql>?show?variables?where?variable_name?in?('tmp_table_size',?'max_heap_table_size');+---------------------+-----------+|?variable_name?|?value?|+---------------------+-----------+|?max_heap_table_size?|?134217728?||?tmp_table_size|?134217728?|+---------------------+-----------+
需要增加tmp_table_size?
7,open table 的情况?
java代码mysql>?show?global?status?like?'open%tables%';+---------------+-------+|?variable_name?|?value?|+---------------+-------+|?open_tables?|?1024||?opened_tables?|?1465|+---------------+-------+
open_tables 表示打开表的数量,opened_tables表示打开过的表数量,如果opened_tables数量过大,说明配置中 table_cache(5.1.3之后这个值叫做table_open_cache)值可能太小,我们查询一下服务器table_cache值?
java代码mysql>?show?variables?like?'table_cache';+---------------+-------+|?variable_name?|?value?|+---------------+-------+|?table_cache?|?1024|+---------------+-------+
open_tables / opened_tables * 100% =69% 理想值 (>= 85%)?
open_tables / table_cache * 100% = 100% 理想值 (
8, 进程使用情况?
java代码mysql>?show?global?status?like?'thread%';+-------------------+-------+|?variable_name?|?value?|+-------------------+-------+|?threads_cached|?31||?threads_connected?|?239?||?threads_created?|?2914||?threads_running?|?4?|+-------------------+-------+
如果我们在mysql服务器配置文件中设置了thread_cache_size,当客户端断开之后,服务器处理此客户的线程将会缓存起来以响应下一个客户而不是销毁(前提是缓存数未达上限)。threads_created表示创建过的线程数,如果发现threads_created值过大的话,表明 mysql服务器一直在创建线程,这也是比较耗资源,可以适当增加配置文件中thread_cache_size值,查询服务器 thread_cache_size配置:?
java代码mysql>?show?variables?like?'thread_cache_size';+-------------------+-------+|?variable_name?|?value?|+-------------------+-------+|?thread_cache_size?|?32|+-------------------+-------+
9, 查询缓存(query cache)?
java代码mysql>?show?global?status?like?'qcache%';+-------------------------+----------+|?variable_name?|?value|+-------------------------+----------+|?qcache_free_blocks|?2226?||?qcache_free_memory|?10794944?||?qcache_hits?|?5385458||?qcache_inserts|?1806301||?qcache_lowmem_prunes|?433101?||?qcache_not_cached?|?4429464||?qcache_queries_in_cache?|?7168?||?qcache_total_blocks?|?16820|+-------------------------+----------+
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:缓存中块的数量。?
我们再查询一下服务器关于query_cache的配置:?
java代码mysql>?show?variables?like?'query_cache%';+------------------------------+----------+|?variable_name|?value|+------------------------------+----------+|?query_cache_limit|?33554432?||?query_cache_min_res_unit?|?4096?||?query_cache_size?|?33554432?||?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%,命中率很差,可能写操作比较频繁吧,而且可能有些碎片。?
10,排序使用情况?
java代码mysql>?show?global?status?like?'sort%';+-------------------+----------+|?variable_name?|?value|+-------------------+----------+|?sort_merge_passes?|?2136?||?sort_range|?81888||?sort_rows?|?35918141?||?sort_scan?|?55269|+-------------------+----------+
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 并不一定能提高速度,见 how fast can you sort data with mysql?(引自http://qroom.blogspot.com/2007/09/mysql-select-sort.html)?
另外,增加read_rnd_buffer_size(3.2.3是record_rnd_buffer_size)的值对排序的操作也有一点的好处,参见:http://www.mysqlperformanceblog.com/2007/07/24/what-exactly-is- read_rnd_buffer_size/?
11.文件打开数(open_files)?
java代码mysql>?show?global?status?like?'open_files';+---------------+-------+|?variable_name?|?value?|+---------------+-------+|?open_files|?821?|+---------------+-------+mysql>?show?variables?like?'open_files_limit';+------------------+-------+|?variable_name|?value?|+------------------+-------+|?open_files_limit?|?65535?|+------------------+-------+
比较合适的设置:open_files / open_files_limit * 100%
正常?
12。 表锁情况?
java代码mysql>?show?global?status?like?'table_locks%';+-----------------------+---------+|?variable_name?|?value?|+-----------------------+---------+|?table_locks_immediate?|?4257944?||?table_locks_waited|?25182?|+-----------------------+---------+
table_locks_immediate 表示立即释放表锁数,table_locks_waited表示需要等待的表锁数,如果 table_locks_immediate / table_locks_waited > 5000,最好采用innodb引擎,因为innodb是行锁而myisam是表锁,对于高并发写入的应用innodb效果会好些.?
13. 表扫描情况?
java代码mysql>?show?global?status?like?'handler_read%';+-----------------------+-----------+|?variable_name?|?value?|+-----------------------+-----------+|?handler_read_first|?108763||?handler_read_key|?92813521||?handler_read_next?|?486650793?||?handler_read_prev?|?688726||?handler_read_rnd|?9321362?||?handler_read_rnd_next?|?153086384?|+-----------------------+-----------+
各字段解释参见http://hi.baidu.com/thinkinginlamp/blog/item/31690cd7c4bc5cdaa144df9c.html,调出服务器完成的查询请求次数:?
java代码mysql>?show?global?status?like?'com_select';+---------------+---------+|?variable_name?|?value?|+---------------+---------+|?com_select|?2693147?|+---------------+---------+
计算表扫描率:?
表扫描率 = handler_read_rnd_next / com_select?
如果表扫描率超过4000,说明进行了太多表扫描,很有可能索引没有建好,增加read_buffer_size值会有一些好处,但最好不要超过8mb。
----------------------------------------------
其它类似信息

推荐信息