mongodb事务解决方案_7个适用于MongoDB的简单速度解决方案

本文提供了七个步骤来解决MongoDB的性能问题,包括检查MongoDB日志、分析查询、添加索引、谨慎排序、创建多个连接对象、设置最大执行时间和重建索引。这些技巧有助于优化查询效率,防止应用程序变得不稳定。
摘要由CSDN通过智能技术生成

mongodb事务解决方案

MongoDB is a fast NoSQL database. Unfortunately, it’s not a cure for all your performance woes, and a single complex query can bring your code grinding to a halt. I recently suffered this fate, and it can be difficult to know where to look when your application suddenly becomes unstable. I hope these tips help you avoid the pain I went through!

MongoDB是一个快速的NoSQL数据库 。 不幸的是,这不能解决所有性能问题,并且单个复杂的查询可能会使您的代码停止工作。 我最近遭受了这种命运,当您的应用程序突然变得不稳定时,可能很难知道在哪里看。 希望这些技巧能帮助您避免我所经历的痛苦!

1.检查您的MongoDB日志 (1. Check Your MongoDB Log)

By default, MongoDB records all queries which take longer than 100 milliseconds. Its location is defined in your configuration’s systemLog.path setting, and it’s normally /var/log/mongodb/mongod.log in Debian-based distributions such as Ubuntu.

默认情况下,MongoDB记录所有耗时超过100毫秒的查询。 它的位置在配置的systemLog.path设置中定义,在基于Debian的发行版(如Ubuntu)中通常为/var/log/mongodb/mongod.log

The log file can be large, so you may want to clear it before profiling. From the mongo command-line console, enter:

日志文件可能很大,因此您可能需要在分析之前将其清除。 在mongo命令行控制台中,输入:

use admin;
db.runCommand({ logRotate : 1 });

A new log file will be started and the old data will be available in a file named with the backup date and time. You can delete the backup or move it elsewhere for further analysis.

将启动一个新的日志文件,而旧数据将在一个名为备份日期和时间的文件中可用。 您可以删除备份或将其移至其他位置以进行进一步分析。

It can also be useful to watch the log while users are accessing your system. For example:

在用户访问系统时查看日志也很有用。 例如:

tail -f /var/log/mongodb/mongod.log

The defaults are reasonable, but you can configure the log level verbosity or modify profiling parameters and change the query time to something other than 100 milliseconds. You could initially set it to one second to catch the worst offending queries, then halve it after every set of successful fixes.

默认值是合理的,但是您可以配置日志级别的详细程度或修改配置参数 ,并将查询时间更改为100毫秒以外的其他值。 最初,您可以将其设置为一秒钟以捕获最恶劣的查询,然后在每组成功修复后将其减半。

Look out for lines containing ‘COMMAND’ with the execution time in milliseconds at the end. For example:

查找包含“ COMMAND”的行,其执行时间以毫秒为结尾。 例如:

2016-02-12T11:05:08.161+0000 I COMMAND  
    [conn563] command project.$cmd 
    command: count { 
        count: "test", 
        query: { published: { $ne: false }, 
        country: "uk" } 
    } 
    planSummary: IXSCAN { country: 1 } 
    keyUpdates:0 
    writeConflicts:0 
    numYields:31 
    reslen:44 
    locks: { 
        Global: { 
            acquireCount: { r: 64 } 
        }, 
        MMAPV1Journal: { 
            acquireCount: { r: 32 } 
        }, 
        Database: { 
            acquireCount: { r: 32 } 
        }, 
        Collection: { 
            acquireCount: { R: 32 } 
        } 
    } 403ms

This will help you determine where potential bottlenecks lie.

这将帮助您确定潜在的瓶颈所在。

2.分析您的查询 (2. Analyze Your Queries)

Like many databases, MongoDB provides an explain facility which reveals how a database operation worked. You can add explain('executionStats') to a query. For example:

与许多数据库一样,MongoDB提供了一个explain工具 ,该工具可以揭示数据库操作的工作方式。 您可以在查询中添加explain('executionStats') 。 例如:

db.user.find(
  { country: 'AU', city: 'Melbourne' }
).explain('executionStats');

or append it to the collection:

或将其附加到集合中:

db.user.explain('executionStats').find(
  { country: 'AU', city: 'Melbourne' }
);

This returns a large JSON result, but there are two primary values to examine:

这将返回较大的JSON结果,但是有两个主要值需要检查:

  • executionStats.nReturned — the number of documents returned, and

    executionStats.nReturned —返回的文档数,以及

  • executionStats.totalDocsExamined — the number of documents scanned to find the result.

    executionStats.totalDocsExamined —扫描以查找结果的文档数。

If the number of documents examined greatly exceeds the number returned, the query may not be efficient. In the worst cases, MongoDB might have to scan every document in the collection. The query would therefore benefit from the use of an index.

如果检查的文档数大大超过了返回的数,则查询可能无效。 在最坏的情况下,MongoDB可能必须扫描集合中的每个文档。 因此,查询将受益于索引的使用。

For more information and examples, refer to Analyze Query Performance and db.collection.explain() in the MongoDB manual.

有关更多信息和示例,请参阅MongoDB手册中的分析查询性能db.collection.explain()

3.添加适当的索引 (3. Add Appropriate Indexes)

NoSQL databases require indexes, just like their relational cousins. An index is built from a set of one or more fields to make querying fast. For example, you could index the country field in a user collection. When a query searches for ‘AU’, MongoDB can find it in the index and reference all matching documents without having to scan the entire user collection.

NoSQL数据库需要索引,就像它们的关系表亲一样。 索引是根据一组一个或多个字段构建的,以加快查询速度。 例如,您可以为user集合中的country字段编制索引。 当查询搜索“ AU”时,MongoDB可以在索引中找到它并引用所有匹配的文档,而不必扫描整个user集合。

Indexes are created with createIndex. The most basic command to index the country field in the user collection in ascending order:

索引是使用createIndex创建的。 最基本的命令以升序索引user集合中的country字段:

db.user.createIndex({ country: 1 });

The majority of your indexes are likely to be single fields, but you can also create compound indexes on two or more fields. For example:

您的大多数索引可能是单个字段,但是您也可以在两个或多个字段上创建复合索引。 例如:

db.user.createIndex({ country: 1, city: 1 });

There are many indexing options, so refer to the MongoDB manual Index Introduction for more information.

索引选项很多,因此请参阅MongoDB手册“ 索引简介”以获取更多信息。

4.整理时要小心 (4. Be Wary When Sorting)

You almost certainly want to sort results, e.g. return all users in ascending country-code order:

您几乎肯定要对结果进行排序,例如以升序的国家(地区)代码返回所有用户:

db.user.find().sort({ country: 1 });

Sorting works effectively when you have an index defined. Either the single or compound index defined above would be suitable.

定义索引后,排序有效。 以上定义的单一或复合索引将是合适的。

If you don’t have an index defined, MongoDB must sort the result itself, and this can be problematic when analyzing a large set of returned documents. The database imposes a 32MB memory limit on sorting operations and, in my experience, 1,000 relatively small documents is enough to push it over the edge. MongoDB won’t necessarily return an error — just an empty set of records.

如果您没有定义索引,则MongoDB必须对结果本身进行排序,这在分析大量返回的文档时可能会出现问题。 数据库对排序操作施加了32MB的内存限制 ,以我的经验,有1000个相对较小的文档足以将其推向边缘。 MongoDB不一定会返回错误-只是一个空的记录集。

The sorting limit can strike in unexpected ways. Presume you have an index on the country code like before:

排序限制可能会以意外的方式出现。 假设您像以前一样在country代码上有一个索引:

db.user.createIndex({ country: 1 });

A query now sorts on the country and city both in ascending order:

现在,查询将按升序对countrycity进行排序:

db.user.find().sort({ country: 1, city: 1 });

While the country index can be used, MongoDB must still sort by the secondary city field itself. This is slow, and could exceed the 32MB sorting memory limit. You should therefore create a compound index:

尽管可以使用country索引,但MongoDB仍必须按二级city字段本身进行排序。 这很慢,并且可能超过32MB的排序内存限制。 因此,您应该创建一个复合索引:

db.user.createIndex({ country: 1, city: 1 });

The sort operation is now fully indexed and will run quickly. You can also sort in reverse country and city order because MongoDB can start at the end of the index and work backward. For example:

排序操作现已完全索引,并且将快速运行。 您还可以按相反的countrycity顺序进行排序,因为MongoDB可以从索引的末尾开始并向后工作。 例如:

db.user.find().sort({ country: -1, city: -1 });

However, problems arise if you attempt to sort in descending country order but ascending city order:

但是,如果您尝试按降序排列但升序排列,则会出现问题:

db.user.find().sort({ country: -1, city: 1 });

Our index cannot be used, so you must either disallow non-indexed secondary sorting criteria or create another suitable index:

我们的索引无法使用,因此您必须禁止未建立索引的二级排序条件或创建另一个合适的索引:

db.user.createIndex({ country: -1, city: 1 });

Again, this could also be used for queries which reversed the order:

同样,这也可以用于颠倒顺序的查询:

db.user.find().sort({ country: 1, city: -1 });

5.创建两个或更多连接对象 (5. Create Two or More Connection Objects)

When building an application, you can increase efficiency with a single persistent database connection object which is reused for all queries and updates.

在构建应用程序时,可以使用单个持久数据库连接对象提高效率,该对象可用于所有查询和更新。

MongoDB runs all commands in the order it receives them from each client connection. While your application may make asynchronous calls to the database, every command is synchronously queued and must complete before the next can be processed. If you have a complex query which takes ten seconds to run, no one else can interact your application at the same time on the same connection.

MongoDB按照从每个客户端连接接收命令的顺序运行所有命令。 尽管您的应用程序可能会异步调用数据库,但是每个命令都将同步排队,并且必须先完成,然后才能处理下一个命令。 如果您有一个复杂的查询需要十秒钟才能运行,则没有其他人可以在同一连接上同时交互您的应用程序。

Performance can be improved by defining more than one database connection object. For example:

通过定义多个数据库连接对象可以提高性能。 例如:

  1. one to handle the majority of fast queries

    一个可以处理大多数快速查询
  2. one to handle slower document inserts and updates

    一种处理较慢的文档插入和更新的方法
  3. one to handle complex report generation.

    一个处理复杂的报告生成。

Each object is treated as a separate database client and will not delay the processing of others. The application should remain responsive.

每个对象都被视为一个单独的数据库客户端,不会延迟其他对象的处理。 应用程序应保持响应状态。

6.设置最大执行时间 (6. Set Maximum Execution Times)

MongoDB commands run as long as they need. A slowly-executing query can hold up others, and your web application may eventually time out. This can throw various strange instability problems in Node.js programs, which happily continue to wait for an asynchronous callback.

MongoDB命令可以根据需要运行。 执行缓慢的查询可能会阻止其他查询,并且您的Web应用程序最终可能会超时。 这可能会在Node.js程序中引发各种奇怪的不稳定问题,这些问题很乐意继续等待异步回调。

You can specify a time limit in milliseconds using maxTimeMS(): for example, permit 100 milliseconds (one tenth of a second) to query documents in the user collection where the city fields starting with the letter ‘A’:

您可以使用maxTimeMS()以毫秒为单位指定时间限制:例如,允许100毫秒(十分之一秒)来查询user集合中以字母“ A”开头的city字段的文档:

db.user.find({ city: /^A.+/i }).maxTimeMS(100);

You should set a reasonable maxTimeMS value for any command which is likely to take considerable time. Unfortunately, MongoDB doesn’t allow you to define a global timeout value, and it must be set for individual queries (although some libraries may apply a default automatically).

您应该为任何可能花费大量时间的命令设置合理的maxTimeMS值。 不幸的是,MongoDB不允许您定义全局超时值,并且必须为单个查询设置它(尽管某些库可能会自动应用默认值)。

7.重建索引 (7. Rebuild Your Indexes)

If you’re satisfied your structure is efficient yet queries are still running slowly, you could try rebuilding indexes on each collection. For example, rebuild the user collection indexes from the mongo command line:

如果您满意您的结构高效但查询仍然运行缓慢,则可以尝试在每个集合上重建索引 。 例如,从mongo命令行重建user集合索引:

db.user.reIndex();

If all else fails, you could consider a database repair to find and fix any problems. This should be considered a last resort when all other options have been exhausted. I’d recommend a full backup using mongodump or another appropriate method before progressing.

如果所有其他方法均失败,则可以考虑进行数据库修复以查找和修复任何问题。 当所有其他选项都用尽时,这应该被视为最后的选择。 我建议您在进行升级之前使用mongodump或其他适当的方法进行完整备份。

May all your MongoDB queries remain fast and efficient! Please let me know if you have further performance tips.

愿您所有的MongoDB查询保持快速高效! 如果您还有其他性能提示,请告诉我。

翻译自: https://www.sitepoint.com/7-simple-speed-solutions-mongodb/

mongodb事务解决方案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值