mysql show命令用法大全

zk1878發表於2011-05-09

原文:http://blog.csdn.net/zztfj/archive/2011/02/12/6181379.aspx

MySQL中有很多的基本命令,show命令也是其中之一,在很多使用者中對show命令的使用還容易產生混淆,本文彙集了show命令的眾多用法。
1. show tables或show tables from database_name; -- 顯示當前資料庫中所有表的名稱。
2. show databases; -- 顯示mysql中所有資料庫的名稱。
3. show columns from table_name from database_name; 或show columns from database_name.table_name; -- 顯示錶中列名稱。
4. show grants for user_name; -- 顯示一個使用者的許可權,顯示結果類似於grant 命令。
5. show index from table_name; -- 顯示錶的索引。
6. show status; -- 顯示一些系統特定資源的資訊,例如,正在執行的執行緒數量。
7. show variables; -- 顯示系統變數的名稱和值。
8. show processlist; -- 顯示系統中正在執行的所有程式,也就是當前正在執行的查詢。大多數使用者可以檢視他們自己的程式,但是如果他們擁有process許可權,就可以檢視所有人的程式,包括密碼。
9. show table status; -- 顯示當前使用或者指定的database中的每個表的資訊。資訊包括表型別和表的最新更新時間。
10. show privileges; -- 顯示伺服器所支援的不同許可權。
11. show create database database_name; -- 顯示create database 語句是否能夠建立指定的資料庫。
12. show create table table_name; -- 顯示create database 語句是否能夠建立指定的資料庫。
13. show engines; -- 顯示安裝以後可用的儲存引擎和預設引擎。
14. show innodb status; -- 顯示innoDB儲存引擎的狀態。
15. show logs; -- 顯示BDB儲存引擎的日誌。
16. show warnings; -- 顯示最後一個執行的語句所產生的錯誤、警告和通知。
17. show errors; -- 只顯示最後一個執行語句所產生的錯誤。
18. show [storage] engines; --顯示安裝後的可用儲存引擎和預設引擎。

示例:

1 顯示該資料庫連線使用的字符集的情況

show variables like '%char%';

2 檢視MySQL伺服器執行的各種狀態值

show global status;

3 連線數

01
mysql> show variables like 'max_connections';
02
+-----------------+-------+
03
| Variable_name   | Value |
04
+-----------------+-------+
05
| max_connections | 500   |
06
+-----------------+-------+
07

08
mysql> show global status like 'max_used_connections';
09
+----------------------+-------+
10
| Variable_name        | Value |
11
+----------------------+-------+
12
| Max_used_connections | 498   |
13
+----------------------+-------+
設定的最大連線數是500,而響應的連線數是498
max_used_connections / max_connections * 100% = 99.6% (理想值 ≈ 85%)

4 key_buffer_size
key_buffer_size是對MyISAM表效能影響最大的一個引數, 不過資料庫中多為Innodb

01
mysql> show variables like 'key_buffer_size';
02
+-----------------+----------+
03
| Variable_name   | Value    |
04
+-----------------+----------+
05
| key_buffer_size | 67108864 |
06
+-----------------+----------+
07

08
mysql> show global status like 'key_read%';
09
+-------------------+----------+
10
| Variable_name     | Value    |
11
+-------------------+----------+
12
| Key_read_requests | 25629497 |
13
| Key_reads         | 66071    |
14
+-------------------+----------+
一共有25629497個索引讀取請求,有66071個請求在記憶體中沒有找到直接從硬碟讀取索引,計算索引未命中快取的概率:
key_cache_miss_rate = Key_reads / Key_read_requests * 100% =0.27%
需要適當加大key_buffer_size

1
mysql> show global status like 'key_blocks_u%';
2
+-------------------+-------+
3
| Variable_name     | Value |
4
+-------------------+-------+
5
| Key_blocks_unused | 10285 |
6
| Key_blocks_used   | 47705 |
7
+-------------------+-------+
Key_blocks_unused表示未使用的快取簇(blocks)數,Key_blocks_used表示曾經用到的最大的blocks數
Key_blocks_used / (Key_blocks_unused + Key_blocks_used) * 100% ≈ 18% (理想值 ≈ 80%)

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

5 open table 的情況

1
mysql> show global status like 'open%tables%';
2
+---------------+-------+
3
| Variable_name | Value |
4
+---------------+-------+
5
| Open_tables   | 1024  |
6
| Opened_tables | 1465  |
7
+---------------+-------+
Open_tables 表示開啟表的數量,Opened_tables表示開啟過的表數量,如果Opened_tables數量過大,說明配置中 table_cache(5.1.3之後這個值叫做table_open_cache)值可能太小,我們查詢一下伺服器table_cache值

1
mysql> mysql> show variables like 'table_cache';
2
+---------------+-------+
3
| Variable_name | Value |
4
+---------------+-------+
5
| table_cache   | 1024  |
6
+---------------+-------+
Open_tables / Opened_tables * 100% =69% 理想值 (>= 85%)
Open_tables / table_cache * 100% = 100% 理想值 (<= 95%)

6 程式使用情況

1
mysql> show global status like 'Thread%';
2
+-------------------+-------+
3
| Variable_name     | Value |
4
+-------------------+-------+
5
| Threads_cached    | 31    |
6
| Threads_connected | 239   |
7
| Threads_created   | 2914  |
8
| Threads_running   | 4     |
9
+-------------------+-------+


如果我們在MySQL伺服器配置檔案中設定了thread_cache_size,當客戶端斷開之後,伺服器處理此客戶的執行緒將會快取起來以響應 下一個客戶而不是銷燬(前提是快取數未達上限)。Threads_created表示建立過的執行緒數,如果發現Threads_created值過大的 話,表明 MySQL伺服器一直在建立執行緒,這也是比較耗資源,可以適當增加配置檔案中thread_cache_size值,查詢伺服器 thread_cache_size配置:

1
mysql> show variables like 'thread_cache_size';
2
+-------------------+-------+
3
| Variable_name     | Value |
4
+-------------------+-------+
5
| thread_cache_size | 32    |
6
+-------------------+-------+
9, 查詢快取(query cache)

01
mysql> show global status like 'qcache%';
02
+-------------------------+----------+
03
| Variable_name           | Value    |
04
+-------------------------+----------+
05
| Qcache_free_blocks      | 2226     |
06
| Qcache_free_memory      | 10794944 |
07
| Qcache_hits             | 5385458  |
08
| Qcache_inserts          | 1806301  |
09
| Qcache_lowmem_prunes    | 433101   |
10
| Qcache_not_cached       | 4429464  |
11
| Qcache_queries_in_cache | 7168     |
12
| Qcache_total_blocks     | 16820    |
13
+-------------------------+----------+
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的配置:

01
mysql> show variables like 'query_cache%';
02
+------------------------------+----------+
03
| Variable_name                | Value    |
04
+------------------------------+----------+
05
| query_cache_limit            | 33554432 |
06
| query_cache_min_res_unit     | 4096     |
07
| query_cache_size             | 33554432 |
08
| query_cache_type             | ON       |
09
| query_cache_wlock_invalidate | OFF      |
10
+------------------------------+----------+
各欄位的解釋:

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%,命中率很差,可能寫操作比較頻繁吧,而且可能有些碎片。

7 檔案開啟數(open_files)

01
mysql> show global status like 'open_files';
02
+---------------+-------+
03
| Variable_name | Value |
04
+---------------+-------+
05
| Open_files    | 821   |
06
+---------------+-------+
07

08
mysql> show variables like 'open_files_limit';
09
+------------------+-------+
10
| Variable_name    | Value |
11
+------------------+-------+
12
| open_files_limit | 65535 |
13
+------------------+-------+
比較合適的設定:Open_files / open_files_limit * 100% <= 75%

正常

8 表鎖情況

1
mysql> show global status like 'table_locks%';
2
+-----------------------+---------+
3
| Variable_name         | Value   |
4
+-----------------------+---------+
5
| Table_locks_immediate | 4257944 |
6
| Table_locks_waited    | 25182   |
7
+-----------------------+---------+
Table_locks_immediate 表示立即釋放表鎖數,Table_locks_waited表示需要等待的表鎖數,如果 Table_locks_immediate / Table_locks_waited > 5000,最好採用InnoDB引擎,因為InnoDB是行鎖而MyISAM是表鎖,對於高併發寫入的應用InnoDB效果會好些.

9 表掃描情況

01
mysql> show global status like 'handler_read%';
02
+-----------------------+-----------+
03
| Variable_name         | Value     |
04
+-----------------------+-----------+
05
| Handler_read_first    | 108763    |
06
| Handler_read_key      | 92813521  |
07
| Handler_read_next     | 486650793 |
08
| Handler_read_prev     | 688726    |
09
| Handler_read_rnd      | 9321362   |
10
| Handler_read_rnd_next | 153086384 |
11
+-----------------------+-----------+
各欄位解釋參見http://hi.baidu.com/thinkinginlamp/blog/item/31690cd7c4bc5cdaa144df9c.html,調出伺服器完成的查詢請求次數:

1
mysql> show global status like 'com_select';
2
+---------------+---------+
3
| Variable_name | Value   |
4
+---------------+---------+
5
| Com_select    | 2693147 |
6
+---------------+---------+
計算表掃描率:

表掃描率 = Handler_read_rnd_next / Com_select

如果表掃描率超過4000,說明進行了太多表掃描,很有可能索引沒有建好,增加read_buffer_size值會有一些好處,但最好不要超過8MB


本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/zztfj/archive/2011/02/12/6181379.aspx

相關文章