select `id`,`title`,`describle`,`created` from myvbga_table where click = xxx limit offset, limit; //总结:如果没有blob/text字段,单行记录比较小,可以把 limit 设大点,会加快速度。
limit offset值比较小:
select `id`,`title`,`describle`,`created` from vbga_table limit 10,10 //多次运行,时间保持在0.0004-0.0005之间 select `id`,`title`,`describle`,`created` from vbga_table where click >=(select click from vbga_table order by click limit 10,1) limit 10 //多次运行,时间保持在0.0005-0.0006之间,主要是0.0006
limit offset值比较大:
select `id`,`title`,`describle`,`created` from vbga_table limit 10000,10 //多次运行,时间保持在0.0187左右 select `id`,`title`,`describle`,`created` from vbga_table where click >=(select click from vbga_table order by click limit 10000,1) limit 10
//多次运行,时间保持在0.0061左右,只有前者的1/3。可以预计offset越大,后者越优。
mysql的limit用法:
limit 子句可以被用于强制 select 语句返回指定的记录数
select `id`,`title`,`describle`,`created` from vbga_table limit [offset,] rows | rows offset offset mysql> select `id`,`title`,`describle`,`created` from vbga_table limit 5,10; // 检索记录行 6-15 //为了检索从某一个偏移量到记录集的结束所有的记录行, 可以指定第二个参数为 -1: mysql> select `id`,`title`,`describle`,`created` from vbga_table limit 95,-1; // 检索记录行 96-last. //如果只给定一个参数,它表示返回最大的记录行数目: mysql> select `id`,`title`,`describle`,`created` from vbga_table limit 5; //检索前 5 个记录行 //换句话说,limit n 等价于 limit 0,n。
mysql limit 子查询用法示例:
select `id`,`title`,`describle`,`created` from vbga_table where id in (select t.id from (select `id`,`title`,`describle`,`created` from vbga_table limit 10)as t)
mysql limit offset用法:
select keyword from `zjoe_table` where advertiserid='59' order by keyword limit 2 offset 1; //比如这个sql ,limit后面跟的是2条数据,offset后面是从第1条开始读取
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
select `keyword` from `zjoe_table` where advertiserid='59' order by keyword limit 2 ,1; //而这个sql,limit后面是从第2条开始读,读取1条信息。
mysql存储过程中limit变量用法
create procedure getble_table(_id int,_limit int) begin prepare s1 from 'select `id`,`title`,`describle`,`created` from ble_table where cityid=? order by sendtime desc limit ?'; set @a=_id; set @b=_limit; execute s1 using @a,@b; deallocate prepare s1; end;
注意:需要传参数的地方一定要用?号,第一个from后面的语句要用''括起。
以上就是mysql,limit,高级用法的内容。
