MySQL高级 - 案例 - 系统性能优化 - 分页优化
生活随笔
收集整理的這篇文章主要介紹了
MySQL高级 - 案例 - 系统性能优化 - 分页优化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
優化count
?
創建一張表用來記錄日志表的總數據量:
create table log_counter(logcount bigint not null )engine = innodb default CHARSET = utf8;在每次插入數據之后,更新該表 :
<update id="updateLogCounter" >update log_counter set logcount = logcount + 1 </update>在進行分頁查詢時, 獲取總記錄數,從該表中查詢既可。
<select id="countLogFromCounter" resultType="long">select logcount from log_counter limit 1 </select>優化 limit
在進行分頁時,一般通過創建覆蓋索引,能夠比較好的提高性能。一個非常常見,而又非常頭疼的分頁場景就是 "limit 1000000,10" ,此時MySQL需要搜索出前1000010 條記錄后,僅僅需要返回第 1000001 到 1000010 條記錄,前1000000 記錄會被拋棄,查詢代價非常大。
當點擊比較靠后的頁碼時,就會出現這個問題,查詢效率非常慢。
優化SQL:
select * from operation_log limit 3000000 , 10;?將上述SQL優化為 :
select * from operation_log t , (select id from operation_log order by id limit 3000000,10) b where t.id = b.id ; <select id="selectListByCondition" parameterType="map" resultType="operationLog">selectid ,operate_class as operateClass ,operate_method as operateMethod,return_class as returnClass,operate_user as operateUser,operate_time as operateTime,param_and_value as paramAndValue,cost_time as costTime,return_value as returnValuefrom operation_log t,(select id from operation_log <where><include refid="oplog_where"/></where>order by id limit #{start},#{rows}) b where t.id = b.id </select>總結
以上是生活随笔為你收集整理的MySQL高级 - 案例 - 系统性能优化 - 分页优化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL高级 - 案例 - 系统性能优
- 下一篇: MySQL高级 - 案例 - 系统性能优