mysql效能最佳化

來源:互聯網
上載者:User

標籤:

sql及索引最佳化開啟慢查詢日誌查看慢查詢日誌是否開啟

mysql> show variables like ‘slow_query_log‘;

+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| slow_query_log | OFF |
+----------------+-------+
1 row in set (0.00 sec)

mysql> set global slow_query_log=on;
Query OK, 0 rows affected (0.05 sec)

慢查詢記錄檔儲存位置

mysql> show variables like ‘%slow_query_log_file%‘;
+---------------------+----------------------------------------------+
| Variable_name | Value |
+---------------------+----------------------------------------------+
| slow_query_log_file | C:\xampp\mysql\data\80CEAE742547827-slow.log |
+---------------------+----------------------------------------------+
2 rows in set (0.00 sec)

是否記錄沒有使用索引的sql

mysql> show variables like ‘%log_queries_not_using%‘;
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | OFF |

+-------------------------------+-------+
1 row in set (0.00 sec)

mysql> set global log_queries_not_using_indexes=on;
Query OK, 0 rows affected (0.00 sec)

執行時間大於N秒的SQL會被記錄

mysql> show variables like ‘%long_query%‘;
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)

mysql> set global long_query_time=1;
Query OK, 0 rows affected (0.00 sec)

group by語句最佳化

最佳化前

select actor.first_name, actor.last_name, count(*)from sakila.film_actorINNER JOIN sakila.actor USING(actor_id)GROUP BY film_actor.actor_id

 

使用join子查詢的方式最佳化後,actor表沒有在使用檔案排序和暫存資料表

最佳化後

select actor.first_name, actor.last_name, c.cntfrom sakila.actorINNER JOIN (    select actor_id, count(*) as cnt from sakila.film_actor GROUP BY actor_id) as c using(actor_id)

雖然最佳化後仍然掃描了200行的記錄,但是actor表沒有使用檔案排序和暫存資料表

資料結構最佳化系統配置最佳化伺服器硬體最佳化

mysql效能最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.