7.6. LIMIT and OFFSET

7.6. LIMIT and OFFSET
7.6. LIMIT    OFFSET
LIMIT and OFFSET allow you to retrieve just a portion of the rows that are generated by the rest  of the query:
LIMIT和OFFSET允许只检索查询生成的部分行:
 
SELECT select_list
FROM table_expression
[ ORDER BY ... ]
[ LIMIT { number | ALL } ] [ OFFSET number ]
 
If a limit count is given, no more than that many rows will be returned (but possibly fewer, if the  query itself yields fewer rows). LIMIT ALL is the same as omitting the LIMIT clause, as is LIMIT  with a NULL argument.
如果限制了输出数量,那么返回行不会多于该限制(但当查询返回行比限制少时,则可能少于该限制)。 LIMIT ALL与省略LIMIT子句相同,类似于LIMIT加NULL参数。
 
OFFSET says to skip that many rows before beginning to return rows. OFFSET 0 is the same as  omitting the OFFSET clause, as is OFFSET with a NULL argument.
OFFSET指定在返回结果行时跳过的行数。OFFECT 0与省略OFFECT相同,类似于OFFSET加NULL参数。
 
If both OFFSET and LIMIT appear, then OFFSET rows are skipped before starting to count the  LIMIT rows that are returned.
如果同时指定OFFSET和LIMIT,那么先跳过OFFSET指定的行,然后再返回LIMIT限定的行数。
 
When using LIMIT , it is important to use an ORDER BY clause that constrains the result rows into a  unique order. Otherwise you will get an unpredictable subset of the query's rows. You might be asking  for the tenth through twentieth rows, but tenth through twentieth in what ordering? The ordering is  unknown, unless you specified ORDER BY .
当使用LIMIT时,使用ORDER BY子句控制输出行进行排序是很重要的。 否则,将获得查询行的不可预测的子集。假设您可能想要第十到第二十行,但是以什么顺序查询第十到第二十行呢 除非您指定ORDER BY,否则顺序是未知的。
 
The query optimizer takes LIMIT into account when generating query plans, so you are very likely  to get different plans (yielding different row orders) depending on what you give for LIMIT and  OFFSET . Thus, using different LIMIT / OFFSET values to select different subsets of a query result  will give inconsistent results unless you enforce a predictable result ordering with ORDER BY . This  is not a bug; it is an inherent consequence of the fact that SQL does not promise to deliver the results  of a query in any particular order unless ORDER BY is used to constrain the order.
查询优化器在生成查询计划时会考虑LIMIT,因此根据LIMIT和OFFSET的不同,您很可能会获得不同的执行计划(产生不同的行顺序)。因此,除非您使用ORDER BY强制获得可预测的结果排序,否则使用不同的LIMIT / OFFSET值选择查询结果的不同子集将产生不一致的结果。这不是缺陷;因为除非使用ORDER BY约束顺序,否则SQL不会保证以任何特定顺序传递查询结果,这是一个惯例。
 
The rows skipped by an OFFSET clause still have to be computed inside the server; therefore a large  OFFSET might be inefficient.
使用OFFSET子句跳过的行仍必须在服务器内部进行计算; 因此,较大的OFFSET可能使得效率不高。
发布了341 篇原创文章 · 获赞 54 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104551943