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
输出的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 除非受到系统范围的限制,否则MongoDB支持的最大传入连接数是使用maxIncomingConnections
setting. maxIncomingConnections
设置配置的。On Unix-based systems, system-wide limits can be modified using the 在基于Unix的系统上,可以使用ulimit
command, or by editing your system's /etc/sysctl
file. ulimit
命令或通过编辑系统的/etc/sysctl
文件来修改系统范围的限制。See UNIX 有关详细信息,请参阅UNIX ulimit
Settings for more information.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:以下配置文件级别可用:
0 | |
1 | slowms or that match a filter.slowms 值或与筛选器匹配的操作集合数据。 |
2 |
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开销。
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
日志消息)包括:
queryHash
to help identify slow queries with the same query shape.以帮助识别具有相同查询形状的慢速查询。planCacheKey
to provide more insight into the query plan cache for slow queries.以便为慢速查询提供对查询计划缓存的更多了解。
Full Time Diagnostic Data Capture全职诊断数据采集
To help MongoDB engineers analyze server behavior, 为了帮助MongoDB工程师分析服务器行为,mongod
and mongos
processes include a Full Time Diagnostic Data Capture (FTDC) mechanism. mongod
和mongos
流程包括一个全职诊断数据捕获(FTDC)机制。FTDC is enabled by default. Due to its importance in debugging deployments, FTDC thread failures are fatal and stop the parent FTDC默认启用。由于其在调试部署中的重要性,FTDC线程故障是致命的,并会停止父mongod
or mongos
process.mongod
或mongos
进程。
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 FTDC数据包含某些主机信息,如主机名、操作系统信息以及用于启动mongod
or mongos
. mongod
或mongos
的选项或设置。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工程师,以协调适当的安全措施。
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. mongod
和mongos
的用户不是管理员,请将它们添加到这些组中以记录FTDC数据。For more information, see the Microsoft documentation here有关详细信息,请参阅此处的Microsoft文档.
。
FTDC periodically collects statistics produced by the following commands:FTDC定期集合由以下命令生成的统计信息:
serverStatus
replSetGetStatus
(mongod
only)collStats
for thelocal.oplog.rs
collection (mongod
only)connPoolStats
(mongos
only)
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只捕获元数据,不捕获或检查任何网络数据包。
Starting in MongoDB 4.4, if the 从MongoDB 4.4开始,如果mongod
process runs in a container, FTDC reports utilization statistics from the perspective of the container instead of the host operating system. 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 进程将FTDC数据文件存储在实例diagnostic.data
directory under the instances storage.dbPath
. 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/db
的dbPath
,诊断数据目录将为/data/db/diagnostic.data
。
mongos
processes store FTDC data files in a diagnostic directory relative to the 进程将FTDC数据文件存储在相对于systemLog.path
log path setting. systemLog.path
日志路径设置的诊断目录中。MongoDB truncates the logpath's file extension and concatenates MongoDB截断日志路径的文件扩展名,并将诊断数据连接到剩余的名称。diagnostic.data
to the remaining name. 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您可以在MongoDB Github存储库. The
ftdc_system_stats_*.ccp
files specifically define any system-specific diagnostic data captured.上查看FTDC源代码。
ftdc_system_stats_*.ccp
文件专门定义捕获的任何系统特定诊断数据。
FTDC runs with the following defaults:FTDC使用以下默认值运行:
Data capture every 1 second每1秒捕获一次数据200MB maximumdiagnostic.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 要禁用FTDC,请使用配置文件的mongod
or mongos
with the diagnosticDataCollectionEnabled: false
option in the setParameter
settings of your configuration file:setParameter
设置中的diagnosticDataCollectionEnabled: false
选项启动mongod
或mongos
:
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支持入门.
。