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

    如果错过太阳时你流了泪,那你也要错过群星了。
    在所有的矛盾中,要优先解决主要矛盾,其他矛盾也就迎刃而解。
    不要做个笨蛋,为失去的郁郁寡欢,聪明的人,已经找到了解决问题的办法,或正在寻找。
  • 相关阅读:
    mysql view
    单点登录原理与简单实现
    复述记忆法
    英语细节锦集(基本时态的构成、元音辅音字母、)
    被动语态 动词的过去分词
    play后面加the不加the如何分辨
    正则表达式入门
    使用 lxml 中的 xpath 高效提取文本与标签属性值
    Android Studio 导入新工程项目
    winfrom Panel 问题
  • 原文地址:https://www.cnblogs.com/szrs/p/13563307.html
Copyright © 2011-2022 走看看