MongoDB operation manual CRUD query performance analysis, mongodbcrud

Source: Internet
Author: User

MongoDB operation manual CRUD query performance analysis, mongodbcrud
The explain () cursor Method for Analyzing query performance allows you to observe the operations performed by the query system. This method is very useful for analyzing efficient queries and deciding how to use indexes for queries. This method detects the query operation, rather than the query execution time. Because this method attempts multiple query plans, it cannot accurately reflect the query execution time.
To evaluate the performance of a query, use the explain () method to call the pointer returned by find.
Example:
Create an index in the type field
Db. testData. ensureIndex ({'type': 1 });
Evaluate a query in the type field.
Db. testData. find ({type: 'food'}). explain ()
The result is as follows:
{
"Cursor": "BtreeCursor type_1 ",
"IsMultiKey": false,
"N": 3,
"NscannedObjects": 3,
"Nscanned": 3,
"NscannedObjectsAllPlans": 3,
"NscannedAllPlans": 3,
"ScanAndOrder": false,
"IndexOnly": false,
"NYields": 1,
"NChunkSkips": 0,
"Millis": 64,
"IndexBounds ":{
"Type ":[
[
"Food ",
"Food"
]
]
},
"Server": "TT: 27017 ",
"FilterSet": false
}
The cursor value is BtreeCursor. The table name query uses an index.
N = 3 records are returned for query.
In order to return these five records, the query scans the nscanned = 3 Records, and then reads the nscannedObjects = 3 complete records. If no index is found, all records are scanned.
Compare index query performance: manually compare a query that uses multiple fields. You can use the hint () and explain () methods in combination.
For example, the index of different fields is evaluated.
Db. testData. find ({type: 'food'}). hint ({type: 1}). explain ();
Result:
{
"Cursor": "BtreeCursor type_1 ",
"IsMultiKey": false,
"N": 3,
"NscannedObjects": 3,
"Nscanned": 3,
"NscannedObjectsAllPlans": 3,
"NscannedAllPlans": 3,
"ScanAndOrder": false,
"IndexOnly": false,
"NYields": 0,
"NChunkSkips": 0,
"Millis": 40,
"IndexBounds ":{
"Type ":[
[
"Food ",
"Food"
]
]
},
"Server": "TT: 27017 ",
"FilterSet": false
}
Db. testData. find ({type: 'food'}). hint ({type: 1, name: 1}). explain ();
// This statement is not executed successfully and will be resolved
The returned statistical results ignore the queries executed using their respective indexes.
Note: If the explain () method is not applicable to hint (), the query optimizer re-evaluates the query and runs the multi-index query before returning the query statistics.
For more detailed explain output, see explain-results.
Will XP make full use of hardware than 98, so that the game runs smoothly?

As a system that has served for more than ten years, it has ushered in its own home. Now, netizens around the world can't help but respect this tenacious system that exists in microsoft for more than 10 years. Only by constantly exploring, trying, and innovating can we make the system operation more humane. This is not comparable to XP 7 and 8.1.

Why does MongoDB replace MySQL?

MongoDB is a document-oriented database, which is currently developed and maintained by 10gen. It has rich functions and is complete and can completely replace MySQL. In the process of using MongoDB as a product prototype, we summarized some highlights of MonogDB: The JSON-style syntax is easy to understand and understand: mongoDB uses the JSON variant BSON as the internal storage format and syntax. All operations on MongoDB use JSON-style syntax, and the data submitted or received by the client is displayed in JSON format. Compared with SQL, it is more intuitive and easy to understand and master. Schema-less: supports embedding sub-documents: MongoDB is a Schema-free document database. A database can have multiple collections, each of which is a Collection of Documents. The Table and Row of Collection and Document are not equal to those of traditional databases. Collection can be created at any time without prior definition. Collection can contain document records with different schemas. This means that the document in your previous record has three attributes, and the document in the next record can have 10 attributes, attribute types can be basic data types (such as numbers, strings, dates, etc.), arrays or hash, or even a sub-document (embed document ). In this way, you can implement the denormalizing data model to improve the query speed. Figure 1 MongoDB is a Schema-free document database. Figure 2 is an example. Works and comments can be designed as a collection. comments are embedded as sub-documents in the comments attribute of art, comments are embedded in the replies attribute as subdocuments of comment sub-documents. According to this design pattern, you only need to retrieve all the relevant information by file id. In MongoDB, we do not emphasize that data must be Normalize. In many cases, we recommend De-normalize. developers can discard the limitations of various paradigms of traditional relational databases, you do not need to map all objects to a Collection. You only need to define the top class. The document model of MongoDB allows us to easily map our own objects to collections for storage. Figure 2 MongoDB supports the simple and easy-to-use query method for Embedded sub-documents: the query in MongoDB is quite comfortable, and the JSON is directly used without the SQL-hard-to-remember syntax, which is quite intuitive. For different development languages, you can use its most basic array or hash format for query. With the added operator, MongoDB supports range query, regular expression query, and subdocument attribute query, which can replace the SQL query of most previous tasks. CRUD is simpler and supports in-place update: you only need to define an array and the insert/update method passed to MongoDB can be automatically inserted or updated. For the update mode, mongoDB supports an upsert option, that is, "If a record exists, it will be updated; otherwise, it will be inserted ". MongoDB's update method also supports Modifier, which allows immediate updates on the server end, saving communication between the client and the server end. These modifer allows MongoDB to have a function similar to Redis, Memcached, and other KV features: Compared with MySQL, MonoDB is simpler and faster.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.