Azure Cosmos DB 中的 OFFSET LIMIT 子句OFFSET LIMIT clause in Azure Cosmos DB

OFFSET LIMIT 子句是一个可选子句,可以跳过它,然后从查询中获取一些值。The OFFSET LIMIT clause is an optional clause to skip then take some number of values from the query. 必须在 OFFSET LIMIT 子句中指定 OFFSET 计数和 LIMIT 计数。The OFFSET count and the LIMIT count are required in the OFFSET LIMIT clause.

将 OFFSET LIMIT 与 ORDER BY 子句结合使用时,将通过跳过然后提取排序值来生成结果集。When OFFSET LIMIT is used in conjunction with an ORDER BY clause, the result set is produced by doing skip and take on the ordered values. 如果不使用 ORDER BY 子句,则会生成值的确定顺序。If no ORDER BY clause is used, it will result in a deterministic order of values.

语法Syntax

OFFSET <offset_amount> LIMIT <limit_amount>

参数Arguments

  • <offset_amount>

    指定查询结果应跳过的项数(整数)。Specifies the integer number of items that the query results should skip.

  • <limit_amount>

    指定查询结果应包含的项数(整数)Specifies the integer number of items that the query results should include

备注Remarks

必须在 OFFSET LIMIT 子句中同时指定 OFFSET 计数和 LIMIT 计数。Both the OFFSET count and the LIMIT count are required in the OFFSET LIMIT clause. 如果使用可选的 ORDER BY 子句,将会通过跳过排序值来生成结果集。If an optional ORDER BY clause is used, the result set is produced by doing the skip over the ordered values. 否则,查询将返回固定顺序的值。Otherwise, the query will return a fixed order of values.

使用 OFFSET LIMIT 进行查询时的 RU 费用会随着要跳过的字词数量的增加而增加。The RU charge of a query with OFFSET LIMIT will increase as the number of terms being offset increases. 对于有多个结果页的查询,我们通常建议使用继续标记For queries that have multiple pages of results, we typically recommend using continuation tokens. 继续标记是一个“书签”,用于在以后恢复查询。Continuation tokens are a "bookmark" for the place where the query can later resume. 如果使用 OFFSET LIMIT,则没有“书签”。If you use OFFSET LIMIT, there is no "bookmark". 如果希望返回查询的下一页,则必须从头开始。If you wanted to return the query's next page, you would have to start from the beginning.

如果希望完全跳过项并保存客户端资源,则应使用 OFFSET LIMITYou should use OFFSET LIMIT for cases when you would like to skip items entirely and save client resources. 例如,如果要跳到第 1000 个查询结果,无需查看第 1 到第 999 个结果,则应使用 OFFSET LIMITFor example, you should use OFFSET LIMIT if you want to skip to the 1000th query result and have no need to view results 1 through 999. 在后端,OFFSET LIMIT 仍会加载每个项,包括那些跳过的项。On the backend, OFFSET LIMIT still loads each item, including those that are skipped. 性能优势是,通过避免处理不需要的项,可以节省客户端资源。The performance advantage is a savings in client resources by avoiding processing items that are not needed.

示例Examples

例如,以下查询跳过第一个值并返回第二个值(按居住城市名称的顺序):For example, here's a query that skips the first value and returns the second value (in order of the resident city's name):

    SELECT f.id, f.address.city
    FROM Families f
    ORDER BY f.address.city
    OFFSET 1 LIMIT 1

结果有:The results are:

    [
      {
        "id": "AndersenFamily",
        "city": "Seattle"
      }
    ]

以下查询跳过第一个值并返回第二个值(不排序):Here's a query that skips the first value and returns the second value (without ordering):

    SELECT f.id, f.address.city
    FROM Families f
    OFFSET 1 LIMIT 1

结果有:The results are:

    [
      {
        "id": "WakefieldFamily",
        "city": "Seattle"
      }
    ]

后续步骤Next steps