Home Database Mysql Tutorial Mongo Database性能优化

Mongo Database性能优化

Jun 07, 2016 pm 02:53 PM
dat database mongo optimization performance

Mongo Database性能优化 SQL Server有工具进行数据库的优化,Mongo Database Profiler.不仅有,而且功能更强大。 MongoDB 自带 Profiler,可以非常方便地记录下所有耗时过长操作,以便于调优。有两种方式可以控制 Profiling 的开关和级别,第一种是直接在启


Mongo Database性能优化

 

SQL Server有工具进行数据库的优化,Mongo Database Profiler.不仅有,而且功能更强大。 

 

MongoDB 自带 Profiler,可以非常方便地记录下所有耗时过长操作,以便于调优。有两种方式可以控制 Profiling 的开关和级别,第一种是直接在启动参数里直接进行设置。 

 

启动MongoDB时加上–profile=级别 即可。 

 

也可以在客户端调用db.setProfilingLevel(级别) 命令来实时配置。可以通过db.getProfilingLevel()命令来获取当前的Profile级别。 

 

> db.setProfilingLevel(2); 

  www.2cto.com  

{"was" : 0 , "ok" : 1} 

 

> db.getProfilingLevel() 

 

上面斜体的级别可以取0,1,2 三个值,他们表示的意义如下: 

 

0 – 不开启,关闭性能分析,测试环境可以打开,生成环境关闭,对性能有很大影响 

 

1 – 记录慢命令 (默认为>100ms) 

 

2 – 记录所有命令 

 

Profile 记录在级别1时会记录慢命令,那么这个慢的定义是什么?上面我们说到其默认为100ms,当然有默认就有设置,其设置方法和级别一样有两种,一种是通过添加–slowms启动参数配置。第二种是调用db.setProfilingLevel时加上第二个参数: 

 

db.setProfilingLevel( level , slowms ) 

 

db.setProfilingLevel( 1 , 10 ); 

 

Profiler 信息保存在 system.profile (Capped Collection) 中。也可以通过这个工具进行设置和查看数据:强大的MongoDB数据库管理工具 

 

Mongo Shell 还提供了一个比较简洁的命令show profile,可列出最近5条执行时间超过1ms的 Profile 记录。 

 

查看当前库下所有集合的分析数据 

 

db.system.profile.find() 

查看某一个集合的分析数据 

 

db.system.profile.find({info:/user.info/}) 

查看执行时间大于100毫秒的执行操作,并倒序排列,并取前5行 

 

db.system.profile.find({millis:{$gt:100}}).sort({$natural:-1}).limit(5); 

 

Profile 信息内容详解: 

 

ts-该命令在何时执行.   www.2cto.com  

 

millis Time-该命令执行耗时,以毫秒记. 

 

info-本命令的详细信息. 

 

query-表明这是一个query查询操作. 

 

ntoreturn-本次查询客户端要求返回的记录数.比如, findOne()命令执行时 ntoreturn 为 1.有limit(n) 条件时ntoreturn为n. 

 

query-具体的查询条件(如x>3). 

 

nscanned-本次查询扫描的记录数. 

 

reslen-返回结果集的大小. 

 

nreturned-本次查询实际返回的结果集. 

 

update-表明这是一个update更新操作. 

 

fastmod-Indicates a fast modify operation. See Updates. These operations are normally quite fast.   www.2cto.com  

 

fastmodinsert – indicates a fast modify operation that performed an upsert. 

 

upsert-表明update的upsert参数为true.此参数的功能是如果update的记录不存在,则用update的条件insert一条记录. 

 

moved-表明本次update是否移动了硬盘上的数据,如果新记录比原记录短,通常不会移动当前记录,如果新记录比原记录长,那么可能会移动记录到其它位置,这时候会导致相关索引的更新.磁盘操作更多,加上索引更新,会使得这样的操作比较慢. 

 

insert-这是一个insert插入操作. 

getmore-这是一个getmore 操作,getmore通常发生在结果集比较大的查询时,第一个query返回了部分结果,后续的结果是通过getmore来获取的。 

 

2、优化 

  www.2cto.com  

MongoDB 查询优化 

 

如果nscanned(扫描的记录数)远大于nreturned(返回结果的记录数)的话,那么我们就要考虑通过加索引来优化记录定位了。 

 

reslen 如果过大,那么说明我们返回的结果集太大了,这时请查看find函数的第二个参数是否只写上了你需要的属性名。(类似于MySQL中不要总是select *) 

 

对于创建索引的建议是:如果很少读,那么尽量不要添加索引,因为索引越多,写操作会越慢。如果读量很大,那么创建索引还是比较划算的。 

 

MongoDB 更新优化 

 

如果写查询量或者update量过大的话,多加索引是会有好处的。以及~~~~(省略N字,和RDBMS差不多的道理) 

 

Use fast modify operations when possible (and usually with these, an index). See Updates. 

 

Profiler 的效率 

 

Profiling 功能肯定是会影响效率的,但是不太严重,原因是他使用的是system.profile 来记录,而system.profile 是一个capped collection 这种collection 在操作上有一些限制和特点,但是效率更高。   www.2cto.com  

 

优化建议: 

 

如果 nscanned 远大于 nreturned,那么需要使用索引。 

如果 reslen 返回字节非常大,那么考虑只获取所需的字段。 

执行 update 操作时同样检查一下 nscanned,并使用索引减少文档扫描数量。

 

使用 db.eval() 在服务端执行某些统计操作。 

减少返回文档数量,使用 skip & limit 分页。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Jun 15, 2024 pm 09:49 PM

IntelArrowLakeisexpectedtobebasedonthesameprocessorarchitectureasLunarLake,meaningthatIntel'sbrandnewLionCoveperformancecoreswillbecombinedwiththeeconomicalSkymontefficiencycores.WhileLunarLakeisonlyavailableasava

Performance comparison of different Java frameworks Performance comparison of different Java frameworks Jun 05, 2024 pm 07:14 PM

Performance comparison of different Java frameworks: REST API request processing: Vert.x is the best, with a request rate of 2 times SpringBoot and 3 times Dropwizard. Database query: SpringBoot's HibernateORM is better than Vert.x and Dropwizard's ORM. Caching operations: Vert.x's Hazelcast client is superior to SpringBoot and Dropwizard's caching mechanisms. Suitable framework: Choose according to application requirements. Vert.x is suitable for high-performance web services, SpringBoot is suitable for data-intensive applications, and Dropwizard is suitable for microservice architecture.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

C++ program optimization: time complexity reduction techniques C++ program optimization: time complexity reduction techniques Jun 01, 2024 am 11:19 AM

Time complexity measures the execution time of an algorithm relative to the size of the input. Tips for reducing the time complexity of C++ programs include: choosing appropriate containers (such as vector, list) to optimize data storage and management. Utilize efficient algorithms such as quick sort to reduce computation time. Eliminate multiple operations to reduce double counting. Use conditional branches to avoid unnecessary calculations. Optimize linear search by using faster algorithms such as binary search.

How to optimize the performance of multi-threaded programs in C++? How to optimize the performance of multi-threaded programs in C++? Jun 05, 2024 pm 02:04 PM

Effective techniques for optimizing C++ multi-threaded performance include limiting the number of threads to avoid resource contention. Use lightweight mutex locks to reduce contention. Optimize the scope of the lock and minimize the waiting time. Use lock-free data structures to improve concurrency. Avoid busy waiting and notify threads of resource availability through events.

What is the performance impact of converting PHP arrays to objects? What is the performance impact of converting PHP arrays to objects? Apr 30, 2024 am 08:39 AM

In PHP, the conversion of arrays to objects will have an impact on performance, mainly affected by factors such as array size, complexity, object class, etc. To optimize performance, consider using custom iterators, avoiding unnecessary conversions, batch converting arrays, and other techniques.

Performance comparison of C++ with other languages Performance comparison of C++ with other languages Jun 01, 2024 pm 10:04 PM

When developing high-performance applications, C++ outperforms other languages, especially in micro-benchmarks. In macro benchmarks, the convenience and optimization mechanisms of other languages ​​such as Java and C# may perform better. In practical cases, C++ performs well in image processing, numerical calculations and game development, and its direct control of memory management and hardware access brings obvious performance advantages.

What are some ways to resolve inefficiencies in PHP functions? What are some ways to resolve inefficiencies in PHP functions? May 02, 2024 pm 01:48 PM

Five ways to optimize PHP function efficiency: avoid unnecessary copying of variables. Use references to avoid variable copying. Avoid repeated function calls. Inline simple functions. Optimizing loops using arrays.

See all articles