MySQL中OPTIMIZE TABLE和慢查詢分析


先來看看手冊中關於 OPTIMIZE 的描述:

OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ...

如果您已經刪除了表的一大部分,或者如果您已經對含有可變長度行的表(含有VARCHAR, BLOB或TEXT列的表)進行了很多更改,則應使用
OPTIMIZE TABLE。被刪除的記錄被保持在鏈接清單中,后續的INSERT操作會重新使用舊的記錄位置。您可以使用OPTIMIZE TABLE來重新
利用未使用的空間,並整理數據文件的碎片。

在多數的設置中,您根本不需要運行OPTIMIZE TABLE。即使您對可變長度的行進行了大量的更新,您也不需要經常運行,每周一次或每月一次
即可,只對特定的表運行。

OPTIMIZE TABLE只對MyISAM, BDB和InnoDB表起作用。注意,在OPTIMIZE TABLE運行過程中,MySQL會鎖定表。
備注:alter table xxxx engine=innodb這個等於optimzize table xxxx的效果
 
        

下面進行實操:

一,原始數據

1.數據量

mysql> select count(*) as total from ad_visit_history; 
+---------+ 
| total | 
+---------+ 
| 1187096 | //總共有118萬多條數據 
+---------+ 
1 row in set (0.04 sec)

2.存放在硬盤中的表文件大小(查看數據文件存儲的路徑可以使用命令:show variables like '%datadir%'

[root@ localhost linys]# ls | grep visit  | xargs -i du {} 
382020 ad_visit_history.MYD //數據文件占了380M 
127116 ad_visit_history.MYI //索引文件占了127M 
12 ad_visit_history.frm //結構文件占了12K

3.查看一下索引信息

mysql> show index from ad_visit_history from test1; //查看一下該表的索引信息 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
| ad_visit_history | 0 | PRIMARY | 1 | id | A | 1187096 | NULL | NULL | | BTREE | | 
| ad_visit_history | 1 | ad_code | 1 | ad_code | A | 46 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | unique_id | 1 | unique_id | A | 1187096 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | ad_code_ind | 1 | ad_code | A | 46 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | from_page_url_ind | 1 | from_page_url | A | 30438 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | ip_ind | 1 | ip | A | 593548 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | port_ind | 1 | port | A | 65949 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | session_id_ind | 1 | session_id | A | 1187096 | NULL | NULL | YES | BTREE | | 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
8 rows in set (0.28 sec)

索引信息中的列的信息說明。

Table :表的名稱。
Non_unique:如果索引不能包括重復詞,則為0。如果可以,則為1。
Key_name:索引的名稱。
Seq_in_index:索引中的列序列號,從1開始。
Column_name:列名稱。
Collation:列以什么方式存儲在索引中。在MySQLSHOW INDEX語法中,有值’A’(升序)或NULL(無分類)。
Cardinality:索引中唯一值的數目的估計值。通過運行ANALYZE TABLE或myisamchk -a可以更新。基數根據被存儲為整數的統計數據來計數,所以即使對於小型表,該值也沒有必要是精確的。基數越大,當進行聯合時,MySQL使用該索引的機會就越大。
Sub_part:如果列只是被部分地編入索引,則為被編入索引的字符的數目。如果整列被編入索引,則為NULL。
Packed:指示關鍵字如何被壓縮。如果沒有被壓縮,則為NULL。
Null:如果列含有NULL,則含有YES。如果沒有,則為空。
Index_type:存儲索引數據結構方法(BTREE, FULLTEXT, HASH, RTREE)

二,刪除一半數據

mysql> delete from ad_visit_history where id>598000; //刪除一半數據 
Query OK, 589096 rows affected (4 min 28.06 sec)

[root@localhost linys]# ls | grep visit | xargs -i du {} //相對應的MYD,MYI文件大小沒有變化 
382020 ad_visit_history.MYD 
127116 ad_visit_history.MYI 
12 ad_visit_history.frm

按常規思想來說,如果在數據庫中刪除了一半數據后,相對應的.MYD,.MYI文件也應當變為之前的一半。但是刪除一半數據后,.MYD.MYI盡然連1KB都沒有減少,這是多么的可怕啊。

我們在來看一看,索引信息
mysql> show index from ad_visit_history; 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
| ad_visit_history | 0 | PRIMARY | 1 | id | A | 598000 | NULL | NULL | | BTREE | | 
| ad_visit_history | 1 | ad_code | 1 | ad_code | A | 23 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | unique_id | 1 | unique_id | A | 598000 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | ad_code_ind | 1 | ad_code | A | 23 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | from_page_url_ind | 1 | from_page_url | A | 15333 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | ip_ind | 1 | ip | A | 299000 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | port_ind | 1 | port | A | 33222 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | session_id_ind | 1 | session_id | A | 598000 | NULL | NULL | YES | BTREE | | 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
8 rows in set (0.00 sec)

對比一下,這次索引查詢和上次索引查詢,里面的數據信息基本上是上次一次的一本,這點還是合乎常理。

 

三,用optimize table來優化一下

??mysql> optimize table ad_visit_history; //刪除數據后的優化 
+------------------------+----------+----------+----------+ 
| Table | Op | Msg_type | Msg_text | 
+------------------------+----------+----------+----------+ 
| test1.ad_visit_history | optimize | status | OK | 
+------------------------+----------+----------+----------+ 
1 row in set (1 min 21.05 sec)

1.查看一下.MYD,.MYI文件的大小

[root@localhost linys]# ls | grep visit | xargs -i du {} 
182080 ad_visit_history.MYD //數據文件差不多為優化前的一半 
66024 ad_visit_history.MYI //索引文件也一樣,差不多是優化前的一半 
12 ad_visit_history.frm

2.查看一下索引信息
mysql> show index from ad_visit_history; 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
| ad_visit_history | 0 | PRIMARY | 1 | id | A | 598000 | NULL | NULL | | BTREE | | 
| ad_visit_history | 1 | ad_code | 1 | ad_code | A | 42 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | unique_id | 1 | unique_id | A | 598000 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | ad_code_ind | 1 | ad_code | A | 42 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | from_page_url_ind | 1 | from_page_url | A | 24916 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | ip_ind | 1 | ip | A | 598000 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | port_ind | 1 | port | A | 59800 | NULL | NULL | YES | BTREE | | 
| ad_visit_history | 1 | session_id_ind | 1 | session_id | A | 598000 | NULL | NULL | YES | BTREE | | 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
8 rows in set (0.00 sec)

從以上數據我們可以得出,ad_code,ad_code_ind,from_page_url_ind等索引機會差不多都提高了85%,這樣效率提高了好多。

四,OPTIMIZE TABLE總結

結合mysql官方網站的信息,個人是這樣理解的。當你刪除數據時,mysql並不會回收,被已刪除數據的占據的存儲空間,以及索引位。

而是空在那里,而是等待新的數據來彌補這個空缺,這樣就有一個缺少,如果一時半會,沒有數據來填補這個空缺,那這樣就太浪費資源了。

所以對於寫比較頻煩的表,要定期進行optimize,一個月一次,看實際情況而定了。

 

mysql慢查詢日志分析:

1.如何開啟慢查詢日志?

在mysql配置文件my.cnf中增加

log-slow-queries=/var/lib/mysql/slowquery.log (指定日志文件存放位置,可以為空,系統會給一個缺省的文件host_name-slow.log)
long_query_time=2 (記錄超過的時間,默認為10s)
log-queries-not-using-indexes (log下來沒有使用索引的query,可以根據情況決定是否開啟)
log-long-format (如果設置了,所有沒有使用索引的查詢也將被記錄)

 

2.使用mysql自帶命令mysqldumpslow查看

常用命令
-s ORDER what to sort by (t, at, l, al, r, ar etc), 'at’ is default
-t NUM just show the top n queries
-g PATTERN grep: only consider stmts that include this string

eg:
s,是order的順序,說明寫的不夠詳細,俺用下來,包括看了代碼,主要有 c,t,l,r和ac,at,al,ar,分別是按照query次數,時間,lock的時間和返回的記錄數來排序,前面加了a的時倒序 -t,是top n的意思,即為返回前面多少條的數據 -g,后邊可以寫一個正則匹配模式,大小寫不敏感的

mysqldumpslow -s c -t 20 host-slow.log
mysqldumpslow -s r -t 20 host-slow.log
上述命令可以看出訪問次數最多的20個sql語句和返回記錄集最多的20個sql。

mysqldumpslow -t 10 -s t -g “left join” host-slow.log這個是按照時間返回前10條里面含有左連接的sql語句。

root@server# tail /var/log/slowqueries

# Time: 130320 7:30:26
# User@Host: db_user[db_database] @ localhost []
# Query_time: 4.545309 Lock_time: 0.000069 Rows_sent: 219 Rows_examined: 254
SET timestamp=1363779026;
SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes';
我們來過一下每一行所代表的意思:
第一行表示記錄日志時的時間。其格式是 YYMMDD H:M:S。我們可以看出上面的查詢記錄於 2013 年 3 月 20 日上午 7:30 - 注意:這個是服務器時間,可能跟你本地時間有所不同然后,我們可以看到 MySql 用戶、服務器以及主機名第三行表示總的查詢時間、鎖定時間、"發送"或者返回的行數、查詢過程中所檢查的行數接下來我們看到的是 SET timestamp=UNIXTIME; 這是查詢實際發生的時間。如果你想找現在的一些慢查詢,通過檢查這個就不會發生你所檢查的是幾個月之前所發生的慢查詢了。

SET timestamp= value 才是實際的查詢的執行時間。

 

 

文章參考出處

https://www.cnblogs.com/jimmy-muyuan/p/5874410.html

https://www.cnblogs.com/jimmy-muyuan/p/5874400.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM