简体   繁体   English

如何调试MongoDB查询以提高性能

[英]How to debug MongoDB queries for performance

I have a small app and I use MongoDb (Mongoid). 我有一个小应用程序,并且使用MongoDb(Mongoid)。 The collection is relatively small (about 300 records), however the query is taking a long time (approximately 12s). 集合相对较小(大约300条记录),但是查询花费的时间很长(大约12s)。 Here's my query: 这是我的查询:

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"

And I have the fowling indexing setup on the Model: 我在模型上有笨拙的索引设置:

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

So my question is, 1) how can I improve the performance of this query and 2) is there a tool (like NewRelic although NewRelic doesn't provide details on how improve performance for queries) or service that can monitor the queries and suggest improvements? 所以我的问题是,1)如何改善此查询的性能; 2)有没有可以监视查询并提出改进建议的工具(如NewRelic,尽管NewRelic并未提供有关如何提高查询性能的详细信息) ?

Thanks in advance! 提前致谢!

CloudManager is a paid-for GUI which provides (amongst others) performance analysis "like NewRelic". CloudManager是付费的GUI,它(其中包括)提供“像NewRelic”一样的性能分析。

A built-in profiler logs slow queries (> 100ms) by default, but can be turned on (with caution) to log everything. 内置的探查器默认情况下会记录慢速查询(> 100毫秒),但可以打开(谨慎)以记录所有内容。

An .explain() to analyse performance of a particular query. .explain()分析特定查询的性能。

Compas is an official GUI with some performance insights. Compas是具有一些性能见解的官方GUI。

More monitoring tools: https://docs.mongodb.com/manual/administration/monitoring/ 更多监视工具: https : //docs.mongodb.com/manual/administration/monitoring/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM