zoukankan      html  css  js  c++  java
  • 如何优化limit

    SELECT * FROM table1 LIMIT offset, rows

    1.简单查询LIMIT优化

    假设表message表中有10万行记录,每次取1000条。

    优化前:

              SELECT message.* FROM message LIMIT 0,1000

              SELECT message.* FROM message LIMIT 1000,1000

              SELECT message.* FROM message LIMIT 2000,1000

              ……

             SELECT message.* FROM message LIMIT 998000,1000

             SELECT message.* FROM message LIMIT 999000,1000  

    优化后:

               SELECT message.* FROM message WHERE uid>0 LIMIT 1000

               SELECT message.* FROM message WHERE uid>1000 LIMIT 1000

               SELECT message.* FROM message WHERE uid>2000 LIMIT 1000

               ……

               SELECT message.* FROM message WHERE uid>998000 LIMIT 1000

               SELECT message.* FROM message WHERE uid>999000 LIMIT 1000

     

    2.复杂查询limit优化

    例如,对于下述SQL语句

    SELECT timerec FROM message WHERE evttype = 1 AND nodename = 'node1' LIMIT 0,1000

    ……

    SELECT timerec FROM message WHERE evttype = 1 AND nodename = 'node1' LIMIT 999000,1000

    ……

    优化方案:建立临时表(含自增主键)存储数十万行的查询结果,之后用第二节的方法分多次访问临时表、获取数据。

    1. 创建临时表
    2. 插入查询结果到临时表
    3. 分多次查询临时表

     

  • 相关阅读:
    2003系统IIS上传文件不能超过200K的解决方案
    ASP从编辑框中获取图片路径
    ASP 编码转换大全 UTF8、GB2312、二进制、十进制代码、十六进制
    解决IE6、IE7、IE8样式不兼容问题
    py2exe setup.py
    Python to 2bit
    python访问ACCESS
    Pamie Web自动化
    Perl 笔记
    常用工具全盗版 汗颜了
  • 原文地址:https://www.cnblogs.com/xcn123/p/10884033.html
Copyright © 2011-2022 走看看