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

SQL Limit 用法_MySQL

limit 中第一个参数为offset。
在mysql 中limit是按顺序取数据的,所以要随机取数据时,使用 order by random();
netezza: 使用random() 函数
select setseed(random());
select * from table order by random() limit 100;
mysql  使用rand() 函数。
----------------------------------------------------------------------------
select * from table  limit [offset,] rows | rows offset 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。select * from table limit 5,10; #返回第6-15行数据
select * from table limit 5; #返回前5行
select * from table limit 0,5; #返回前5行
1、offset比较小的时候。
select * from yanxue8_visit limit 10,10
多次运行,时间保持在0.0004-0.0005之间
select * from yanxue8_visit where vid >=(
select vid from yanxue8_visit order by vid limit 10,1
) limit 10
多次运行,时间保持在0.0005-0.0006之间,主要是0.0006
结论:偏移offset较小的时候,直接使用limit较优。这个显然是子查询的原因。
2、offset大的时候。
select * from yanxue8_visit limit 10000,10
多次运行,时间保持在0.0187左右
select * from yanxue8_visit where vid >=(
select vid from yanxue8_visit order by vid limit 10000,1
) limit 10
多次运行,时间保持在0.0061左右,只有前者的1/3。可以预计offset越大,后者越优。
性能优化:
基于mysql5.0中limit的高性能,我对数据分页也重新有了新的认识.
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;
同样是取90000条后100条记录,第1句快还是第2句快?
第1句是先取了前90001条记录,取其中最大一个id值作为起始标识,然后利用它可以快速定位下100条记录
第2句择是仅仅取90000条记录后1条,然后取id值作起始标识定位下100条记录
第1句执行结果.100 rows in set (0.23) sec
第2句执行结果.100 rows in set (0.19) sec
很明显第2句胜出.看来limit好像并不完全像我之前想象的那样做全表扫描返回limit offset+length条记录,这样看来limit比起ms-sql的top性能还是要提高不少的.
其实第2句完全可以简化成
select * from cyclopedia where id>=(
select id from cyclopedia limit 90000,1
)limit 100;
直接利用第90000条记录的id,不用经过max运算,这样做理论上效率因该高一些,但在实际使用中几乎看不到效果,因为本身定位id返回的就是1条记录,max几乎不用运作就能得到结果,但这样写更清淅明朗,省去了画蛇那一足.
可是,既然mysql有limit可以直接控制取出记录的位置,为什么不干脆用select * from cyclopedia limit 90000,1呢?岂不更简洁?
这 样想就错了,试了就知道,结果是:1 row in set (8.88) sec,怎么样,够吓人的吧,让我想起了昨天在4.1中比这还有过之的高分.select * 最好不要随便用,要本着用什么,选什么的原则, select的字段越多,字段数据量越大,速度就越慢. 上面2种分页方式哪种都比单写这1句强多了,虽然看起来好像查询的次数更多一些,但实际上是以较小的代价换取了高效的性能,是非常值得的.
第1种方案同样可用于ms-sql,而且可能是最好的.因为靠主键id来定位起始段总是最快的.
select top 100 * from cyclopedia where id>=(
select top 90001 max(id) from (
 select id from cyclopedia order by id
) as tmp
)
但不管是实现方式是存贮过程还是直接代码中,瓶颈始终在于ms-sql的top总是要返回前n个记录,这种情况在数据量不大时感受不深,但如果成百上千万,效率肯定会低下的.相比之下mysql的limit就有优势的多,执行:
select id from cyclopedia limit 90000
select id from cyclopedia limit 90000,1
的结果分别是:
90000 rows in set (0.36) sec
1 row in set (0.06) sec
而ms-sql只能用select top 90000 id from cyclopedia 执行时间是390ms,执行同样的操作时间也不及mysql的360ms.----------------------------------------------------------limit 思考 percona performance conference 2009上,来自雅虎的几位工程师带来了一篇”efficient pagination using mysql“的报告,有很多亮点,本文是在原文基础上的进一步延伸。首先看一下分页的基本原理:mysql> explain select * from message order by id desc limit 10000, 20g
***************** 1. row **************
id: 1
select_type: simple
table: message
type: index
possible_keys: null
key: primary
key_len: 4
ref: null
rows: 10020
extra:
1 row in set (0.00 sec)limit 10000,20的意思扫描满足条件的10020行,扔掉前面的10000行,返回最后的20行,问题就在这里,如果是limit 100000,100,需要扫描100100行,在一个高并发的应用里,每次查询需要扫描超过10w行,性能肯定大打折扣。文中还提到limit n性能是没问题的,因为只扫描n行。文中提到一种”clue”的做法,给翻页提供一些”线索”,比如还是select * from message order by id desc,按id降序分页,每页20条,当前是第10页,当前页条目id最大的是9527,最小的是9500,如果我们只提供”上一页”、”下一页”这样 的跳转(不提供到第n页的跳转),那么在处理”上一页”的时候sql语句可以是:select * from message where id > 9527 order by id asc limit 20;处理”下一页”的时候sql语句可以是:select * from message where id ”这样的链接方式,怎么办呢?如果limit m,n不可避免的话,要优化效率,只有尽可能的让m小一下,我们扩展前面的”clue”做法,还是select * from message order by id desc,按id降序分页,每页20条,当前是第10页,当前页条目id最大的是9527,最小的是9500,比如要跳到第8页,我看的sql语句可以这 样写:select * from message where id > 9527 order by id asc limit 20,20;跳转到第13页:select * from message where id select * from table  limit [offset,] rows | rows offset 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。select * from table limit 5,10; #返回第6-15行数据
select * from table limit 5; #返回前5行
select * from table limit 0,5; #返回前5行 
1、offset比较小的时候。
select * from yanxue8_visit limit 10,10
多次运行,时间保持在0.0004-0.0005之间
select * from yanxue8_visit where vid >=(
select vid from yanxue8_visit order by vid limit 10,1
) limit 10
多次运行,时间保持在0.0005-0.0006之间,主要是0.0006
结论:偏移offset较小的时候,直接使用limit较优。这个显然是子查询的原因。
2、offset大的时候。
select * from yanxue8_visit limit 10000,10
多次运行,时间保持在0.0187左右
select * from yanxue8_visit where vid >=(
select vid from yanxue8_visit order by vid limit 10000,1
) limit 10
多次运行,时间保持在0.0061左右,只有前者的1/3。可以预计offset越大,后者越优。
性能优化:
基于mysql5.0中limit的高性能,我对数据分页也重新有了新的认识.
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;
同样是取90000条后100条记录,第1句快还是第2句快?
第1句是先取了前90001条记录,取其中最大一个id值作为起始标识,然后利用它可以快速定位下100条记录
第2句择是仅仅取90000条记录后1条,然后取id值作起始标识定位下100条记录
第1句执行结果.100 rows in set (0.23) sec
第2句执行结果.100 rows in set (0.19) sec
很明显第2句胜出.看来limit好像并不完全像我之前想象的那样做全表扫描返回limit offset+length条记录,这样看来limit比起ms-sql的top性能还是要提高不少的.
其实第2句完全可以简化成
select * from cyclopedia where id>=(
select id from cyclopedia limit 90000,1
)limit 100;
直接利用第90000条记录的id,不用经过max运算,这样做理论上效率因该高一些,但在实际使用中几乎看不到效果,因为本身定位id返回的就是1条记录,max几乎不用运作就能得到结果,但这样写更清淅明朗,省去了画蛇那一足.
可是,既然mysql有limit可以直接控制取出记录的位置,为什么不干脆用select * from cyclopedia limit 90000,1呢?岂不更简洁?
这 样想就错了,试了就知道,结果是:1 row in set (8.88) sec,怎么样,够吓人的吧,让我想起了昨天在4.1中比这还有过之的高分.select * 最好不要随便用,要本着用什么,选什么的原则, select的字段越多,字段数据量越大,速度就越慢. 上面2种分页方式哪种都比单写这1句强多了,虽然看起来好像查询的次数更多一些,但实际上是以较小的代价换取了高效的性能,是非常值得的.
第1种方案同样可用于ms-sql,而且可能是最好的.因为靠主键id来定位起始段总是最快的.
select top 100 * from cyclopedia where id>=(
select top 90001 max(id) from (
 select id from cyclopedia order by id
) as tmp
)
但不管是实现方式是存贮过程还是直接代码中,瓶颈始终在于ms-sql的top总是要返回前n个记录,这种情况在数据量不大时感受不深,但如果成百上千万,效率肯定会低下的.相比之下mysql的limit就有优势的多,执行:
select id from cyclopedia limit 90000
select id from cyclopedia limit 90000,1
的结果分别是:
90000 rows in set (0.36) sec
1 row in set (0.06) sec
而ms-sql只能用select top 90000 id from cyclopedia 执行时间是390ms,执行同样的操作时间也不及mysql的360ms.----------------------------------------------------------limit 思考 percona performance conference 2009上,来自雅虎的几位工程师带来了一篇”efficient pagination using mysql“的报告,有很多亮点,本文是在原文基础上的进一步延伸。首先看一下分页的基本原理:mysql> explain select * from message order by id desc limit 10000, 20g
***************** 1. row **************
id: 1
select_type: simple
table: message
type: index
possible_keys: null
key: primary
key_len: 4
ref: null
rows: 10020
extra:
1 row in set (0.00 sec)limit 10000,20的意思扫描满足条件的10020行,扔掉前面的10000行,返回最后的20行,问题就在这里,如果是limit 100000,100,需要扫描100100行,在一个高并发的应用里,每次查询需要扫描超过10w行,性能肯定大打折扣。文中还提到limit n性能是没问题的,因为只扫描n行。文中提到一种”clue”的做法,给翻页提供一些”线索”,比如还是select * from message order by id desc,按id降序分页,每页20条,当前是第10页,当前页条目id最大的是9527,最小的是9500,如果我们只提供”上一页”、”下一页”这样 的跳转(不提供到第n页的跳转),那么在处理”上一页”的时候sql语句可以是:select * from message where id > 9527 order by id asc limit 20;处理”下一页”的时候sql语句可以是:select * from message where id ”这样的链接方式,怎么办呢?如果limit m,n不可避免的话,要优化效率,只有尽可能的让m小一下,我们扩展前面的”clue”做法,还是select * from message order by id desc,按id降序分页,每页20条,当前是第10页,当前页条目id最大的是9527,最小的是9500,比如要跳到第8页,我看的sql语句可以这 样写:select * from message where id > 9527 order by id asc limit 20,20;跳转到第13页:select * from message where id
其它类似信息

推荐信息