Azure Cosmos DB 中的请求单位Request Units in Azure Cosmos DB

使用 Azure Cosmos DB 时,需要支付预配的吞吐量和每小时消耗的存储的费用。With Azure Cosmos DB, you pay for the throughput you provision and the storage you consume on an hourly basis. 必须预配吞吐量才能确保始终为 Azure Cosmos 数据库提供足够的系统资源。Throughput must be provisioned to ensure that sufficient system resources are available for your Azure Cosmos database at all times. 需要有足够的资源才能达到或超过 Azure Cosmos DB SLA 的要求。You need enough resources to meet or exceed the Azure Cosmos DB SLAs.

Azure Cosmos DB 支持多种 API,例如 SQL、MongoDB、Cassandra、Gremlin 和表。Azure Cosmos DB supports many APIs, such as SQL, MongoDB, Cassandra, Gremlin, and Table. 每个 API 具有自身的数据库操作集。Each API has its own set of database operations. 这些操作包括简单的点读取和写入,以及复杂的查询等等。These operations range from simple point reads and writes to complex queries. 每个数据库操作根据其复杂性消耗系统资源。Each database operation consumes system resources based on the complexity of the operation.

所有数据库操作的成本将由 Azure Cosmos DB 规范化,并以“请求单位”(缩写为 RU)表示。 The cost of all database operations is normalized by Azure Cosmos DB and is expressed by Request Units (or RUs, for short). 可将每秒 RU 数视为吞吐量的货币。You can think of RUs per second as the currency for throughput. 每秒 RU 数是基于费率的货币。RUs per second is a rate-based currency. 它抽象化了执行 Azure Cosmos DB 支持的数据库操作所需的系统资源,例如 CPU、IOPS 和内存。It abstracts the system resources such as CPU, IOPS, and memory that are required to perform the database operations supported by Azure Cosmos DB.

读取 1 KB 项的成本为 1 个请求单位(1 个 RU)。The cost to read a 1 KB item is 1 Request Unit (or 1 RU). 以类似方式为其他所有数据库操作分配 RU 成本。All other database operations are similarly assigned a cost using RUs. 不管使用哪个 API 来与 Azure Cosmos 容器和数据库操作交互,都始终以 RU 来计量成本。No matter which API you use to interact with your Azure Cosmos container, costs are always measured by RUs. 无论数据库操作是写入、读取还是查询,都始终以 RU 来计量成本。Whether the database operation is a write, read, or query, costs are always measured in RUs.

下图展示了 RU 的概要情况。The following image shows the high-level idea of RUs:

数据库操作消耗请求单位

为了方便管理和规划容量,Azure Cosmos DB 会确保针对给定数据集执行的给定数据库操作的 RU 数是确定性的。To manage and plan capacity, Azure Cosmos DB ensures that the number of RUs for a given database operation over a given dataset is deterministic. 可以检查响应标头来跟踪任一数据库操作消耗的 RU 数。You can examine the response header to track the number of RUs that are consumed by any database operation. 了解影响 RU 费用的因素以及应用程序吞吐量要求后,可以经济高效地运行应用程序。When you understand the factors that affect RU charges and your application's throughput requirements, you can run your application cost effectively.

可以按秒来预配应用程序的 RU 数,增量为每秒 100 RU。You provision the number of RUs for your application on a per-second basis in increments of 100 RUs per second. 若要缩放应用程序的预配吞吐量,随时可以增加或减少 RU 数。To scale the provisioned throughput for your application, you can increase or decrease the number of RUs at any time. 可以以 100 RU 为增量或减量进行缩放。You can scale in increments or decrements of 100 RUs. 可以编程方式或使用 Azure 门户进行更改。You can make your changes either programmatically or by using the Azure portal. 你需要按小时付费。You are billed on an hourly basis.

可在两个不同的粒度级别预配吞吐量:You can provision throughput at two distinct granularities:

请求单位注意事项Request Unit considerations

估算要预配的每秒 RU 数量时,请考虑以下因素:While you estimate the number of RUs per second to provision, consider the following factors:

  • 项大小:随着项的增大,读取或写入该项所要消耗的 RU 数也会增加。Item size: As the size of an item increases, the number of RUs consumed to read or write the item also increases.

  • 项索引:默认情况下会自动为每个项创建索引。Item indexing: By default, each item is automatically indexed. 如果选择不为容器中的某些项创建索引,则消耗的 RU 数将会减少。Fewer RUs are consumed if you choose not to index some of your items in a container.

  • 项属性计数:假设所有属性采用默认索引,写入某个项所要消耗的 RU 数会随着项属性计数的增加而增加。Item property count: Assuming the default indexing is on all properties, the number of RUs consumed to write an item increases as the item property count increases.

  • 带索引的属性:每个容器的索引策略都可确定默认情况下要进行索引的属性类别。Indexed properties: An index policy on each container determines which properties are indexed by default. 若要减少写入操作的 RU 消耗,请限制带索引的属性数目。To reduce the RU consumption for write operations, limit the number of indexed properties.

  • 数据一致性:在执行读取操作时,非常一致性和有限过期一致性级别消耗的 RU 数大约比其他宽松一致性级别要多两倍。Data consistency: The strong and bounded staleness consistency levels consume approximately two times more RUs while performing read operations when compared to that of other relaxed consistency levels.

  • 查询模式:查询的复杂性会影响操作使用的 RU 数。Query patterns: The complexity of a query affects how many RUs are consumed for an operation. 影响查询操作成本的因素:Factors that affect the cost of query operations include:

    • 查询结果数The number of query results
    • 谓词数The number of predicates
    • 谓词性质The nature of the predicates
    • 用户定义的函数数目The number of user-defined functions
    • 源数据的大小The size of the source data
    • 结果集的大小The size of the result set
    • 投影数Projections

    Azure Cosmos DB 保证针对相同数据重复执行的相同查询所消耗的 RU 数相同。Azure Cosmos DB guarantees that the same query on the same data always costs the same number of RUs on repeated executions.

  • 脚本的使用:与查询一样,存储过程和触发器也是根据所执行的操作的复杂性来消耗 RU。Script usage: As with queries, stored procedures and triggers consume RUs based on the complexity of the operations that are performed. 开发应用程序时,请检查请求费用标头,以更好地了解每个操作消耗的 RU 容量。As you develop your application, inspect the request charge header to better understand how much RU capacity each operation consumes.

后续步骤Next steps