Database Manual / Administration

MongoDB PerformanceMongoDB性能

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

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

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部分。

Note

Some serverStatus response fields are not returned on MongoDB Atlas M0 clusters or Flex clusters. For more information, see Limited Commands in the MongoDB Atlas documentation.MongoDB Atlas M0集群或Flex集群上没有返回某些serverStatus响应字段。有关更多信息,请参阅MongoDB Atlas文档中的有限命令

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. This indicates a possible concurrency issue that may be affecting performance.如果globalLock.currentQueue.total一直很高,那么有可能有大量请求正在等待锁。这表明可能存在影响性能的并发问题。

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. An extremely high number of connections, particularly without corresponding workload, is often indicative of a driver or other configuration error.连接数量的激增也可能是应用程序或驱动程序错误的结果。所有官方支持的MongoDB驱动程序都实现了连接池,这允许客户端更有效地使用和重用连接。极高数量的连接,特别是没有相应工作负载的连接,通常表明存在驱动程序或其他配置错误。

Self-Managed Connection Limits自我管理连接限制

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. See UNIX ulimit Settings for Self-Managed Deployments for more information.在基于Unix的系统上,可以使用ulimit命令或通过编辑系统的/etc/sysctl文件来修改系统范围的限制。有关详细信息,请参阅用于自我管理部署的UNIX ulimit设置

MongoDB Atlas Connection LimitsMongoDB Atlas连接限制

MongoDB Atlas sets the limit for concurrent incoming connections based on the cluster tier and class. MongoDB Atlas根据集群层和类设置并发传入连接的限制。To learn more, see Connection Limits and Cluster Tier in the Atlas documentation.要了解更多信息,请参阅Atlas文档中的连接限制和群集层