search
HomeDatabaseMysql Tutorial【MongoDB】MongoDB之八大优化技巧

技巧一、尽量减少磁盘访问 内存访问要比磁盘访问快得多。所以使用优化的本质就是尽可能地减少对磁盘的访问。 内存的读取速度要比磁盘速度快一百万倍。读磁盘要消耗很长时间。几种简单的办法:使用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
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
How does MySQL handle concurrency compared to other RDBMS?How does MySQL handle concurrency compared to other RDBMS?Apr 29, 2025 am 12:44 AM

MySQLhandlesconcurrencyusingamixofrow-levelandtable-levellocking,primarilythroughInnoDB'srow-levellocking.ComparedtootherRDBMS,MySQL'sapproachisefficientformanyusecasesbutmayfacechallengeswithdeadlocksandlacksadvancedfeatureslikePostgreSQL'sSerializa

How does MySQL handle transactions compared to other relational databases?How does MySQL handle transactions compared to other relational databases?Apr 29, 2025 am 12:37 AM

MySQLhandlestransactionseffectivelyusingtheInnoDBengine,supportingACIDpropertiessimilartoPostgreSQLandOracle.1)MySQLusesREPEATABLEREADasthedefaultisolationlevel,whichcanbeadjustedtoREADCOMMITTEDforhigh-trafficscenarios.2)Itoptimizesperformancewithabu

What are the data types available in MySQL?What are the data types available in MySQL?Apr 29, 2025 am 12:28 AM

MySQL data types are divided into numerical, date and time, string, binary and spatial types. Selecting the correct type can optimize database performance and data storage.

What are some best practices for writing efficient SQL queries in MySQL?What are some best practices for writing efficient SQL queries in MySQL?Apr 29, 2025 am 12:24 AM

Best practices include: 1) Understanding the data structure and MySQL processing methods, 2) Appropriate indexing, 3) Avoid SELECT*, 4) Using appropriate JOIN types, 5) Use subqueries with caution, 6) Analyzing queries with EXPLAIN, 7) Consider the impact of queries on server resources, 8) Maintain the database regularly. These practices can make MySQL queries not only fast, but also maintainability, scalability and resource efficiency.

How does MySQL differ from PostgreSQL?How does MySQL differ from PostgreSQL?Apr 29, 2025 am 12:23 AM

MySQLisbetterforspeedandsimplicity,suitableforwebapplications;PostgreSQLexcelsincomplexdatascenarioswithrobustfeatures.MySQLisidealforquickprojectsandread-heavytasks,whilePostgreSQLispreferredforapplicationsrequiringstrictdataintegrityandadvancedSQLf

How does MySQL handle data replication?How does MySQL handle data replication?Apr 28, 2025 am 12:25 AM

MySQL processes data replication through three modes: asynchronous, semi-synchronous and group replication. 1) Asynchronous replication performance is high but data may be lost. 2) Semi-synchronous replication improves data security but increases latency. 3) Group replication supports multi-master replication and failover, suitable for high availability requirements.

How can you use the EXPLAIN statement to analyze query performance?How can you use the EXPLAIN statement to analyze query performance?Apr 28, 2025 am 12:24 AM

The EXPLAIN statement can be used to analyze and improve SQL query performance. 1. Execute the EXPLAIN statement to view the query plan. 2. Analyze the output results, pay attention to access type, index usage and JOIN order. 3. Create or adjust indexes based on the analysis results, optimize JOIN operations, and avoid full table scanning to improve query efficiency.

How do you back up and restore a MySQL database?How do you back up and restore a MySQL database?Apr 28, 2025 am 12:23 AM

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

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.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.