

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 评测将 SQL Server 数据库迁移至 MongoDB Atlas on AWS 的查询性能
<a name="assess-query-performance-for-migrating-sql-server-databases-to-mongodb-atlas-on-aws"></a>

*Battulga Purevragchaa, Amazon Web Services*

*Krishnakumar Sathyanarayana，US Inc PeerIslands *

*Babu Srinivasan，MongoDB*

## Summary
<a name="assess-query-performance-for-migrating-sql-server-databases-to-mongodb-atlas-on-aws-summary"></a>

这种模式为使用近乎真实的数据加载 MongoDB 以及评测尽可能接近生产场景的 MongoDB 查询性能提供指导。该评测提供的信息可帮助您规划从关系数据库迁移至MongoDB 的操作。该模式使用[PeerIslands 测试数据生成器和性能分析器](https://tools.peerislands.io/)来测试查询性能。

这种模式对于 Microsoft SQL Server 迁移至 MongoDB 特别有用，因为执行架构转换以及将数据从当前 SQL Server 实例加载到 MongoDB 可能非常复杂。相反，在开始实际迁移之前，您可以将近乎真实的数据加载至 MongoDB，了解 MongoDB 的性能并微调架构设计。

## 先决条件和限制
<a name="assess-query-performance-for-migrating-sql-server-databases-to-mongodb-atlas-on-aws-prereqs"></a>

**先决条件**
+ 一个活跃的 AWS 账户
+ 熟悉 [MongoDB Atlas](https://www.mongodb.com/docs/atlas/getting-started/)
+ 目标 MongoDB 架构
+ 典型查询模式

**限制**
+ 数据加载时间和性能受到 MongoDB 集群实例大小限制。我们建议您选择适用于生产的实例，以了解实际性能。
+ PeerIslands 测试数据生成器和性能分析器目前仅支持在线数据加载和查询。尚不支持离线批处理（例如，使用 Spark 连接器将数据加载至 MongoDB）。
+ PeerIslands 测试数据生成器和性能分析器支持集合中的字段关系。它不支持跨集合关系。

**产品版本**
+ 此模式同时支持 [MongoDB Atlas](https://www.mongodb.com/atlas) 和 [MongoDB Enterprise Advanced](https://www.mongodb.com/products/mongodb-enterprise-advanced)。

## 架构
<a name="assess-query-performance-for-migrating-sql-server-databases-to-mongodb-atlas-on-aws-architecture"></a>

**目标技术堆栈**
+ MongoDB Atlas 或 MongoDB Enterprise Advanced

**架构**

![用于评测将 SQL Server 数据库迁移至 MongoDB Atlas on AWS 的查询性能的架构。](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/25f0ab73-d587-4bd0-9fc0-ac675d5aa349/images/717caae4-d52e-4c78-bb7d-2ecb5acccd42.png)


PeerIslands 测试数据生成器和性能分析器使用 Java 和 Angular 构建，并将其生成的数据存储在亚马逊 Elastic Block Store (Amazon EBS) Elastic Block Store 上。该工具包含两个工作流：测试数据生成与性能测试。
+ 在测试数据生成中，您需要创建模板，该模板是必须生成的数据模型的 JSON 表示形式。创建模板后，您可按照负载生成配置的定义在目标集合中生成数据。
+ 在性能测试中，您可创建配置文件。配置文件是一种多阶段测试方案，您可在其中配置创建、读取、更新和删除 (CRUD) 操作、聚合管道、每个操作的权重以及每个阶段的持续时间。创建配置文件后，您可根据配置对目标数据库运行性能测试。

PeerIslands 测试数据生成器和性能分析器将其数据存储在 Amazon EBS 上，因此您可以使用任何 MongoDB 支持的连接机制（包括对等互连、允许列表和私有终端节点）将 Amazon EBS 连接到 MongoDB。默认情况下，该工具不包含操作组件；但是，如果需要，可以将其配置为适用于 Prometheus 的亚马逊托管服务、亚马逊托管 Grafana、Amazon 和 AWS Secrets Manag CloudWatch er。

## 工具
<a name="assess-query-performance-for-migrating-sql-server-databases-to-mongodb-atlas-on-aws-tools"></a>
+ [PeerIslands 测试数据生成器和性能分析器](https://tools.peerislands.io/)包括两个组件。测试数据生成器组件可帮您根据 MongoDB 架构生成高度针对客户的真实数据。该工具完全由用户界面驱动，包含丰富的数据库，可用于在 MongoDB 上快速生成数十亿条记录。该工具还提供在 MongoDB 架构中实现字段间关系的功能。Performance Analyzer 组件可帮助您生成高度针对客户的查询和聚合，并在 MongoDB 上执行真实的性能测试。您可以使用 Performance Analyzer 通过丰富的负载配置文件和针对特定用例的参数化查询来测试 MongoDB 性能。

## 最佳实践
<a name="assess-query-performance-for-migrating-sql-server-databases-to-mongodb-atlas-on-aws-best-practices"></a>

请参阅以下资源：
+ [MongoDB 架构设计最佳实践](https://www.mongodb.com/developer/products/mongodb/mongodb-schema-design-best-practices/)（MongoDB 开发人员网站）
+ 在 [AWS 上部署 MongoDB Atlas 的最佳实践（Mongo](https://www.mongodb.com/presentation/best-practices-of-deploying-mongodb-atlas-on-aws) DB 网站）
+ 使用 AWS [将应用程序安全地连接到 MongoDB Atlas 数据平面 PrivateLink（AWS 博](https://aws.amazon.com/blogs/apn/connecting-applications-securely-to-a-mongodb-atlas-data-plane-with-aws-privatelink/)客文章）
+ [MongoDB 性能最佳实践指南（MongoDB 网站](https://www.mongodb.com/basics/best-practices)）

## 操作说明
<a name="assess-query-performance-for-migrating-sql-server-databases-to-mongodb-atlas-on-aws-epics"></a>

### 了解您的源数据
<a name="understand-your-source-data"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 了解当前 SQL Server 源数据库占用空间。 | 了解当前的 SQL Server 占用空间。这可以通过对数据库 `INFORMATION` 架构运行查询来实现。确定表数量和每个表的大小。分析每个表关联的索引。有关 SQL 分析的更多信息，请参阅 PeerIslands 网站上的博客文章 [SQL2Mongo：数据迁移之旅](https://engineering.peerislands.io/sql2mongo-data-migration-journey-fec91a421d60)。 | 数据库管理员 | 
| 了解源架构。 | 确定表架构和数据的业务表示形式（例如邮政编码、名称和货币）。使用现有的实体关系 (ER) 图或者从现有数据库生成 ER 图。有关更多信息，请参阅 PeerIslands 网站上的博客文章《[SQL2Mongo：数据迁移之旅](https://engineering.peerislands.io/sql2mongo-data-migration-journey-fec91a421d60)》。 | 数据库管理员 | 
| 了解查询模式。 | 记录您使用的前 10 个 SQL 查询。你可以使用数据库中可用的 **performance\_schema.events\_statements\_summary\_by\_digest** 表了解热门查询。有关更多信息，请参阅 PeerIslands 网站上的博客文章《[SQL2Mongo：数据迁移之旅](https://engineering.peerislands.io/sql2mongo-data-migration-journey-fec91a421d60)》。 | 数据库管理员 | 
| 了解 SLA 承诺。 | 记录数据库操作的目标服务级别协议 (SLAs)。常见衡量标准包括查询延迟和每秒查询次数。这些措施及其目标通常可在非功能性需求 (NFR) 文档中查找。 | 数据库管理员 | 

### 定义 MongoDB 架构
<a name="define-the-mongodb-schema"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 定义目标架构。 | 为目标 MongoDB 架构定义多种选项。有关更多信息，请参阅 MongoDB Atlas 文档中的 [Schemas](https://www.mongodb.com/docs/atlas/app-services/schemas/)。根据表格关系考虑最佳实践与设计模式。 | MongoDB 工程师 | 
| 定义目标查询模式。 | 定义 MongoDB 查询和聚合管道。这些查询等同于您为 SQL Server 工作负载所捕获的热门查询。[要了解如何构造 MongoDB 聚合管道，请参阅 MongoDB 文档](https://www.mongodb.com/docs/manual/core/aggregation-pipeline/)。 | MongoDB 工程师 | 
| 定义 MongoDB 实例类型。 | 确定您计划用于测试的实例大小。有关指南，请参阅 [MongoDB 文档](https://www.mongodb.com/docs/atlas/sizing-tier-selection/)。 | MongoDB 工程师 | 

### 准备目标数据库
<a name="prepare-the-target-database"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 设置 MongoDB Atlas 集群。 | 要在 AWS 上设置 MongoDB 集群，请按照 [MongoDB 文档](https://www.mongodb.com/docs/atlas/tutorial/create-new-cluster/)中的说明进行操作。 | MongoDB 工程师 | 
| 在目标数据库中创建用户。 | 按照 [MongoDB 文档](https://www.mongodb.com/docs/atlas/connect-to-database-deployment/)中的说明配置 MongoDB Atlas 集群以实现访问和网络安全。 | MongoDB 工程师 | 
| 在 AWS 中创建相应的角色，并为 Atlas 配置基于角色的访问控制。 | 如有需要，请按照 [MongoDB 文档](https://www.mongodb.com/docs/atlas/security/set-up-unified-aws-access/)中的说明设置其他用户。通过 AWS 角色配置[身份验证与授权](https://www.mongodb.com/docs/atlas/security/config-db-auth/)。 | MongoDB 工程师 | 
| 设置 Compass for MongoDB Atlas 访问。 | 设置 [MongoDB Compass GUI 实用程序](https://www.mongodb.com/products/compass)以便于导航和访问。 | MongoDB 工程师 | 

### 使用测试数据生成器设置基本负荷
<a name="set-up-the-base-load-by-using-test-data-generator"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 安装测试数据生成器。 | 在您的环境中安装[PeerIsland 测试数据生成器](https://github.com/PeerIslands/loadgen_binary)。 | MongoDB 工程师 | 
| 配置测试数据生成器，以生成相应的数据。 | 使用数据库为 MongoDB 架构中的所有字段生成特定数据，从而创建模板。有关更多信息，请参阅 [MongoDB 数据生成器和性能。分析器](https://vimeo.com/570068857)视频。 | MongoDB 工程师 | 
| 水平扩展测试数据生成器，以生成所需负载。 | 使用您创建的模板，通过配置所需并行度开始针对目标集合生成负载。确定生成必要数据的时间范围与规模。 | MongoDB 工程师 | 
| 验证 MongoDB Atlas 中的负载。 | 检查加载至 MongoDB Atlas 中的数据。 | MongoDB 工程师 | 
| 在 MongoDB 上生成所需索引。 | 根据查询模式定义所需索引 有关最佳实践，请参阅 [MongoDB 文档](https://www.mongodb.com/docs/manual/applications/indexes/)。 | MongoDB 工程师 | 

### 执行性能测试
<a name="conduct-performance-testing"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在性能分析器设置负载配置文件。 | 通过配置特定的查询及其相应的权重、测试运行的持续时间和阶段，在 Performance Analyzer 中创建性能测试的配置文件。有关更多信息，请参阅 [MongoDB 数据生成器和性能。分析器](https://vimeo.com/570068857)视频。 | MongoDB 工程师 | 
| 运行性能测试。 | 使用您创建的性能测试配置文件，通过配置所需并行度开始对目标集合进行测试。水平扩展性能测试工具，以对 MongoDB Atlas 运行查询。 | MongoDB 工程师 | 
| 记录测试结果。 | 记录查询 P95、P99 延迟。 | MongoDB 工程师 | 
| 调整架构与查询模式。 | 修改索引和查询模式，以解决任何性能问题。 | MongoDB 工程师 | 

### 关闭项目
<a name="close-the-project"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 关闭临时 AWS 资源。 | 删除用于测试数据生成器与性能分析器的所有临时资源。 | AWS 管理员 | 
| 更新性能测试结果。 | 了解 MongoDB 的查询性能，并将其与您的查询性能进行比较。 SLAs如有必要，可微调 MongoDB 架构并重新运行该过程。 | MongoDB 工程师 | 
| 结束项目。 | 关闭项目并提供反馈。 | MongoDB 工程师 | 

## 相关资源
<a name="assess-query-performance-for-migrating-sql-server-databases-to-mongodb-atlas-on-aws-resources"></a>
+ GitHub 存储库：[s3toAtlas](https://github.com/mongodb-partners/S3toAtlas)
+ 架构：[MongoDB 架构设计](https://www.mongodb.com/developer/products/mongodb/mongodb-schema-design-best-practices/)
+ 聚合管道：[MongoDB 聚合管道](https://www.mongodb.com/docs/manual/core/aggregation-pipeline/)
+ MongoDB Atlas 大小调整：[大小调整等级选择](https://www.mongodb.com/docs/atlas/sizing-tier-selection/)
+ 视频：[MongoDB 数据生成器](https://vimeo.com/570068857)和性能。分析器
+ 参考文献：[MongoDB 文档](https://www.mongodb.com/docs/)
+ [教程：****[MongoDB 开发人员指南、](https://www.mongodb.com/docs/develop-applications/)MongoDB Jumpstart](https://www.youtube.com/playlist?list=PL4RCxklHWZ9v2lcat4oEVGQhZg6r4IQGV)
+ Amazon Web Services Marketplace：**·**[Amazon Web Services Marketplace 上的 MongoDB Atlas](https://aws.amazon.com/marketplace/seller-profile?id=c9032c7b-70dd-459f-834f-c1e23cf3d092)
+ AWS 合作伙伴解决方案：** **[ AWS Reference Deployment 中的 MongoDB Atlas](https://aws.amazon.com/quickstart/architecture/mongodb-atlas/)

其他资源：
+ [SQL 分析](https://engineering.peerislands.io/sql2mongo-data-migration-journey-fec91a421d60)
+ [MongoDB 开发人员社区论坛](https://www.mongodb.com/community/forums/)
+ [MongoDB 性能微调问题](https://www.mongodb.com/developer/products/mongodb/performance-tuning-tips/)
+ [使用 Atlas 和 Redshift 执行运营分析](https://github.com/mongodb-partners/Atlas_to_Redshift)
+ [使用 MongoDB Atlas 和 AWS Elastic Beanstalk 实施应用程序现代化](https://github.com/mongodb-partners/MEANStack_with_Atlas_on_AWS_EB)