龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 数据库类 > MySQL 技术 >

mysql limit 优化方法与详细说明

时间:2011-03-14 23:24来源:未知 作者:admin 点击:
分享到:
select * from table limit [offset,] rows | rows offset offset limit 子句可以被用于强制 select 语句返回指定的记录数。limit 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第

 select * from table limit [offset,] rows | rows offset offset

  limit 子句可以被用于强制 select 语句返回指定的记录数。limit 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1): 为了与 postgresql 兼容,mysql教程 也支持句法: limit # offset #。

  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。

  注意limit 10和limit 9,1的不同:

  例如:

  1.

 

 


  select * from cyclopedia where id>=(
  select max(id) from (
  select id from cyclopedia order by id limit 90001
  ) as tmp
  ) limit 100;


 

 

  2.

 

  select * from cyclopedia where id>=(
  select max(id) from (
  select id from cyclopedia order by id limit 90000,1
  ) as tmp
  ) limit 100;

1 2 3
精彩图集

赞助商链接