欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

通过show status 来优化 MySQL数据库

通过show status 来优化MySQL数据库

 

http://lxneng.iteye.com/blog/451985

 

[sql] view plain copy
 
  1. 1, 查看MySQL服务器配置信息   
  2. Java代码    
  3. mysql> show variables;    
  4.   
  5. mysql> show variables;  
  6. 2, 查看MySQL服务器运行的各种状态值   
  7. Java代码    
  8. mysql> show global status;    
  9.   
  10. mysql> show global status;  
  11. 3, 慢查询   
  12. Java代码    
  13. mysql> show variables like '%slow%';     
  14. +------------------+-------+     
  15. | Variable_name    | Value |     
  16. +------------------+-------+     
  17. | log_slow_queries | OFF   |     
  18. | slow_launch_time | 2     |     
  19. +------------------+-------+     
  20. mysql> show global status like '%slow%';     
  21. +---------------------+-------+     
  22. | Variable_name       | Value |     
  23. +---------------------+-------+     
  24. | Slow_launch_threads | 0     |     
  25. | Slow_queries        | 279   |     
  26. +---------------------+-------+    
  27.   
  28. mysql> show variables like '%slow%';  
  29. +------------------+-------+  
  30. | Variable_name    | Value |  
  31. +------------------+-------+  
  32. | log_slow_queries | OFF   |  
  33. | slow_launch_time | 2     |  
  34. +------------------+-------+  
  35. mysql> show global status like '%slow%';  
  36. +---------------------+-------+  
  37. | Variable_name       | Value |  
  38. +---------------------+-------+  
  39. | Slow_launch_threads | 0     |  
  40. | Slow_queries        | 279   |  
  41. +---------------------+-------+  
  42. 配置中关闭了记录慢查询(最好是打开,方便优化),超过2秒即为慢查询,一共有279条慢查询   
  43.   
  44. 4, 连接数   
  45.   
  46. Java代码    
  47. mysql> show variables like 'max_connections';     
  48. +-----------------+-------+     
  49. | Variable_name   | Value |     
  50. +-----------------+-------+     
  51. | max_connections | 500   |     
  52. +-----------------+-------+     
  53.     
  54. mysql> show global status like 'max_used_connections';     
  55. +----------------------+-------+     
  56. | Variable_name        | Value |     
  57. +----------------------+-------+     
  58. | Max_used_connections | 498   |     
  59. +----------------------+-------+    
  60.   
  61. mysql> show variables like 'max_connections';  
  62. +-----------------+-------+  
  63. | Variable_name   | Value |  
  64. +-----------------+-------+  
  65. | max_connections | 500   |  
  66. +-----------------+-------+  
  67.   
  68. mysql> show global status like 'max_used_connections';  
  69. +----------------------+-------+  
  70. | Variable_name        | Value |  
  71. +----------------------+-------+  
  72. | Max_used_connections | 498   |  
  73. +----------------------+-------+  
  74.   
  75. 设置的最大连接数是500,而响应的连接数是498   
  76.   
  77. max_used_connections / max_connections * 100% = 99.6% (理想值 ≈ 85%)   
  78.   
  79. 5, key_buffer_size   
  80. key_buffer_size是对MyISAM表性能影响最大的一个参数, 不过数据库中多为Innodb   
  81.   
  82. Java代码    
  83. mysql> show variables like 'key_buffer_size';     
  84. +-----------------+----------+     
  85. | Variable_name   | Value    |     
  86. +-----------------+----------+     
  87. | key_buffer_size | 67108864 |     
  88. +-----------------+----------+     
  89.     
  90. mysql> show global status like 'key_read%';     
  91. +-------------------+----------+     
  92. | Variable_name     | Value    |     
  93. +-------------------+----------+     
  94. | Key_read_requests | 25629497 |     
  95. | Key_reads         | 66071    |     
  96. +-------------------+----------+    
  97.   
  98. mysql> show variables like 'key_buffer_size';  
  99. +-----------------+----------+  
  100. | Variable_name   | Value    |  
  101. +-----------------+----------+  
  102. | key_buffer_size | 67108864 |  
  103. +-----------------+----------+  
  104.   
  105. mysql> show global status like 'key_read%';  
  106. +-------------------+----------+  
  107. | Variable_name     | Value    |  
  108. +-------------------+----------+  
  109. | Key_read_requests | 25629497 |  
  110. | Key_reads         | 66071    |  
  111. +-------------------+----------+  
  112.   
  113. 一共有25629497个索引读取请求,有66071个请求在内存中没有找到直接从硬盘读取索引,计算索引未命中缓存的概率:   
  114. key_cache_miss_rate = Key_reads / Key_read_requests * 100% =0.27%   
  115. 需要适当加大key_buffer_size   
  116.   
  117. Java代码    
  118. mysql> show global status like 'key_blocks_u%';     
  119. +-------------------+-------+     
  120. | Variable_name     | Value |     
  121. +-------------------+-------+     
  122. | Key_blocks_unused | 10285 |     
  123. | Key_blocks_used   | 47705 |     
  124. +-------------------+-------+    
  125.   
  126. mysql> show global status like 'key_blocks_u%';  
  127. +-------------------+-------+  
  128. | Variable_name     | Value |  
  129. +-------------------+-------+  
  130. | Key_blocks_unused | 10285 |  
  131. | Key_blocks_used   | 47705 |  
  132. +-------------------+-------+  
  133. Key_blocks_unused表示未使用的缓存簇(blocks)数,Key_blocks_used表示曾经用到的最大的blocks数   
  134. Key_blocks_used / (Key_blocks_unused + Key_blocks_used) * 100% ≈ 18% (理想值 ≈ 80%)   
  135.   
  136. 6, 临时表   
  137.   
  138. Java代码    
  139. mysql> show global status like 'created_tmp%';     
  140. +-------------------------+---------+     
  141. | Variable_name           | Value   |     
  142. +-------------------------+---------+     
  143. | Created_tmp_disk_tables | 4184337 |     
  144. | Created_tmp_files       | 4124    |     
  145. | Created_tmp_tables      | 4215028 |     
  146. +-------------------------+---------+    
  147.   
  148. mysql> show global status like 'created_tmp%';  
  149. +-------------------------+---------+  
  150. | Variable_name           | Value   |  
  151. +-------------------------+---------+  
  152. | Created_tmp_disk_tables | 4184337 |  
  153. | Created_tmp_files       | 4124    |  
  154. | Created_tmp_tables      | 4215028 |  
  155. +-------------------------+---------+  
  156. 每次创建临时表,Created_tmp_tables增加,如果是在磁盘上创建临时表,Created_tmp_disk_tables也增加,Created_tmp_files表示MySQL服务创建的临时文件文件数:   
  157. Created_tmp_disk_tables / Created_tmp_tables * 100% = 99% (理想值<= 25%)   
  158.   
  159. Java代码    
  160. mysql> show variables where Variable_name in ('tmp_table_size''max_heap_table_size');     
  161. +---------------------+-----------+     
  162. | Variable_name       | Value     |     
  163. +---------------------+-----------+     
  164. | max_heap_table_size | 134217728 |     
  165. | tmp_table_size      | 134217728 |     
  166. +---------------------+-----------+    
  167.   
  168. mysql> show variables where Variable_name in ('tmp_table_size''max_heap_table_size');  
  169. +---------------------+-----------+  
  170. | Variable_name       | Value     |  
  171. +---------------------+-----------+  
  172. | max_heap_table_size | 134217728 |  
  173. | tmp_table_size      | 134217728 |  
  174. +---------------------+-----------+  
  175. 需要增加tmp_table_size   
  176.   
  177. 7,open table 的情况   
  178. Java代码    
  179. mysql> show global status like 'open%tables%';     
  180. +---------------+-------+     
  181. | Variable_name | Value |     
  182. +---------------+-------+     
  183. | Open_tables   | 1024  |     
  184. | Opened_tables | 1465  |     
  185. +---------------+-------+    
  186.   
  187. mysql> show global status like 'open%tables%';  
  188. +---------------+-------+  
  189. | Variable_name | Value |  
  190. +---------------+-------+  
  191. | Open_tables   | 1024  |  
  192. | Opened_tables | 1465  |  
  193. +---------------+-------+  
  194. Open_tables 表示打开表的数量,Opened_tables表示打开过的表数量,如果Opened_tables数量过大,说明配置中 table_cache(5.1.3之后这个值叫做table_open_cache)值可能太小,我们查询一下服务器table_cache值   
  195. Java代码    
  196. mysql> show variables like 'table_cache';     
  197. +---------------+-------+     
  198. | Variable_name | Value |     
  199. +---------------+-------+     
  200. | table_cache   | 1024  |     
  201. +---------------+-------+    
  202.   
  203. mysql> show variables like 'table_cache';  
  204. +---------------+-------+  
  205. | Variable_name | Value |  
  206. +---------------+-------+  
  207. | table_cache   | 1024  |  
  208. +---------------+-------+  
  209.   
  210. Open_tables / Opened_tables * 100% =69% 理想值 (>= 85%)   
  211. Open_tables / table_cache * 100% = 100% 理想值 (<= 95%)   
  212.   
  213. 8, 进程使用情况   
  214. Java代码    
  215. mysql> show global status like 'Thread%';     
  216. +-------------------+-------+     
  217. | Variable_name     | Value |     
  218. +-------------------+-------+     
  219. | Threads_cached    | 31    |     
  220. | Threads_connected | 239   |     
  221. | Threads_created   | 2914  |     
  222. | Threads_running   | 4     |     
  223. +-------------------+-------+    
  224.   
  225. mysql> show global status like 'Thread%';  
  226. +-------------------+-------+  
  227. | Variable_name     | Value |  
  228. +-------------------+-------+  
  229. | Threads_cached    | 31    |  
  230. | Threads_connected | 239   |  
  231. | Threads_created   | 2914  |  
  232. | Threads_running   | 4     |  
  233. +-------------------+-------+  
  234. 如果我们在MySQL服务器配置文件中设置了thread_cache_size,当客户端断开之后,服务器处理此客户的线程将会缓存起来以响应下一个客户而不是销毁(前提是缓存数未达上限)。Threads_created表示创建过的线程数,如果发现Threads_created值过大的话,表明 MySQL服务器一直在创建线程,这也是比较耗资源,可以适当增加配置文件中thread_cache_size值,查询服务器 thread_cache_size配置:   
  235. Java代码    
  236. mysql> show variables like 'thread_cache_size';     
  237. +-------------------+-------+     
  238. | Variable_name     | Value |     
  239. +-------------------+-------+     
  240. | thread_cache_size | 32    |     
  241. +-------------------+-------+    
  242.   
  243. mysql> show variables like 'thread_cache_size';  
  244. +-------------------+-------+  
  245. | Variable_name     | Value |  
  246. +-------------------+-------+  
  247. | thread_cache_size | 32    |  
  248. +-------------------+-------+  
  249.   
  250. 9, 查询缓存(query cache)   
  251. Java代码    
  252. mysql> show global status like 'qcache%';     
  253. +-------------------------+----------+     
  254. | Variable_name           | Value    |     
  255. +-------------------------+----------+     
  256. | Qcache_free_blocks      | 2226     |     
  257. | Qcache_free_memory      | 10794944 |     
  258. | Qcache_hits             | 5385458  |     
  259. | Qcache_inserts          | 1806301  |     
  260. | Qcache_lowmem_prunes    | 433101   |     
  261. | Qcache_not_cached       | 4429464  |     
  262. | Qcache_queries_in_cache | 7168     |     
  263. | Qcache_total_blocks     | 16820    |     
  264. +-------------------------+----------+    
  265.   
  266. mysql> show global status like 'qcache%';  
  267. +-------------------------+----------+  
  268. | Variable_name           | Value    |  
  269. +-------------------------+----------+  
  270. | Qcache_free_blocks      | 2226     |  
  271. | Qcache_free_memory      | 10794944 |  
  272. | Qcache_hits             | 5385458  |  
  273. | Qcache_inserts          | 1806301  |  
  274. | Qcache_lowmem_prunes    | 433101   |  
  275. | Qcache_not_cached       | 4429464  |  
  276. | Qcache_queries_in_cache | 7168     |  
  277. | Qcache_total_blocks     | 16820    |  
  278. +-------------------------+----------+  
  279. Qcache_free_blocks:缓存中相邻内存块的个数。数目大说明可能有碎片。FLUSH QUERY CACHE会对缓存中的碎片进行整理,从而得到一个空闲块。   
  280. Qcache_free_memory:缓存中的空闲内存。   
  281. Qcache_hits:每次查询在缓存中命中时就增大   
  282. Qcache_inserts:每次插入一个查询时就增大。命中次数除以插入次数就是不中比率。   
  283. Qcache_lowmem_prunes:缓存出现内存不足并且必须要进行清理以便为更多查询提供空间的次数。这个数字最好长时间来看;如果这个数字在不断增长,就表示可能碎片非常严重,或者内存很少。(上面的          free_blocks和free_memory可以告诉您属于哪种情况)   
  284. Qcache_not_cached:不适合进行缓存的查询的数量,通常是由于这些查询不是 SELECT 语句或者用了now()之类的函数。   
  285. Qcache_queries_in_cache:当前缓存的查询(和响应)的数量。   
  286. Qcache_total_blocks:缓存中块的数量。   
  287.   
  288. 我们再查询一下服务器关于query_cache的配置:   
  289. Java代码    
  290. mysql> show variables like 'query_cache%';     
  291. +------------------------------+----------+     
  292. | Variable_name                | Value    |     
  293. +------------------------------+----------+     
  294. | query_cache_limit            | 33554432 |     
  295. | query_cache_min_res_unit     | 4096     |     
  296. | query_cache_size             | 33554432 |     
  297. | query_cache_type             | ON       |     
  298. | query_cache_wlock_invalidate | OFF      |     
  299. +------------------------------+----------+    
  300.   
  301. mysql> show variables like 'query_cache%';  
  302. +------------------------------+----------+  
  303. | Variable_name                | Value    |  
  304. +------------------------------+----------+  
  305. | query_cache_limit            | 33554432 |  
  306. | query_cache_min_res_unit     | 4096     |  
  307. | query_cache_size             | 33554432 |  
  308. | query_cache_type             | ON       |  
  309. | query_cache_wlock_invalidate | OFF      |  
  310. +------------------------------+----------+  
  311. 各字段的解释:   
  312.   
  313. query_cache_limit:超过此大小的查询将不缓存   
  314. query_cache_min_res_unit:缓存块的最小大小   
  315. query_cache_size:查询缓存大小   
  316. query_cache_type:缓存类型,决定缓存什么样的查询,示例中表示不缓存 select sql_no_cache 查询   
  317. query_cache_wlock_invalidate:当有其他客户端正在对MyISAM表进行写操作时,如果查询在query cache中,是否返回cache结果还是等写操作完成再读表获取结果。   
  318.   
  319. query_cache_min_res_unit的配置是一柄”双刃剑”,默认是4KB,设置值大对大数据查询有好处,但如果你的查询都是小数据查询,就容易造成内存碎片和浪费。   
  320.   
  321. 查询缓存碎片率 = Qcache_free_blocks / Qcache_total_blocks * 100%   
  322.   
  323. 如果查询缓存碎片率超过20%,可以用FLUSH QUERY CACHE整理缓存碎片,或者试试减小query_cache_min_res_unit,如果你的查询都是小数据量的话。   
  324.   
  325. 查询缓存利用率 = (query_cache_size – Qcache_free_memory) / query_cache_size * 100%   
  326.   
  327. 查询缓存利用率在25%以下的话说明query_cache_size设置的过大,可适当减小;查询缓存利用率在80%以上而且Qcache_lowmem_prunes > 50的话说明query_cache_size可能有点小,要不就是碎片太多。   
  328.   
  329. 查询缓存命中率 = (Qcache_hits – Qcache_inserts) / Qcache_hits * 100%   
  330.   
  331. 示例服务器 查询缓存碎片率 = 20.46%,查询缓存利用率 = 62.26%,查询缓存命中率 = 1.94%,命中率很差,可能写操作比较频繁吧,而且可能有些碎片。   
  332.   
  333. 10,排序使用情况   
  334.   
  335. Java代码    
  336. mysql> show global status like 'sort%';     
  337. +-------------------+----------+     
  338. | Variable_name     | Value    |     
  339. +-------------------+----------+     
  340. | Sort_merge_passes | 2136     |     
  341. | Sort_range        | 81888    |     
  342. | Sort_rows         | 35918141 |     
  343. | Sort_scan         | 55269    |     
  344. +-------------------+----------+    
  345.   
  346. mysql> show global status like 'sort%';  
  347. +-------------------+----------+  
  348. | Variable_name     | Value    |  
  349. +-------------------+----------+  
  350. | Sort_merge_passes | 2136     |  
  351. | Sort_range        | 81888    |  
  352. | Sort_rows         | 35918141 |  
  353. | Sort_scan         | 55269    |  
  354. +-------------------+----------+  
  355.   
  356. 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)   
  357.   
  358. 另外,增加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/   
  359.   
  360. 11.文件打开数(open_files)   
  361.   
  362. Java代码    
  363. mysql> show global status like 'open_files';     
  364. +---------------+-------+     
  365. | Variable_name | Value |     
  366. +---------------+-------+     
  367. | Open_files    | 821   |     
  368. +---------------+-------+     
  369.     
  370. mysql> show variables like 'open_files_limit';     
  371. +------------------+-------+     
  372. | Variable_name    | Value |     
  373. +------------------+-------+     
  374. | open_files_limit | 65535 |     
  375. +------------------+-------+    
  376.   
  377. mysql> show global status like 'open_files';  
  378. +---------------+-------+  
  379. | Variable_name | Value |  
  380. +---------------+-------+  
  381. | Open_files    | 821   |  
  382. +---------------+-------+  
  383.   
  384. mysql> show variables like 'open_files_limit';  
  385. +------------------+-------+  
  386. | Variable_name    | Value |  
  387. +------------------+-------+  
  388. | open_files_limit | 65535 |  
  389. +------------------+-------+  
  390.   
  391. 比较合适的设置:Open_files / open_files_limit * 100% <= 75%   
  392.   
  393. 正常   
  394.   
  395. 12。 表锁情况   
  396. Java代码    
  397. mysql> show global status like 'table_locks%';     
  398. +-----------------------+---------+     
  399. | Variable_name         | Value   |     
  400. +-----------------------+---------+     
  401. | Table_locks_immediate | 4257944 |     
  402. | Table_locks_waited    | 25182   |     
  403. +-----------------------+---------+    
  404.   
  405. mysql> show global status like 'table_locks%';  
  406. +-----------------------+---------+  
  407. | Variable_name         | Value   |  
  408. +-----------------------+---------+  
  409. | Table_locks_immediate | 4257944 |  
  410. | Table_locks_waited    | 25182   |  
  411. +-----------------------+---------+  
  412. Table_locks_immediate 表示立即释放表锁数,Table_locks_waited表示需要等待的表锁数,如果 Table_locks_immediate / Table_locks_waited > 5000,最好采用InnoDB引擎,因为InnoDB是行锁而MyISAM是表锁,对于高并发写入的应用InnoDB效果会好些.   
  413.   
  414. 13. 表扫描情况   
  415. Java代码    
  416. mysql> show global status like 'handler_read%';     
  417. +-----------------------+-----------+     
  418. | Variable_name         | Value     |     
  419. +-----------------------+-----------+     
  420. | Handler_read_first    | 108763    |     
  421. | Handler_read_key      | 92813521  |     
  422. | Handler_read_next     | 486650793 |     
  423. | Handler_read_prev     | 688726    |     
  424. | Handler_read_rnd      | 9321362   |     
  425. | Handler_read_rnd_next | 153086384 |     
  426. +-----------------------+-----------+    
  427.   
  428. mysql> show global status like 'handler_read%';  
  429. +-----------------------+-----------+  
  430. | Variable_name         | Value     |  
  431. +-----------------------+-----------+  
  432. | Handler_read_first    | 108763    |  
  433. | Handler_read_key      | 92813521  |  
  434. | Handler_read_next     | 486650793 |  
  435. | Handler_read_prev     | 688726    |  
  436. | Handler_read_rnd      | 9321362   |  
  437. | Handler_read_rnd_next | 153086384 |  
  438. +-----------------------+-----------+  
  439.   
  440. 各字段解释参见http://hi.baidu.com/thinkinginlamp/blog/item/31690cd7c4bc5cdaa144df9c.html,调出服务器完成的查询请求次数:   
  441. Java代码    
  442. mysql> show global status like 'com_select';     
  443. +---------------+---------+     
  444. | Variable_name | Value   |     
  445. +---------------+---------+     
  446. | Com_select    | 2693147 |     
  447. +---------------+---------+    
  448.   
  449. mysql> show global status like 'com_select';  
  450. +---------------+---------+  
  451. | Variable_name | Value   |  
  452. +---------------+---------+  
  453. | Com_select    | 2693147 |  
  454. +---------------+---------+  
  455.   
  456. 计算表扫描率:   
  457.   
  458. 表扫描率 = Handler_read_rnd_next / Com_select   
  459.   
  460. 如果表扫描率超过4000,说明进行了太多表扫描,很有可能索引没有建好,增加read_buffer_size值会有一些好处,但最好不要超过8MB  


 

来自 http://blog.csdn.net/kash_chen007/article/details/19757577

普通分类: