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深入研究。

  • 相关阅读:
    String类的substring方法
    postman绕过登录,进行接口测试的方法
    Dubbo
    那些吊炸天的互联网名词
    版本控制工具git
    Ubunto20.04 sudo apt-get update 出现目标被重置多次!
    ubuntu环境下搭建Hadoop集群中必须需要注意的问题
    Python作业---内置数据类型
    python作业完成简单的文件操作
    python3实现计算器
  • 原文地址:https://www.cnblogs.com/zhangshuaivole/p/13563307.html
Copyright © 2011-2022 走看看