zoukankan      html  css  js  c++  java
  • Mysql数据量较大时分页查询优化

    据表 collect ( id, title ,info ,vtype) 就这4个字段,其中 title 用定长,info 用text, id 是主键,vtype是int,vtype是索引。

    最后collect 为 10万条记录,看下面这条sql语句: 

    select id,title from collect limit 1000,10; 很快;基本上0.01秒就OK。

    再来看这条语句: 

    select id,title from collect limit 90000,10; 

    从9万条开始分页,结果8-9秒完成,性能严重的差。

    可以这样优化: 

    select id from collect order by id limit 90000,10; 很快,0.04秒就OK。 

    使用id主键做索引,速度没得说。

    改进后的方法: 

    select id,title from collect where id>=(select id from collect order by id 
    limit 90000,1) limit 10;  

    这条语句依然是用了id做索引的结果。

    测试:

    -- 创建索引查询与没有索引查询比较
    CREATE INDEX t_outstanding_treaty_assessdate ON t_outstanding_treaty(assessdate);
    
    select * from t_outstanding_treaty where assessdate = '2019-12-31';//当数据量较大时,时间差异较大
    
    -- 提高分页查询效率
    
    -- select *from table_name limit m,n;
    如每页10条数据,第三页:3,10 m为 (3-1)*10=20 ==》20是要开始取数的位置的下标。 -- 每页10条数 第300页: (300-1)*10,10 2999 10

    --原始 select * from t_outstanding_treaty LIMIT 2990,10select * from t_outstanding_treaty limit 2990,10; select * from t_outstanding_treaty order by treatyid,sectionno,accperiod,uwyear,acccurrency,exchrate,exchratenew,freinscode,retreatyid,refreinscode,reacccurrency,reexchrate,reexchratenew,assessdate,acckind limit 2990,10;

    可以参看链接:https://zhuanlan.zhihu.com/p/92552787深入研究。

  • 相关阅读:
    软工5--结对2--四则运算生成器
    软工4——结对审查
    天数计算器
    翻转字符串
    位图排序
    软工 assignment 3 —— 求最大子数列之和
    剑指offer 面试题5.替换空格
    剑指offer 面试题4.二维数组中的查找
    剑指offer 面试题3.数组中重复的数字
    如何通过onenote发送博客到cnblog(博客园)
  • 原文地址:https://www.cnblogs.com/zhangshuaivole/p/13563307.html
Copyright © 2011-2022 走看看