Docs HomeMongoDB Manual

MongoDB PerformanceMongoDB性能

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输出的locks部分和globalLock部分。

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

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, 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 don't 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:以下配置文件级别可用:

Level数量Description描述
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 or that match a filter.探查器为耗时超过slowms值或与筛选器匹配的操作集合数据。
When a filter is set: 设置筛选器时:
  • The slowms and sampleRate options are not used for profiling.slowmssampleRate选项不用于分析。
  • The profiler only captures operations that match the filter.探查器仅捕获与filter匹配的操作。
2The profiler collects data for all operations.探查器集合所有操作的数据。
Warning

Profiling can degrade performance and expose unencrypted query data in 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.有关潜在性能下降的更多信息,请参阅Profiler开销

Note

When logLevel is set to 0, MongoDB records slow operations 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设置下,所有操作都会显示在诊断日志中,无论其延迟如何,但以下情况除外:由辅助设备记录慢速操作日志条目消息The secondaries log only the slow oplog entries; increasing the logLevel does not log all oplog entries.辅助设备只记录慢速操作日志条目;增加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 help MongoDB engineers analyze server behavior, mongod and mongos processes include a Full Time Diagnostic Data Capture (FTDC) mechanism. 为了帮助MongoDB工程师分析服务器行为,mongodmongos流程包括一个全职诊断数据捕获(FTDC)机制。FTDC is enabled by default. Due to its importance in debugging deployments, FTDC thread failures are fatal and stop the parent mongod or mongos process.FTDC默认启用。由于其在调试部署中的重要性,FTDC线程故障是致命的,并会停止父mongodmongos进程。

Important

FTDC Privacy

FTDC data files are compressed and not human-readable. They 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 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 are configured with protected, confidential, or PII data, please notify MongoDB engineers before sending FTDC data to coordinate appropriate security measures.对于这些字段配置有受保护、机密或PII数据的集群,请在发送FTDC数据之前通知MongoDB工程师,以协调适当的安全措施。

Note

FTDC User Permissions on WindowsWindows上的FTDC用户权限

On Windows, to collect system data such as disk, cpu, and memory, FTDC requires Microsoft access permissions from the following groups:在Windows上,要集合磁盘、cpu和内存等系统数据,FTDC需要以下组的Microsoft访问权限:

  • Performance Monitor Users性能监视器用户
  • Performance Log Users性能日志用户

If the user running mongod and mongos is not an administrator, add them to these groups to log FTDC data. 如果运行mongodmongos的用户不是管理员,请将它们添加到这些组中以记录FTDC数据。For more information, see the Microsoft documentation here.有关详细信息,请参阅此处的Microsoft文档

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 reports 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 calculates memory utilization against the container's RAM limit, as opposed to the host operating system's RAM limit.例如,如果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截断日志路径的文件扩展名,并将诊断数据连接到剩余的名称。For example, given a path setting of /var/log/mongodb/mongos.log, the diagnostic data directory would be /var/log/mongodb/mongos.diagnostic.data.例如,如果path设置为/var/log/mongodb/mongos.log,则诊断数据目录将为/var/log/mongodb/mongos.diagnostic.data

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

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 engineers with minimal impact on performance or storage size. These values only require modifications if requested by MongoDB engineers for specific diagnostic purposes.这些默认值旨在为MongoDB工程师提供有用的数据,同时对性能或存储大小的影响最小。只有当MongoDB工程师出于特定诊断目的提出要求时,才需要修改这些值。

To disable FTDC, start up the mongod or mongos with the diagnosticDataCollectionEnabled: false option in the setParameter settings of your configuration file:要禁用FTDC,请使用配置文件的setParameter设置中的diagnosticDataCollectionEnabled: false选项启动mongodmongos

setParameter:
diagnosticDataCollectionEnabled: false

Disabling FTDC may increase the time or resources required when analyzing or debugging issues with support from MongoDB engineers. 在MongoDB工程师的支持下,禁用FTDC可能会增加分析或调试问题所需的时间或资源。For information on MongoDB Support, visit Get Started With MongoDB Support.有关MongoDB支持的信息,请访问MongoDB支持入门