MySQL 查詢最大最小值最佳化

來源:互聯網
上載者:User

標籤:

1. 假設你使用了Innodb儲存引擎
2. 假設你在innodb設定了主鍵(叢集索引)

3. 因為叢集索引頁面之間是通過雙向鏈錶鏈接,頁按照主鍵的順序排序
每個頁中的記錄也是通過雙向鏈表維護。叢集索引上儲存了主鍵的值
由於B+樹的特性,最左端的葉子節點儲存最小的值,最右端的葉子節點儲存最大的值。

4. 最小值的一般方法:我們可以看到沒有使用key,設計的行299600行
root:employees 11:00 > select min(emp_no) from employees where gender=‘M‘;
+-------------+
| min(emp_no) |
+-------------+
| 10001 |
+-------------+
1 row in set (0.11 sec)

root:employees 11:07 > explain select min(emp_no) from employees where gender=‘M‘;
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | employees | ALL | NULL | NULL | NULL | NULL | 299600 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+

5. 利用上面的說明,取出最左端的葉子節點即可。此時我們看到執行時間很短,雖然explain結果比較困惑!
root:employees 11:12 > select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ limit 1;
+--------+
| emp_no |
+--------+
| 10001 |
+--------+
1 row in set (0.00 sec)

root:employees 11:13 > explain select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ limit 1;
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | employees | ALL | NULL | NULL | NULL | NULL | 299600 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+

6. 同樣我們執行max最大值的時候,可以先倒排在取出第一個資料。因為頁之間通過雙向鏈錶鏈接。
root:employees 11:18 > select max(emp_no) from employees where gender=‘M‘;
+-------------+
| max(emp_no) |
+-------------+
| 499999 |
+-------------+
1 row in set (0.22 sec)

root:employees 11:18 > select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ order by emp_no desc limit 1;
+--------+
| emp_no |
+--------+
| 499999 |
+--------+
1 row in set (0.00 sec)

root:employees 11:18 > explain select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ order by emp_no desc limit 1;
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | employees | index | NULL | PRIMARY | 4 | NULL | 1 | Using where |
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+

7.我們在查詢範圍的使用,也可以利用B+樹的特性來迅速查詢到我們想要的資訊。因為B+樹的索引頁儲存了主鍵的範圍;
root:employees 11:22 > explain select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ order by emp_no desc limit 1;
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | employees | index | NULL | PRIMARY | 4 | NULL | 1 | Using where |
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+

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.