一. 查看庫的各鏈接狀態
對於一個mysql連接或者一個線程,任何時刻都有一個狀態,表示其當前正在做什么。一般使用show full processlist查看。
+---------+-------------+-------------------+------+---------+------+-------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +---------+-------------+-------------------+------+---------+------+-------+-----------------------+ | 2547225 | operator@RR | 10.3.18.205:60253 | fb | Query | 0 | NULL | show full processlist | | 2548100 | operator@RR | 10.4.19.83:51754 | fb | Sleep | 1 | | NULL | | 2548101 | operator@RR | 10.4.19.83:53661 | fb | Sleep | 1 | | NULL | | 2548102 | operator@RR | 10.4.19.83:45808 | fb | Sleep | 1 | | NULL | | 2548103 | operator@RR | 10.4.19.83:58881 | fb | Sleep | 1 | | NULL | | 2548104 | operator@RR | 10.4.19.83:33521 | fb | Sleep | 1 | | NULL | +---------+-------------+-------------------+------+---------+------+-------+-----------------------+
二. 查看存儲引擎
2.1 查看當前庫支持的存儲引擎
保存在information_schema.ENGINES表里。
SHOW ENGINES
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+ | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | InnoDB | DEFAULT | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | +--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
2.2 查看某存儲引擎的狀態
SHOW ENGINE INNODB STATUS
=====================================
2016-01-15 10:12:44 7f4a4c4b6700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 38 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 1 srv_active, 0 srv_shutdown, 172619 srv_idle
srv_master_thread log flush and writes: 172620
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 3
OS WAIT ARRAY INFO: signal count 3
Mutex spin waits 40, rounds 47, OS waits 1
RW-shared spins 2, rounds 60, OS waits 2
RW-excl spins 0, rounds 0, OS waits 0
Spin rounds per wait: 1.18 mutex, 30.00 RW-shared, 0.00 RW-excl
------------
TRANSACTIONS
------------
Trx id counter 1799
Purge done for trx's n:o < 0 undo n:o < 0 state: running but idle
History list length 0
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 152, OS thread handle 0x7f4a4c4b6700, query id 137 192.168.122.45 adu_w init
show engine innodb status
--------
FILE I/O
--------
I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
I/O thread 1 state: waiting for completed aio requests (log thread)
I/O thread 2 state: waiting for completed aio requests (read thread)
I/O thread 3 state: waiting for completed aio requests (read thread)
I/O thread 4 state: waiting for completed aio requests (read thread)
I/O thread 5 state: waiting for completed aio requests (read thread)
I/O thread 6 state: waiting for completed aio requests (read thread)
I/O thread 7 state: waiting for completed aio requests (read thread)
I/O thread 8 state: waiting for completed aio requests (read thread)
I/O thread 9 state: waiting for completed aio requests (read thread)
I/O thread 10 state: waiting for completed aio requests (write thread)
I/O thread 11 state: waiting for completed aio requests (write thread)
I/O thread 12 state: waiting for completed aio requests (write thread)
I/O thread 13 state: waiting for completed aio requests (write thread)
I/O thread 14 state: waiting for completed aio requests (write thread)
I/O thread 15 state: waiting for completed aio requests (write thread)
I/O thread 16 state: waiting for completed aio requests (write thread)
I/O thread 17 state: waiting for completed aio requests (write thread)
Pending normal aio reads: 0 [0, 0, 0, 0, 0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0, 0, 0, 0, 0] ,
ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
190 OS file reads, 5 OS file writes, 5 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
insert 0, delete mark 0, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 1626012
Log flushed up to 1626012
Pages flushed up to 1626012
Last checkpoint at 1626012
Max checkpoint age 867604194
Checkpoint age target 840491563
Modified age 0
Checkpoint age 0
0 pending log writes, 0 pending chkp writes
8 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 1098907648; in additional pool allocated 0
Total memory allocated by read views 88
Internal hash tables (constant factor + variable factor)
Adaptive hash index 18926128 (18921928 + 4200)
Page hash 139112 (buffer pool 0 only)
Dictionary cache 4487029 (4426736 + 60293)
File system 815920 (812272 + 3648)
Lock system 2657536 (2657176 + 360)
Recovery system 0 (0 + 0)
Dictionary memory allocated 60293
Buffer pool size 65528
Buffer pool size, bytes 1073610752
Free buffers 65354
Database pages 174
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 174, created 0, written 1
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 174, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
----------------------
INDIVIDUAL BUFFER POOL INFO
----------------------
---BUFFER POOL 0
Buffer pool size 8191
Buffer pool size, bytes 134201344
Free buffers 8159
Database pages 32
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 32, created 0, written 1
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 32, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 1
Buffer pool size 8191
Buffer pool size, bytes 134201344
Free buffers 8187
Database pages 4
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 4, created 0, written 0
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 4, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 2
Buffer pool size 8191
Buffer pool size, bytes 134201344
Free buffers 8187
Database pages 4
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 4, created 0, written 0
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 4, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 3
Buffer pool size 8191
Buffer pool size, bytes 134201344
Free buffers 8123
Database pages 68
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 68, created 0, written 0
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 68, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 4
Buffer pool size 8191
Buffer pool size, bytes 134201344
Free buffers 8129
Database pages 62
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 62, created 0, written 0
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 62, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 5
Buffer pool size 8191
Buffer pool size, bytes 134201344
Free buffers 8187
Database pages 4
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 4, created 0, written 0
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 4, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 6
Buffer pool size 8191
Buffer pool size, bytes 134201344
Free buffers 8191
Database pages 0
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 0, created 0, written 0
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 0, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 7
Buffer pool size 8191
Buffer pool size, bytes 134201344
Free buffers 8191
Database pages 0
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 0, created 0, written 0
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 0, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
0 RW transactions active inside InnoDB
0 RO transactions active inside InnoDB
0 out of 1000 descriptors used
Main thread process no. 3572, id 139955545581312, state: sleeping
Number of rows inserted 0, updated 0, deleted 0, read 0
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
二. 查看表狀態
2.1 單表詳情
數據庫各表的信息都保存在INFORMATION_SCHEMA-->TABLES表里。可以直接在此表查詢也可以通過SHOW TABLE STATUS命令來查詢。
SHOW TABLE STATUS LIKE 'user'\G;
結果:
*************************** 1. row ***************************
Name: user(表名)
Engine: InnoDB(存儲引擎)
Version: 10
Row_format: Compact(行的格式,是否固定或壓縮)
Rows: 4(行數,對於MyISAM該值是精確的,但對於InnoDB該值是估計值)
Avg_row_length: 4096(平均每行的字節數)
Data_length: 16384(表數據總的字節數)
Max_data_length: 0(表數據的最大容量,和存儲引擎有關)
Index_length: 0(索引的大小B)
Data_free: 7340032(對於MyISAM表示已分配但沒有使用的空間)
Auto_increment: 5(下一個AUTO_INCREMENT值)
Create_time: 2014-06-17 16:45:53(表的創建時間)
Update_time: NULL(表數據的最后修改時間)
Check_time: NULL(使用CHECK TABLE或myisamchk檢查表的時間)
Collation: utf8_bin(表的默認字符集和字符列排序規則)
Checksum: NULL(整個表的實時檢驗和)
Create_options: (創建表時指定的其他選項)
Comment:
1 row in set (0.00 sec)
通過查詢資料發現需要設置collate(校對) 。 collate規則:
- *_bin: 表示的是binary case sensitive collation,也就是說是區分大小寫的
- *_cs: case sensitive collation,區分大小寫
- *_ci: case insensitive collation,不區分大小寫
2.2 各表的占用空間大小
SELECT CONCAT(table_schema,'.',table_name) AS 'Table Name', table_rows AS 'Number of Rows', CONCAT(ROUND(data_length/(1024*1024),3),' MB') AS 'Data Size', CONCAT(ROUND(index_length/(1024*1024),3),' MB') AS 'Index Size', CONCAT(ROUND((data_length+index_length)/(1024*1024),3),' MB') AS 'Total Size' FROM information_schema.TABLES WHERE table_schema = 'db_name' AND table_name = 'table_name';
三. 查看執行計划
EXPLAIN sql...
explain顯示了mysql如何使用索引來處理select語句以及連接表。可以幫助選擇更好的索引和寫出更優化的查詢語句。
EXPLAIN SELECT id FROM user WHERE id=1 \G;
結果:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: user
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: const
rows: 1
Extra: Using index
1 row in set (0.02 sec)
| 列名 | 類型 | 解釋 |
|---|---|---|
| id | SELECT語句的ID編號,優先執行編號較大的查詢,如果編號相同,則從上向下執行 | |
| select_type | SIMPLE | 一條沒有UNION或子查詢部分的SELECT語句 |
| PIMARY | 最外層或最左側的SELECT語句 | |
| UNION | UNION語句里的第二條或最后一條SELECT語句 | |
| DEPENDENT UNION | 和UNION類型的含義相似,但需要依賴於某個外層查詢 | |
| UNION RESULT | 一條UNION語句的結果 | |
| SUBQUERY | 子查詢中的第一個SELECT子句 | |
| DEPENDENT SUBQUERY | 和SUBQUERY類型的含義相似,但需要依賴於某個外層查詢 | |
| DERIVED | FROM子句里的子查詢 | |
| table | t1 | 各輸出行里的信息是關於哪個數據表的 |
| Partitions | NULL | 將要使用的分區.只有EXPLAIN PARTITIONS ...語句才會顯示這一列.非分區表顯示為NULL |
| type | 聯接操作的類型,性能由好到差依次如下 | |
| system | 表中僅有一行 | |
| const | 單表中最多有一個匹配行 | |
| eq_ref | 聯接查詢中,對於前表的每一行,在此表中只查詢一條記錄,使用了PRIMARY或UNIQUE | |
| ref | 聯接查詢中,對於前表的每一行,在此表中只查詢一條記錄,使用了INDEX | |
| ref_or_null | 聯接查詢中,對於前表的每一行,在此表中只查詢一條記錄,使用了INDEX,但是條件中有NULL值查詢 | |
| index_merge | 多個索引合並 | |
| unique_subquery | 舉例說明: value IN (SELECT primary_key FROM single_table WHERE some_expr) | |
| index_subquery | 舉例說明: value IN (SELECT key_column FROM single_table WHERE some_expr) | |
| range | 只檢索給定范圍的行,包括如下操作符: =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN, or IN() | |
| index | 掃描索引樹(略比ALL快,因為索引文件通常比數據文件小) | |
| ALL | 前表的每一行數據都要跟此表匹配,全表掃描 | |
| possible_keys | NULL | MySQL認為在可能會用到的索引.NULL表示沒有找到索引 |
| key | NULL | 檢索時,實際用到的索引名稱.如果用了index_merge聯接類型,此時會列出多個索引名稱,NULL表示沒有找到索引 |
| key_len | NULL | 實際使用的索引的長度.如果是復合索引,那么只顯示使用的最左前綴的大小 |
| ref | NULL | MySQL用來與索引值比較的值, 如果是單詞const或者???,則表示比較對象是一個常數.如果是某個數據列的名稱,則表示比較操作是逐個數據列進行的.NULL表示沒有使用索引 |
| rows | MySQL為完成查詢而需要在數據表里檢查的行數的估算值.這個輸出列里所有的值的乘積就是必須檢查的數據行的各種可能組合的估算值 | |
| Extra | Using filesort | 需要將索引值寫到文件中並且排序,這樣按順序檢索相關數據行 |
| Using index | MySQL可以不必檢查數據文件, 只使用索引信息就能檢索數據表信息 | |
| Using temporary | 在使用 GROUP BY 或 ORDER BY 時,需要創建臨時表,保存中間結果集 | |
| Using where | 利用SELECT語句中的WHERE子句里的條件進行檢索操作 |
