Table of Contents
技巧一、尽量减少磁盘访问
技巧二、使用索引减少内存占用
技巧三、不要到处使用索引
技巧四、索引覆盖查询
技巧五、使用复合索引加快多个查询
技巧六、通过建立分级文档加快扫描
技巧七、AND型查询要点
技巧八:OR型查询要点
Home Database Mysql Tutorial 【MongoDB】MongoDB之八大优化技巧

【MongoDB】MongoDB之八大优化技巧

Jun 07, 2016 pm 04:10 PM
mongodb optimization reduce Skill disk

技巧一、尽量减少磁盘访问 内存访问要比磁盘访问快得多。所以使用优化的本质就是尽可能地减少对磁盘的访问。 内存的读取速度要比磁盘速度快一百万倍。读磁盘要消耗很长时间。几种简单的办法:使用SSD(固态硬盘);增加内存可以减少对硬盘的读取,让常用的文

技巧一、尽量减少磁盘访问

内存访问要比磁盘访问快得多。所以使用优化的本质就是尽可能地减少对磁盘的访问。
内存的读取速度要比磁盘速度快一百万倍。读磁盘要消耗很长时间。几种简单的办法: 使用SSD(固态硬盘);增加内存可以减少对硬盘的读取,让常用的文档在内存中;

技巧二、使用索引减少内存占用

索引是有序的,所以不必遍历全部项。当查询时候会先根据索引查到索引中的结果,然后符合条件的文档加载到内存中,从而减少内存占用。

技巧三、不要到处使用索引

这里要注意,不是所有的查询都可以用索引的。索引一般用在返回结果只是总体数据的一小部分的时候。根据经验,一旦要大约返回集合一半的数据就不要使用索引了。
若是已经对某个字段建立了索引,又想在大规模模糊查询时候不使用它(因为使用时候可能较低效)可以使用自然排序,用{“$natural”:1}来强制mongodb禁用索引。自然排序就是“按照磁盘上的存储顺序返回数据”,这样mongodb就不会使用索引了。

技巧四、索引覆盖查询

如果只想返回某些字段且所有这些字段都可放在索引中,mong odb可以做索引覆盖查询(covered index query),这种查询不会访问指针指向的文档,二是直接用索引的数据返回结果。
例如: db.foo.ensuIndex({x:1, y :1,z:1}) 现在查询被索引的字段,并只要求返回这些字段,mongodb就没必要加载整个文档。 db,foo.find({x: ceriteria, y: ceriteria})
这样的查询仅仅访问了索引的数据,而没有访问整个集合的数据;

技巧五、使用复合索引加快多个查询

查询只要和索引开头部分匹配就能利用索引,所以创建索引时要考虑这些查询依赖的所有字段。并且根据各个字段查询的频率定义索引字段顺序;

技巧六、通过建立分级文档加快扫描

将数组组织有层次话,不仅可以让其看着更有条理,还可以让mongodb在偶尔没有索引时候也能快速查询。 如果文档没有层次结构的话,mongodb必须遍历文档中的每个字段。合理使用层次可以减少mongodb对字段的访问。

技巧七、AND型查询要点

假设要查询满足条件A,B和C的文档,满足A的文档有4万,满足B的有9K,满足C的是200,那么应该用C and B and A 这样只需要查询200条记录。
这就是说要是已知某个查询条件更加苛刻,那要将放置在最前面;

技巧八:OR型查询要点

OR型查询与AND查询恰好相反,匹配最多的查询语句放在最前面,因为Mongodb每次都要匹配不在结果集中的文档。







Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

'Black Myth: Wukong ' Xbox version was delayed due to 'memory leak', PS5 version optimization is in progress 'Black Myth: Wukong ' Xbox version was delayed due to 'memory leak', PS5 version optimization is in progress Aug 27, 2024 pm 03:38 PM

Recently, "Black Myth: Wukong" has attracted huge attention around the world. The number of people online at the same time on each platform has reached a new high. This game has achieved great commercial success on multiple platforms. The Xbox version of "Black Myth: Wukong" has been postponed. Although "Black Myth: Wukong" has been released on PC and PS5 platforms, there has been no definite news about its Xbox version. It is understood that the official has confirmed that "Black Myth: Wukong" will be launched on the Xbox platform. However, the specific launch date has not yet been announced. It was recently reported that the Xbox version's delay was due to technical issues. According to a relevant blogger, he learned from communications with developers and "Xbox insiders" during Gamescom that the Xbox version of "Black Myth: Wukong" exists.

How to configure MongoDB automatic expansion on Debian How to configure MongoDB automatic expansion on Debian Apr 02, 2025 am 07:36 AM

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

Soland's feeding-level hair-raising airdrop tutorial: 0 cost, no routines, no counter-playing! Soland's feeding-level hair-raising airdrop tutorial: 0 cost, no routines, no counter-playing! Jun 25, 2024 pm 07:13 PM

Soland's feeding-level lubbing tutorial: 0 cost, no routines, no feedback! Recently, hair removal has once again become a topic of discussion among many friends in the currency circle. Of course, this time everyone is generally disappointed or even angry. The reason is for no other reason. First, there was the tug-of-war of Layerzero’s witch screening batch screening, and then the PUA king ZKsync’s Airdrops have caused many people to complain that they were severely criticized, and some even spent tens of thousands of dollars in gas and interaction costs, and the final benefit was less than one-tenth, not counting the time spent and other costs. The two "King of Heaven" level projects have made many people want to quit the terry circle. In fact, we think it is not necessary here, because there are still many opportunities in the encrypted track. Regardless of the scale, project parties have a need to promote it.

How to ensure high availability of MongoDB on Debian How to ensure high availability of MongoDB on Debian Apr 02, 2025 am 07:21 AM

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

What is the CentOS MongoDB backup strategy? What is the CentOS MongoDB backup strategy? Apr 14, 2025 pm 04:51 PM

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

Major update of Pi Coin: Pi Bank is coming! Major update of Pi Coin: Pi Bank is coming! Mar 03, 2025 pm 06:18 PM

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

See all articles