mysql效能最佳化學習筆記(3)常見sql語句最佳化

來源:互聯網
上載者:User

標籤:


一、max()最佳化
mysql> explain select max(payment_date) from payment;
+----+-------------+---------+------+---------------+------+---------+------+-------+-------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows  | Extra |
+----+-------------+---------+------+---------------+------+---------+------+-------+-------+
|  1 | SIMPLE      | payment | ALL  | NULL          | NULL | NULL    | NULL | 14394 |       |
+----+-------------+---------+------+---------------+------+---------+------+-------+-------+
1 row in set (0.00 sec)

mysql> create index idx_paymentdate on  payment(payment_date);
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> explain select max(payment_date) from payment;
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                        |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Select tables optimized away |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
1 row in set (0.00 sec)

從返回行數上來說,效率比較高。

二、count()的最佳化

分別查詢出2005年和2006年影片產量

mysql> select count(release_year=2006 or null) as ‘film2006‘,count(release_year=2005 or null) as ‘film2005‘ from film;
+----------+----------+
| film2006 | film2005 |
+----------+----------+
|     1000 |        0 |
+----------+----------+
1 row in set (0.00 sec)


三、子查詢方式
通常情況下,需要把子查詢最佳化成join查詢,但是在最佳化中需要注意關聯鍵是否存在一對多關聯性,要注意重複資料(distinct)

四、group by 查詢方式
group by可能會出現暫存資料表(Using temporary),檔案排序(Using filesort)等,影響效率。
可以通過關聯的子查詢,來避免產生暫存資料表和檔案排序,可以節省io
改寫前
select actor.first_name,actor.last_name,count(*)
from sakila.film_actor
inner join sakila.actor using(actor_id)
group by film_actor.actor_id;
改寫後
select actor.first_name,actor.last_name,c.cnt
from sakila.actor inner join(
select actor_id,count(*) as cnt from sakila.film_actor group by
actor_id
)as c using(actor_id);

五、limit查詢
limit常用於分頁處理,時常會伴隨order by從句使用,因此大多時候會使用Filesorts這樣會造成大量的io問題
1.使用有索引的列或主鍵進行order by操作
2.記錄上次返回的主鍵,在下次查詢時使用主鍵過濾
使用這種方式有一個限制,就是主鍵一定要順序排序和連續的,如果主鍵出現空缺可能會導致最終頁面上顯示的列表不足5條,解決辦法是附加一列,保證這一列是自增的並增加索引就可以了


mysql效能最佳化學習筆記(3)常見sql語句最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.