【MongoDB】MongoDB之优化器Profiler
在mysql数据库中,慢查询日志经常作为优化数据库的依据, mongodb中依然有类似的功能。Mongodb自带的profiler,可以方便地记录所有耗时的操作,以便于调优; 一、开始profiler功能 开启profier功能有两种: 第一种就是直接在启动参数里面进行设置,就在茄冬m
在mysql数据库中,慢查询日志经常作为优化数据库的依据, mongodb中依然有类似的功能。Mongodb自带的profiler,可以方便地记录所有耗时的操作,以便于调优;
一、开始profiler功能
开启profier功能有两种:
第一种就是直接在启动参数里面进行设置,就在茄冬mongodb时候添加-profile=级别
第二种就是在客户端执行“db.setProfilingLevel(级别)”命令
profiler信息保存在system.profile表中,可以通过执行“db.getProfilingLevel()”命令来获取当前profiler级别来:
在上图可以看到,level总共有三个参数,0是关闭,1是慢查询,2是所有的。如果设置为2表示所有的语句都会记录到log中。慢查询的默认时间是100ms,当然也可以通过参数slowsms进行设置。
二、查询profiler记录
mysql慢查询日志是存储在磁盘上,而mongodb profiler记录直接存在系统的db中。记录到system.profile中。profile就是前面讲过的capped collection集合。所以只要查询这个collection的记录就可以获取profiler记录的日志,可以使用db.system.profile.find()的命令直接查找执行时间大于某一限度的(如5ms)的profiler日志;
开启设置为100ms
<pre name="code" class="html">> db.students.find({'comment.aihao':'reading'}).limit(1) { "_id" : ObjectId("5485c80bdf41c6670aa8d51c"), "name" : "albert", "age" : 12, "comment" : { "aihao" : [ "basket", "reading" ], "relatives" : [ "parent", "brother" ] } } > db.system.profile.find().sort({$natural:-1}).limit(1) { "op" : "query", "ns" : "test.system.indexes", "query" : { "expireAfterSeconds" : { "$exists" : true } }, "ntoreturn" : 0, "ntoskip" : 0, "nscanned" : 7, "nscannedObjects" : 7, "keyUpdates" : 0, "numYield" : 0, "lockStats" : { "timeLockedMicros" : { "r" : NumberLong(159), "w" : NumberLong(0) }, "timeAcquiringMicros" : { "r" : NumberLong(4), "w" : NumberLong(6) } }, "nreturned" : 0, "responseLength" : 20, "millis" : 0, "execStats" : { "type" : "COLLSCAN", "works" : 9, "yields" : 0, "unyields" : 0, "invalidates" : 0, "advanced" : 0, "needTime" : 8, "needFetch" : 0, "isEOF" : 1, "docsTested" : 7, "children" : [ ] }, "ts" : ISODate("2014-12-08T15:54:47.377Z"), "client" : "0.0.0.0", "allUsers" : [ { "user" : "__system", "db" : "local" } ], "user" : "__system@local" }
通过执行上面语句,可以看出在system.profile中记录了详细的查询信息。主要字段说明:
1: ts 命令在何时执行
2: info 命令的详细信息
3:reslen: 返回结果集的大小
4: nscanned:本次查询扫描的记录数
5: nreturned: 本次查询实际返回的结果集
6: mills:该命令的执行耗时(单位:毫秒)
可以利用命令查询特定条件的值:例如:查询执行时间大于4ms的查询语句:

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



不同数据库系统添加列的语法为:MySQL:ALTER TABLE table_name ADD column_name data_type;PostgreSQL:ALTER TABLE table_name ADD COLUMN column_name data_type;Oracle:ALTER TABLE table_name ADD (column_name data_type);SQL Server:ALTER TABLE table_name ADD column_name data_

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

Building an SQL database involves 10 steps: selecting DBMS; installing DBMS; creating a database; creating a table; inserting data; retrieving data; updating data; deleting data; managing users; backing up the database.

SQL (Structured Query Language) is a programming language used to create, manage, and query databases. The main functions include: creating databases and tables, inserting, updating and deleting data, sorting and filtering results, aggregating functions, joining tables, subqueries, operators, functions, keywords, data manipulation/definition/control language, connection types, query optimization, security, tools, resources, versions, common errors, debugging techniques, best practices, trends and row locking.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Importing SQL files allows you to load SQL statements from text files into a database. The steps include: Connect to the database. Prepare the SQL file to make sure it is valid and the statement ends in a semicolon. Import files via the command line or database client tools. Query the database verification import results.

In SQL, the steps to insert new columns gracefully: Create a new table with new columns and original columns, and formulate the column order as needed. Insert the old table data into the new table and specify the default value for the new column. Delete the old table and rename the new table to the original table name.

Adding columns at the end of a database table is not easy, the specific operation depends on the database system, table size and data volume. Common errors include: ignoring data types, incorrectly using indexes, and concurrent operations. Optimization strategies include: selecting the appropriate storage engine, using partition tables, and utilizing database replication technology. Good code readability and maintainability also help avoid problems. Only by operating with caution and paying attention to the underlying mechanism can we avoid risks in data security and integrity.
