您好,欢迎访问一九零五行业门户网

Mysql 分页语句Limit用法

1、mysql的limit用法 在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心, mysql 已经为我们提供了这样一个功能。 sql代码 select * from table limit[offset,] rows | rows offsetoffset limit 子句可以被用于强
1、mysql的limit用法 
在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能。
sql代码  
select * from table limit [offset,] rows | rows offset offset   
limit 子句可以被用于强制 select 语句返回指定的记录数。limit 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1): 为了与 postgresql 兼容,mysql 也支持句法: limit # offset #。
sql代码  
mysql> select * from table limit 5,10; // 检索记录行 6-15    //为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:   mysql> select * from table limit 95,-1; // 检索记录行 96-last.    //如果只给定一个参数,它表示返回最大的记录行数目:   mysql> select * from table limit 5; //检索前 5 个记录行    //换句话说,limit n 等价于 limit 0,n。   
    【引用,路人乙:mysql中limit的用法详解】
2、mysql的分页查询语句的性能分析
mysql分页sql语句,如果和mssql的top语法相比,那么mysql的limit语法要显得优雅了许多。使用它来分页是再自然不过的事情了。
2.1最基本的分页方式:
sql代码  
   select ... from ... where ... order by ... limit ...    在中小数据量的情况下,这样的sql足够用了,唯一需要注意的问题就是确保使用了索引:
举例来说,如果实际sql类似下面语句,那么在category_id, id两列上建立复合索引比较好:
sql代码  
select * from articles where category_id = 123 order by id limit 50, 10     
2.2子查询的分页方式:
随着数据量的增加,页数会越来越多,查看后几页的sql就可能类似:
sql代码  
select * from articles where category_id = 123 order by id limit 10000, 10    一言以蔽之,就是越往后分页,limit语句的偏移量就会越大,速度也会明显变慢。
此时,我们可以通过子查询的方式来提高分页效率,大致如下:
sql代码  
select * from articles where  id >=   (select id from articles  where category_id = 123 order by id limit 10000, 1) limit 10    
2.3join分页方式
sql代码  
select * from `content` as t1   join (select id from `content` order by id desc limit .($page-1)*$pagesize., 1) as t2   where t1.id order by t1.id desc limit $pagesize;         经过我的测试,join分页和子查询分页的效率基本在一个等级上,消耗的时间也基本一致。
explain sql语句:
id select_type table type possible_keys key key_len ref rows extra
1 primary system null null null null 1  
1 primary t1 range primary primary 4 null 6264 using where
2 derived content index null primary 4 null 27085 using index
----------------------------------------
为什么会这样呢?因为子查询是在索引上完成的,而普通的查询时在数据文件上完成的,通常来说,索引文件要比数据文件小得多,所以操作起来也会更有效率。
实际可以利用类似策略模式的方式去处理分页,比如判断如果是一百页以内,就使用最基本的分页方式,大于一百页,则使用子查询的分页方式。
【引用原文,energy1010的空间:mysql分页sql语句】
3、oracle分页查询语句oralce数据库 
从数据库表中第m条记录开始检索n条记录
sql代码  
select * from (select rownum r,t1.* from 表名称 t1 where rownum  where t2.r >= m     例如从表sys_option(主键为sys_id)中从第10条记录开始检索20条记录,语句如下:
sql代码  
select * from (select rownum r,t1.* from sys_option where rownum where t2.r >= 10     3、mssqlserver分页查询语句 
sql server主要利用 select top语句分页,具体方案,请参考
-------------------------------------
分页方案一:(利用not in和select top分页) 
语句形式:
sql代码  
select top 10 *   from testtable   where (id not in   (select top 20 id   from testtable   order by id))   order by id
sql代码  
select top 页大小 *   from testtable   where (id not in   (select top 页大小*页数 id   from 表   order by id))   order by id   select top 页大小 *
sql代码  
from testtable   where (id >   (select max(id)   from (select top 页大小*页数 id   from 表   order by id) as t))   order by id     -------------------------------------
分页方案二:(利用id大于多少和select top分页) 
语句形式:
sql代码  
select top 10 *   from testtable   where (id >   (select max(id)   from (select top 20 id   from testtable   order by id) as t))   order by id     ------------------------------------- 
分页方案三:(利用sql的游标存储过程分页)
sql代码  
create procedure xiaozhengge   @sqlstr nvarchar(4000), --查询字符串   @currentpage int, --第n页   @pagesize int --每页行数   as   set nocount on   declare @p1 int, --p1是游标的id   @rowcount int   exec sp_cursoropen @p1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output   select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页   set @currentpage=(@currentpage-1)*@pagesize+1   exec sp_cursorfetch @p1,16,@currentpage,@pagesize   exec sp_cursorclose @p1   set nocount off     其它的方案:如果没有主键,可以用临时表,也可以用方案三做,但是效率会低。 
建议优化的时候,加上主键和索引,查询效率会提高。
通过sql 查询分析器,显示比较:我的结论是: 
分页方案二:(利用id大于多少和select top分页)效率最高,需要拼接sql语句 
分页方案一:(利用not in和select top分页) 效率次之,需要拼接sql语句 
分页方案三:(利用sql的游标存储过程分页) 效率最差,但是最为通用
在实际情况中,要具体分析。
【引用:在sql server中通过sql语句实现分页查询 】
其它类似信息

推荐信息