MySQL的分頁優化 (轉)

 

MySQL中一般的分頁作法大多利用Limit限制回傳的資料筆數來達成分頁效果 
例如下面的代碼 
Select * From news limit 0, 100第一頁 
Select * From news limit 100,100第二頁 
Select * From news limit 200,100第三頁 
今天突然來了一個思路 
和前作上下頁查詢優化 
的思路略同 
定位到id值後再用id值作條件 
優化的作法 
第一頁 
Select * From news Where id >=( 
Select id From news Order By id limit 0,1 
) limit 100 
第二頁 
Select * From news Where id >=( 
Select id From news Order By id limit 100,1 
) limit 100 
第三頁 
Select * From news Where id >=( 
Select id From news Order By id limit 200,1 
) limit 100 
經測試,一萬條數據以內一般的分頁作法比較快 
超過一萬條後優化過的作法優勢就呈現出來 
當數據量愈多,優化的分頁查詢速度愈快 本文來自Sqlclub

本篇文章來源於 SQL學習社區 原文鏈接:http://www.sqlclub.cn/Optimi/2009-04/2626.htm

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章