顯示具有 MySQL 標籤的文章。 顯示所有文章
顯示具有 MySQL 標籤的文章。 顯示所有文章

2012年7月23日 星期一

如何找到 MySQL 的瓶頸


Windows下開啟 MySQL 慢查詢
MySQL在Windows系統中的配置文件一般是是 my.ini 找到[mysqld]下面加上
log-slow-queries = C:\temp\mysqlslowquery.log
long_query_time = 3


Linux下啟用MySQL慢查詢
MySQL在Windows系統中的配置文件一般是是 /etc/my.cnf 找到[mysqld]下面加上
log-slow-queries=/tmp/slowquery.log
long_query_time=3

# service mysqld restart

然後看一下 /tmp/slowquery.log
看到慢查詢的 SQL
再看一下索引問題

如何在 MySQL中找到 SQL 瓶頸 - 利用 show profiles


寫資料庫程式時,常需要對 SQL 語法做效能調教,我發現 MySQL 的 profiling 還不錯。

1. 檢查是否有開啟 profiling 功能 (預設是關閉的)
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)

2. 開啟 profiling 功能
mysql> set profiling=1;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)

3. 執行需要測試的 SQL
mysql> SELECT xxx FROM tbl WHERE id=123

4. 檢視效能資料
mysql> show profiles;
或
mysql> show profile for query 1;

5. 關閉 profiling 功能
mysql> set profiling=0;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)