使用 Azure Cosmos DB 的 API for MongoDB 进行迁移后的优化步骤Post-migration optimization steps when using Azure Cosmos DB’s API for MongoDB

将 MongoDB 数据库中存储的数据迁移到 Azure Cosmos DB 的 API for MongoDB 后,你可以连接到 Azure Cosmos DB 并管理数据。After you migrate the data stored in MongoDB database to Azure Cosmos DB’s API for MongoDB, you can connect to Azure Cosmos DB and manage the data. 本指南提供迁移后应考虑的步骤。This guide provides the steps you should consider after the migration. 有关迁移步骤,请参阅将 MongoDB 迁移到 Azure Cosmos DB 的 API for MongoDB 教程See the Migrate MongoDB to Azure Cosmos DB’s API for MongoDB tutorial for the migration steps.

本指南介绍如何:In this guide, you will learn how to:

备注

必须在应用程序级别执行的唯一一个迁移后步骤是将应用程序中的连接字符串更改为指向新的 Azure Cosmos DB 帐户。The only mandatory post-migration step on your application level is changing the connection string in your application to point to your new Azure Cosmos DB account. 所有其他迁移步骤都是建议的优化措施。All other migration steps are recommended optimizations.

连接应用程序Connect your application

  1. 在新窗口中,登录到 Azure 门户In a new window sign into the Azure portal

  2. Azure 门户的左窗格中打开“所有资源”菜单,找到已将数据迁移到的 Azure Cosmos DB 帐户。 From the Azure portal, in the left pane open the All resources menu and find the Azure Cosmos DB account to which you have migrated your data.

  3. 打开“连接字符串”边栏选项卡。 Open the Connection String blade. 右侧窗格中包含成功连接到帐户所需的全部信息。The right pane contains all the information that you need to successfully connect to your account.

  4. 使用应用程序配置(或其他相关位置)中的连接信息来反映应用中的 Azure Cosmos DB MongoDB API 连接。Use the connection information in your application's configuration (or other relevant places) to reflect the Azure Cosmos DB's API for MongoDB connection in your app.

    Connection-String

有关更多详细信息,请参阅将 MongoDB 应用程序连接到 Azure Cosmos DB 页。For more details, please see the Connect a MongoDB application to Azure Cosmos DB page.

优化索引策略Optimize the indexing policy

在将数据迁移到 Azure Cosmos DB 的过程中,默认将为所有数据字段自动编制索引。All data fields are automatically indexed, by default, during the migration of data to Azure Cosmos DB. 在许多情况下,此默认索引策略是可接受的。In many cases, this default indexing policy is acceptable. 一般情况下,删除索引可以优化写入请求,而使用默认索引策略(即自动编制索引)可以优化读取请求。In general, removing indexes optimizes write requests and having the default indexing policy (i.e., automatic indexing) optimizes read requests.

有关索引的详细信息,请参阅文章 Azure Cosmos DB MongoDB API 中的数据索引Azure Cosmos DB 中的索引For more information on indexing, see Data indexing in Azure Cosmos DB's API for MongoDB as well as the Indexing in Azure Cosmos DB articles.

全局分发数据Globally distribute your data

Azure Cosmos DB 面向中国的所有 Azure 区域提供。Azure Cosmos DB is available in all Azure regions around China. 为 Azure Cosmos DB 帐户选择默认的一致性级别后,可以关联一个或多个 Azure 区域(具体取决于多区域分发需求)。After selecting the default consistency level for your Azure Cosmos DB account, you can associate one or more Azure regions (depending on your multiple-region distribution needs). 为了实现高可用性和业务连续性,我们始终建议至少在 2 个区域中运行 Azure Cosmos DB。For high availability and business continuity, we always recommend running in at least 2 regions. 可以查看有关 Azure Cosmos DB 中多区域部署的优化成本的提示。You can review the tips for optimizing cost of multi-region deployments in Azure Cosmos DB.

若要多区域分配数据,请参阅在 Azure Cosmos DB 的 API for MongoDB 中多区域分配数据To multiple-regionally distribute your data, please see Distribute data multiple-regionally on Azure Cosmos DB’s API for MongoDB.

设置一致性级别Set consistency level

Azure Cosmos DB 提供 5 个妥善定义的一致性级别Azure Cosmos DB offers 5 well-defined consistency levels. 若要了解 MongoDB 与 Azure Cosmos DB 一致性级别之间的映射,请参阅一致性级别和 Azure Cosmos DB APITo read about the mapping between MongoDB and Azure Cosmos DB consistency levels, read Consistency levels and Azure Cosmos DB APIs. 默认一致性级别是会话一致性级别。The default consistency level is the session consistency level. 更改一致性级别是可选操作,可为应用优化一致性级别。Changing the consistency level is optional and you can optimize it for your app. 若要使用 Azure 门户更改一致性级别:To change consistency level using Azure portal:

  1. 转到“设置”下的“默认一致性”边栏选项卡。 Go to the Default Consistency blade under Settings.
  2. 选择所需的一致性级别Select your consistency level

大多数用户会将其一致性级别保留为默认的会话一致性设置。Most users leave their consistency level at the default session consistency setting. 但是,不同一致性级别的可用性和性能各有利弊However, there are availability and performance tradeoffs for various consistency levels.

后续步骤Next steps