程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> 優化mysql的limit offset的例子

優化mysql的limit offset的例子

編輯:MySQL綜合教程

優化mysql的limit offset的例子。本站提示廣大學習愛好者:(優化mysql的limit offset的例子)文章只能為提供參考,不一定能成為您想要的結果。以下是優化mysql的limit offset的例子正文


 常常碰著的一個成績是limit的offset太高,如:limit 100000,20,如許體系會查詢100020條,然後把後面的100000條都扔失落,這是開支很年夜的操作,招致查詢很慢。假定一切分頁的頁面拜訪頻率一樣,如許的查詢均勻掃描表的一折半據。優化的辦法,要末限制拜訪前面的頁數,要末晉升高偏移的查詢效力。

     一個簡略的優化方法是應用籠罩查詢(covering index)查詢,然後再跟全行的做join操作。如:


SQL>select * from user_order_info limit 1000000,5;

這條語句便可以優化為:

select * from user_order_info inner join (select pin from user_order_info limit 1000000,5) as lim using(pin);
SQL>explain select * from user_order_info limit 1000000,5;
+----+-------------+-----------------+------+---------------+------+---------+------+----------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------+------+---------------+------+---------+------+----------+-------+
| 1 | SIMPLE | user_order_info | ALL | NULL | NULL | NULL | NULL | 23131886 | |
+----+-------------+-----------------+------+---------------+------+---------+------+----------+-------+
1 row in set (0.00 sec)
SQL>explain extended select * from user_order_info inner join (select pin from user_order_info limit 1000000,5) as lim using(pin);
+----+-------------+-----------------+--------+---------------+---------+---------+---------+----------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------------+--------+---------------+---------+---------+---------+----------+----------+-------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 5 | 100.00 | |
| 1 | PRIMARY | user_order_info | eq_ref | PRIMARY | PRIMARY | 42 | lim.pin | 1 | 100.00 | |
| 2 | DERIVED | user_order_info | index | NULL | PRIMARY | 42 | NULL | 23131886 | 100.00 | Using index |
+----+-------------+-----------------+--------+---------------+---------+---------+---------+----------+----------+-------------+
3 rows in set, 1 warning (0.66 sec)


依據兩個explain的比較,可以清楚發明,第一個未應用索引,掃描了23131886行,第二個也掃描了異樣的行數,然則應用了索引,效力進步了。如許可以直接應用index獲得數據,而不去查詢表,當找到須要的數據以後,在與全表join,取得其他的列。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved