![点击图片可在新窗口打开](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1): 为了与 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。
![点击图片可在新窗口打开](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![点击图片可在新窗口打开](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![点击图片可在新窗口打开](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![点击图片可在新窗口打开](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![点击图片可在新窗口打开](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![点击图片可在新窗口打开](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![点击图片可在新窗口打开](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![点击图片可在新窗口打开](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![点击图片可在新窗口打开](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1): 为了与 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。
复合索引!
有一次设计
mysql
索引的时候,无意中发现索引名字可以任取,
可以选择几个字段进来,这有什么用呢?开始的
select
id
from
collect
order
by
id
limit
90000,10;
这么快就是因为走了索引,可是如果加了
where
就不走索引了。抱着试试看
的想法加了
search(vtype,id)
这样的索引。然后测试
select
id
from
collect
where
vtype=1
limit
90000,10;
非常快!
0.04
秒完成!
再测试
:
select
id
,title
from
collect
where
vtype=1
limit
90000,10;
非常遗憾,
8-9
秒,没走
search
索引!
再测试:
search(id,vtype)
,还是
select
id
这个语句,也非常遗憾,
0.5
秒。
综上:
如果对于有
where
条件,
又想走索引用
limit
的,
必须设计一个索引,
将
where
放
第一位,
limit
用到的主键放第
2
位,而且只能
select
主键
复合索引!
有一次设计
mysql
索引的时候,无意中发现索引名字可以任取,
可以选择几个字段进来,这有什么用呢?开始的
select
id
from
collect
order
by
id
limit
90000,10;
这么快就是因为走了索引,可是如果加了
where
就不走索引了。抱着试试看
的想法加了
search(vtype,id)
这样的索引。然后测试
select
id
from
collect
where
vtype=1
limit
90000,10;
非常快!
0.04
秒完成!
再测试
:
select
id
,title
from
collect
where
vtype=1
limit
90000,10;
非常遗憾,
8-9
秒,没走
search
索引!
再测试:
search(id,vtype)
,还是
select
id
这个语句,也非常遗憾,
0.5
秒。
综上:
如果对于有
where
条件,
又想走索引用
limit
的,
必须设计一个索引,
将
where
放
第一位,
limit
用到的主键放第
2
位,而且只能
select
主键