如何调试 MongoDB 性能查询

How to debug MongoDB queries for performance

我有一个小应用程序,我使用 MongoDb (Mongoid)。该集合相对较小(大约 300 条记录),但是查询需要很长时间(大约 12 秒)。这是我的查询:

query = Lead.order(:post_date.desc).cache
query = query.page(params['page_number']).cache if params['page_number']
query = query.per(params['page_size']).cache if params['page_size']
query = query.where(read: params['read']).cache if params['read']
query = query.where(important: @user.id.to_s).cache if params['important'] == "true"
query = query.where(starred: @user.id.to_s).cache if params['starred'] == "true"

我在模型上设置了 fowling 索引:

index({ post_date: 1 }, { background: true })
index({ read: 1 }, { background: true })
index({ important: 1 }, { background: true })
index({ starred: 1 }, { background: true })

所以我的问题是,1) 我怎样才能提高这个查询的性能,以及 2) 有没有一种工具(比如 NewRelic,尽管 NewRelic 没有提供有关如何提高查询性能的详细信息)或可以监控的服务查询和改进建议?

提前致谢!

CloudManager 是一个付费 GUI,它提供(除其他外)性能分析 "like NewRelic".

A built-in profiler 默认记录慢速查询(> 100 毫秒),但可以打开(谨慎)以记录所有内容。

.explain() 用于分析特定查询的性能。

Compas 是具有一些性能见解的官方 GUI。

更多监控工具:https://docs.mongodb.com/manual/administration/monitoring/