What this article brings to you is a summary of methods to improve MongoDB performance. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
MongoDB is a high-performance data, but in the process of using it, everyone occasionally encounters some performance problems. MongoDB compared to other relational databases, such as SQL Server, MySQL, Oracle In comparison, it is relatively new and many people are not very familiar with it, so many developers and DBAs tend to focus on the realization of functions and ignore the performance requirements. In fact, MongoDB and SQL Like Server, MySQL, and Oracle, the design adjustment of a database object, the creation of indexes, and the optimization of statements will have a huge impact on performance.
In order to fully tap the performance of MongoDB, we have simply summarized the following 18 items. Everyone is welcome to continue to summarize and improve them.
(1) It is recommended to use the default value for the _id key in the document, and it is prohibited to save customized values to _id.
Interpretation: There will be a "_id" key in the MongoDB document, which defaults to an ObjectID object (the identifier includes timestamp, machine ID, process ID and counter). MongoDB has a big difference in insertion speed when specifying _id and not specifying _id. Specifying _id will slow down the insertion rate.
(2) It is recommended to use short field names.
Interpretation: Unlike relational databases, each document in the MongoDB collection needs to store a field name, and long field names will require more storage space.
(3) MongoDB index can improve document query, update, delete, and sort operations, so create an index appropriately based on business needs.
(4) Each index will occupy some space and cause resource consumption for the insertion operation. Therefore, it is recommended that the number of indexes in each collection be controlled within 5 as much as possible.
(5) For queries that contain multiple keys, creating a composite index containing these keys is a good solution. The key value order of a composite index is very important. Understand the leftmost prefix principle of the index.
Interpretation: For example, create a combined index {a:1,b:1,c:1} on the test collection. Execute the following 7 query statements:
db.test.find({a:”hello”}) // 1 db.test.find({b:”sogo”, a:”hello”}) // 2 db.test.find({a:”hello”,b:”sogo”, c:”666”}) // 3 db.test.find({c:”666”, a:”hello”}) // 4 db.test.find({b:”sogo”, c:”666”}) // 5 db.test.find({b:”sogo” }) // 6 db.test.find({c:”666”}) // 7
The above query statements may be indexed 1, 2, 3, 4
The query should include the leftmost index field, and the index creation order shall prevail. The order of query fields is irrelevant.
The least index covers the most queries.
(6) TTL index (time-to-live index, index with life cycle), using TTL index can age documents within the timeout period, and a document will be deleted after reaching the aging level.
Interpretation: The index used to create TTL must be of date type. TTL index is a single field index and cannot be a composite index. TTL delete document background thread removes invalid documents every 60 seconds. Fixed-length collections are not supported.
(7) When you need to create an index on a certain field in the collection, but a large number of documents in the collection do not contain this key value, it is recommended to create a sparse index.
Interpretation: The index is intensive by default, which means that even if the index field of the document is missing, there is a corresponding relationship in the index. In a sparse index, only documents containing index key values will appear.
(8) When creating a text index, the field specifies text instead of 1 or -1. There is only one text index per collection, but it can index as many fields as you like.
Interpretation: Text search is much faster. It is recommended to use text index to replace inefficient queries on multiple fields of collection documents.
(9) Use findOne to query matching multiple items in the database, and it will return the first item in the naturally sorted file collection. If you need to return multiple documents, use the find method.
(10) If the query does not need to return the entire document or is only used to determine whether a key value exists, you can limit the returned fields through projection (mapping) to reduce network traffic and client memory usage.
Interpretation: You can either explicitly specify the returned fields by setting {key:1}, or you can set {key:0} to specify the fields that need to be excluded.
(11) Except for prefix style queries, regular expression queries cannot use indexes and take longer to execute than most selectors. They should be used sparingly.
(12) In the aggregation operation, $ must be before match and $group. By prefixing $, you can reduce the prefix of match and reduce the number of documents to be processed by the $group operator.
(13) Modifying documents through operators can usually achieve better performance, because there is no need to go back and forth to the server to obtain and modify document data, and less time can be spent on serializing and transmitting data. time.
(14) Batch Insert (batchInsert) can reduce the number of data submissions to the server and improve performance. However, the BSON Size submitted in batches does not exceed 48MB.
(15) It is forbidden to retrieve too much data for sorting at one time. MongoDB currently supports sorting result sets within 32M. If sorting is required, try to limit the amount of data in the result set.
(16) Some $ operators in the query may cause low performance, such as $ne, $, not, $exists, $nin, $or try to use Do not use in business.
a) $exist: Because of the loose document structure, the query must traverse each document;
b) $ne: If the negated value is the majority, the entire index will be scanned ;
c) $not: may cause the query optimizer to not know which index should be used, so it will often degrade to a full table scan;
d) $nin: a full table scan;
e) \$If there are multiple conditions, it will be queried as many times as possible. When merging the result set, you should consider changing it to or: If there are multiple conditions, you will be querying how many times. When merging the result set, you should consider changing it to $in.
(17) Fixed collections can be used to record logs, which insert data faster and can eliminate the oldest data when inserting data. This feature can be considered during demand analysis and design, which improves performance and eliminates the need for deletion.
Interpretation: Fixed collections need to be created explicitly, specify the Size, and can also specify the number of documents. No matter which limit is reached first, new documents inserted later will remove the oldest document.
(18) The data volume of documents in the collection will affect query performance. To maintain an appropriate volume, regular archiving is required.
The above is the detailed content of Summary of MongoDB methods to improve performance. For more information, please follow other related articles on the PHP Chinese website!

mongodb php扩展没有的解决办法:1、在linux中执行“$ sudo pecl install mongo”命令来安装MongoDB的PHP扩展驱动;2、在window中,下载php mongodb驱动二进制包,然后在“php.ini”文件中配置“extension=php_mongo.dll”即可。

Redis和MongoDB都是流行的开源NoSQL数据库,但它们的设计理念和使用场景有所不同。本文将重点介绍Redis和MongoDB的区别和使用场景。Redis和MongoDB简介Redis是一个高性能的数据存储系统,常被用作缓存和消息中间件。Redis以内存为主要存储介质,但它也支持将数据持久化到磁盘上。Redis是一款键值数据库,它支持多种数据结构(例

MongoDB是一种高性能、开源、文档型的NoSQL数据库,被广泛应用于Web应用、大数据以及云计算领域。而Go语言则是一种快速、开发效率高、代码可维护性强的编程语言。本文将为您完整介绍如何在Go语言中使用MongoDB。一、安装MongoDB在使用MongoDB之前,需要先在您的系统中安装MongoDB。在Linux系统下,可以通过如下命令安装:sudo

php7.0安装mongo扩展的方法:1、创建mongodb用户组和用户;2、下载mongodb源码包,并将源码包放到“/usr/local/src/”目录下;3、进入“src/”目录;4、解压源码包;5、创建mongodb文件目录;6、将文件复制到“mongodb/”目录;7、创建mongodb配置文件并修改配置即可。

MongoDB作为一款流行的NoSQL数据库,已经被广泛应用于各种大型Web应用和企业级应用中。而PHP语言也作为一种流行的Web编程语言,与MongoDB的结合也变得越来越重要。在本文中,我们将会学习如何使用PHP语言操作MongoDB数据库进行增删查改的操作。

一、什么是MongoDBMongoDB与我们之前熟知的关系型数据库(MySQL、Oracle)不同,MongoDB是一个文档数据库,它具有所需的可伸缩性和灵活性,以及所需的查询和索引。MongoDB将数据存储在灵活的、类似JSON的文档中,这意味着文档的字段可能因文档而异,数据结构也会随着时间的推移而改变。文档模型映射到应用程序代码中的对象,使数据易于处理。MongoDB是一个以分布式数据库为核心的数据库,因此高可用性、横向扩展和地理分布是内置的,并且易于使用。况且,MongoDB是免费的,开源

自定义Appender非常简单,继承一下AppenderBase类即可。可以看到有个AppenderBase,有个UnsynchronizedAppenderBase,还有个AsyncAppenderBase继承了UnsynchronizedAppenderBase。从名字就能看出来区别,异步的、普通的、不加锁的。我们定义一个MongoDBAppender继承UnsynchronizedAppenderBasepublicclassMongoDBAppenderextendsUnsynchron

随着互联网技术的不断发展,大数据成为企业发展的重要资产。而对于企业来说,数据的可用性和安全性至关重要。MongoDB是一个高性能、高可用性的NoSQL数据库,越来越受到企业的青睐。然而,MongoDB的可用性也是企业关注的焦点之一,本文将介绍PHP实现MongoDB数据库可用性的方法。一、了解MongoDB的高可用性特性MongoDB作为NoSQL数据库,具


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
