MongoDB PerformanceMongoDB性能

On this page本页内容

As you develop and operate applications with MongoDB, you may need to analyze the performance of the application and its database. 在使用MongoDB开发和操作应用程序时,您可能需要分析应用程序及其数据库的性能。When you encounter degraded performance, it is often a function of database access strategies, hardware availability, and the number of open database connections.当您遇到性能下降时,这通常是数据库访问策略、硬件可用性和打开的数据库连接数的函数。

Some users may experience performance limitations as a result of inadequate or inappropriate indexing strategies, or as a consequence of poor schema design patterns. 由于索引策略不充分或不适当,或者由于模式设计模式不佳,一些用户可能会遇到性能限制。Locking Performance discusses how these can impact MongoDB's internal locking.锁定性能讨论了这些因素如何影响MongoDB的内部锁定。

Performance issues may indicate that the database is operating at capacity and that it is time to add additional capacity to the database. 性能问题可能表明数据库正在满负荷运行,是时候向数据库添加额外容量了。In particular, the application's working set should fit in the available physical memory.特别是,应用程序的工作集应该适合可用的物理内存。

In some cases performance issues may be temporary and related to abnormal traffic load. 在某些情况下,性能问题可能是暂时的,并与异常流量负载有关。As discussed in Number of Connections, scaling can help relax excessive traffic.正如在连接数中所讨论的,缩放可以帮助缓解过多的流量。

Database Profiling数据库分析 can help you to understand what operations are causing degradation.可以帮助您了解哪些操作导致降级。

Locking Performance锁定性能

MongoDB uses a locking system to ensure data set consistency. MongoDB使用锁定系统来确保数据集的一致性。If certain operations are long-running or a queue forms, performance will degrade as requests and operations wait for the lock.如果某些操作长时间运行或形成队列,则性能将随着请求和操作等待锁定而降低。

Lock-related slowdowns can be intermittent. 与锁相关的减速可能是间歇性的。To see if the lock has been affecting your performance, refer to the locks section and the globalLock section of the serverStatus output.要查看锁是否影响了性能,请参阅serverStatus输出的部分和全局锁部分。

Dividing locks.<type>.timeAcquiringMicros by locks.<type>.acquireWaitCount can give an approximate average wait time for a particular lock mode.locks.<type>.timeAcquiringMicros除以locks.<type>.acquireWaitCount可以给出特定锁定模式的近似平均等待时间。

locks.<type>.deadlockCount provide the number of times the lock acquisitions encountered deadlocks.提供锁获取遇到死锁的次数。

If globalLock.currentQueue.total is consistently high, then there is a chance that a large number of requests are waiting for a lock. 如果globalLock.currentQueue.total始终较高,则有可能有大量请求等待锁定。This indicates a possible concurrency issue that may be affecting performance.这表明可能存在影响性能的并发问题。

If globalLock.totalTime is high relative to uptime, the database has existed in a lock state for a significant amount of time.如果globalLock.totalTime相对于正常运行时间较高uptime,则数据库已处于锁定状态很长时间。

Long queries can result from ineffective use of indexes; non-optimal schema design; poor query structure; system architecture issues; or insufficient RAM resulting in disk reads.长查询可能是由于索引的无效使用造成的;非最优方案设计;查询结构差;系统架构问题;或RAM不足导致磁盘读取。

Number of Connections连接数

In some cases, the number of connections between the applications and the database can overwhelm the ability of the server to handle requests. 在某些情况下,应用程序和数据库之间的连接数量可能会超过服务器处理请求的能力。The following fields in the serverStatus document can provide insight:serverStatus文档中的以下字段可以提供详细信息:

  • connections is a container for the following two fields:是以下两个字段的容器:

    • connections.current the total number of current clients connected to the database instance.连接到数据库实例的当前客户端总数。
    • connections.available the total number of unused connections available for new clients.新客户端可用的未使用连接总数。

If there are numerous concurrent application requests, the database may have trouble keeping up with demand. 如果有大量并发应用程序请求,数据库可能无法跟上需求。If this is the case, then you will need to increase the capacity of your deployment.如果是这种情况,则需要增加部署的容量。

For write-heavy applications, deploy sharding and add one or more shards to a sharded cluster to distribute load among mongod instances.对于写重的应用程序,部署分片并将一个或多个分片添加到分片集群,以在mongod实例之间分配负载。

Spikes in the number of connections can also be the result of application or driver errors. 连接数的峰值也可能是应用程序或驱动程序错误的结果。All of the officially supported MongoDB drivers implement connection pooling, which allows clients to use and reuse connections more efficiently. 所有官方支持的MongoDB驱动程序都实现了连接池,这允许客户端更有效地使用和重用连接。An extremely high number of connections, particularly without corresponding workload, is often indicative of a driver or other configuration error.极高数量的连接,特别是没有相应的工作负载的连接,通常表示驱动程序或其他配置错误。

Unless constrained by system-wide limits, the maximum number of incoming connections supported by MongoDB is configured with the maxIncomingConnections setting. 除非受到系统范围的限制,否则MongoDB支持的最大传入连接数是使用maxIncomingConnections设置配置的。On Unix-based systems, system-wide limits can be modified using the ulimit command, or by editing your system's /etc/sysctl file. 在基于Unix的系统上,可以使用ulimit命令或通过编辑系统的/etc/sysctl文件来修改系统范围的限制。See UNIX ulimit Settings for more information.有关详细信息,请参阅UNIX ulimit设置

Database Profiling数据库分析

The Database Profiler collects detailed information about operations run against a mongod instance. 数据库探查器集合有关针对mongod实例运行的操作的详细信息。The profiler's output can help to identify inefficient queries and operations.探查器的输出可以帮助识别效率低下的查询和操作。

You can enable and configure profiling for individual databases or for all databases on a mongod instance. 您可以为单个数据库或mongod实例上的所有数据库启用和配置评测。Profiler settings affect only a single mongod instance and will not propagate across a replica set or sharded cluster.探查器设置仅影响单个mongod实例,不会跨副本集分片集群传播。

See Database Profiler for information on enabling and configuring the profiler.有关启用和配置探查器的信息,请参阅数据库探查器

The following profiling levels are available:以下分析级别可用:

LevelDescription描述
0The profiler is off and does not collect any data. 探查器已关闭,不集合任何数据。This is the default profiler level.这是默认的探查器级别。
1The profiler collects data for operations that take longer than the value of slowms.探查器为比slowms值花费更长时间的操作集合数据。
2The profiler collects data for all operations.探查器集合所有操作的数据。
Important重要

Profiling can impact performance and shares settings with the system log. 分析可能会影响性能,并与系统日志共享设置。Carefully consider any performance and security implications before configuring and enabling the profiler on a production deployment.在生产部署中配置和启用探查器之前,请仔细考虑任何性能和安全影响。

See Profiler Overhead for more information on potential performance degradation.有关潜在性能下降的更多信息,请参阅探查器开销

Note注意

When logLevel is set to 0, MongoDB records slowoperations to the diagnostic log at a rate determined by slowOpSampleRate.logLevel设置为0时,MongoDB将以slowOpSampleRate确定的速率将操作记录到诊断日志中。

At higher logLevel settings, all operations appear in the diagnostic log regardless of their latency with the following exception: the logging of slow oplog entry messages by the secondaries. 在更高的logLevel设置下,所有操作都会显示在诊断日志中,而不管它们的延迟如何,但以下例外情况除外:辅助设备记录慢速oplog条目消息The secondaries log only the slow oplog entries; increasing the logLevel does not log all oplog entries.二级仅记录慢速oplog条目;增加logLevel不会记录所有oplog条目。

Starting in MongoDB 4.2, the profiler entries and the diagnostic log messages (i.e. mongod/mongos log messages) for read/write operations include:从MongoDB 4.2开始,用于读/写操作的探查器条目诊断日志消息(即mongod/mongos日志消息)包括:

Full Time Diagnostic Data Capture全职诊断数据捕获

To facilitate analysis of the MongoDB server behavior by MongoDB Inc. engineers, mongod and mongos processes include a Full Time Diagnostic Data Collection (FTDC) mechanism. 为了便于MongoDB股份有限公司工程师分析MongoDB服务器行为,mongodmongos进程包括一个全职诊断数据集合(FTDC)机制。FTDC data files are compressed, are not human-readable, and inherit the same file access permissions as the MongoDB data files. Only users with access to FTDC data files can transmit the FTDC data. FTDC数据文件被压缩,不可读,并继承与MongoDB数据文件相同的文件访问权限。只有能够访问FTDC数据文件的用户才能传输FTDC数据。MongoDB Inc. engineers cannot access FTDC data independent of system owners or operators. MongoDB股份有限公司的工程师无法独立于系统所有者或运算符访问FTDC数据。MongoDB processes run with FTDC on by default. 默认情况下,MongoDB进程在FTDC打开的情况下运行。For more information on MongoDB Support options, visit Getting Started With MongoDB Support.有关MongoDB支持选项的更多信息,请访问开始使用MongoDB

Important重要
FTDC Privacy

FTDC data files are compressed and not human-readable. FTDC数据文件是压缩的,不可读。MongoDB Inc. engineers cannot access FTDC data without explicit permission and assistance from system owners or operators.没有系统所有者或运算符的明确许可和协助,MongoDB股份有限公司工程师无法访问FTDC数据。

FTDC data never contains any of the following information:FTDC数据从不包含以下任何信息:

  • Samples of queries, query predicates, or query results查询、查询谓词或查询结果的示例
  • Data sampled from any end-user collection or index从任何最终用户集合或索引中采样的数据
  • System or MongoDB user credentials or security certificates系统或MongoDB用户凭据或安全证书

FTDC data contains certain host machine information such as hostnames, operating system information, and the options or settings used to start the mongod or mongos. FTDC数据包含某些主机信息,如主机名、操作系统信息以及用于启动mongodmongos的选项或设置。This information may be considered protected or confidential by some organizations or regulatory bodies, but is not typically considered to be Personally Identifiable Information (PII). 某些组织或监管机构可能认为该信息受到保护或保密,但通常不认为是个人身份信息(PII)。For clusters where these fields were configured with protected, confidential, or PII data, please notify MongoDB Inc. engineers before sending the FTDC data so appropriate measures can be taken.对于这些字段配置了受保护、机密或PII数据的集群,请在发送FTDC数据之前通知MongoDB股份有限公司的工程师,以便采取适当的措施。

FTDC periodically collects statistics produced by the following commands:FTDC定期集合由以下命令生成的统计信息:

Depending on the host operating system, the diagnostic data may include one or more of the following utilization statistics:根据主机操作系统,诊断数据可以包括以下利用率统计中的一个或多个:

  • CPU utilizationCPU利用率
  • Memory utilization内存利用率
  • Disk utilization related to performance. 与性能相关的磁盘利用率。FTDC does not include data related to storage capacity.FTDC不包括与存储容量相关的数据。
  • Network performance statistics. 网络性能统计。FTDC only captures metadata and does not capture or inspect any network packets.FTDC仅捕获元数据,不捕获或检查任何网络数据包。
Note注意

Starting in MongoDB 4.4, if the mongod process runs in a container, FTDC will report utilization statistics from the perspective of the container instead of the host operating system. 从MongoDB 4.4开始,如果mongod进程在容器中运行,FTDC将从容器而不是主机操作系统的角度报告利用率统计数据。For example, if a the mongod runs in a container that is configured with RAM restrictions, FTDC will calculate memory utilization against the container's RAM limit, as opposed to the host operating system's total available RAM.例如,如果mongod在配置有RAM限制的容器中运行,FTDC将根据容器的RAM限制计算内存利用率,而不是主机操作系统的总可用RAM。

FTDC collects statistics produced by the following commands on file rotation or startup:FTDC集合以下命令在文件旋转或启动时生成的统计信息:

mongod processes store FTDC data files in a diagnostic.data directory under the instances storage.dbPath. 进程将FTDC数据文件存储在实例storage.dbPath下的diagnostic.data目录中。All diagnostic data files are stored under this directory. 所有诊断数据文件都存储在此目录下。For example, given a dbPath of /data/db, the diagnostic data directory would be /data/db/diagnostic.data.例如,给定/data/dbdbPath,诊断数据目录将是/data/db/diagnostic.data

mongos processes store FTDC data files in a diagnostic directory relative to the systemLog.path log path setting. 进程将FTDC数据文件存储在相对于systemLog.path日志路径设置的诊断目录中。MongoDB truncates the logpath's file extension and concatenates diagnostic.data to the remaining name. MongoDB截断日志路径的文件扩展名,并将diagnostic.data连接到剩余名称。For example, given a path setting of /var/log/mongodb/mongos.log, the diagnostic data directory would be /var/log/mongodb/mongos.diagnostic.data.例如,给定/var/log/mongodb/mongos.logpath设置,诊断数据目录将为/var/log/mongodb/mongos.diagnostic.data

FTDC runs with the following defaults:FTDC使用以下默认值运行:

  • Data capture every 1 second每1秒捕获一次数据
  • 200MB maximum diagnostic.data folder size.最大diagnostic.data文件夹大小为200MB。

These defaults are designed to provide useful data to MongoDB Inc. engineers with minimal impact on performance or storage size. 这些默认值旨在为MongoDB股份有限公司的工程师提供有用的数据,对性能或存储大小的影响最小。These values only require modifications if requested by MongoDB Inc. engineers for specific diagnostic purposes.仅当MongoDB股份有限公司工程师出于特定诊断目的要求时,才需要修改这些值。

You can view the FTDC source code on the MongoDB Github Repository. 您可以在MongoDB Github存储库中查看FTDC源代码。The ftdc_system_stats_*.ccp files specifically define any system-specific diagnostic data captured.ftdc_system_stats_*.ccp文件专门定义捕获的任何系统特定诊断数据。

To disable FTDC, start up the mongod or mongos with the diagnosticDataCollectionEnabled: false option specified to the setParameter setting in your configuration file:要禁用FTDC,请启动mongodmongos,并为配置文件中的setParameter设置指定diagnosticDataCollectionEnabled: false选项:

setParameter:
  diagnosticDataCollectionEnabled: false

Disabling FTDC may increase the time or resources required when analyzing or debugging issues with support from MongoDB Inc. engineers.在MongoDB股份有限公司工程师的支持下分析或调试问题时,禁用FTDC可能会增加所需的时间或资源。

←  Development ChecklistConnection Pool Overview →