Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize Mongodb Database

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to optimize the Mongodb database, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Database design optimization

In the project design phase, defining the purpose of the collection is a very important step for performance tuning.

From the perspective of performance optimization, the design of the collection needs to consider the common operations of the data in the collection. For example, we need to design a log collection, and the log is viewed frequently, but the write frequency is very high, so we can get that the common operation in this collection is to update (add, delete, modify). What if we want to save a list of cities? Obviously, this collection is a collection with a high frequency of viewing but a low frequency of writing, so the common operation is query.

For collections with frequent updates and queries, the most important thing we need to pay attention to is the degree of their normalization. Suppose we need to store a book and its author now, the association in MongoDB can be reflected in the following forms:

1. Complete separation (stylized design)

Example 1:

View Code {"_ id": ObjectId ("5124b5d86041c7dca81917"), "title": "how to use MongoDB", "author": [ObjectId ("144b5d83041c7dca84416"), ObjectId ("144b5d83041c7dca84418"), ObjectId ("144b5d83041c7dca84420"),]}

We added the id array of the author (comment) to the book as a field. This kind of design is commonly used in non-relational databases, which is what we call paradigm design. In MongoDB, we extract the books that are not directly related to the primary key to another collection, and make the association query by storing the primary key. When we want to query articles and comments, we need to first query the articles we need, then get the comment id from the article, and finally use the complete article and its comments. In this case, the query performance is obviously not ideal. However, when an author's information needs to be modified, the advantage of stylized maintenance is highlighted, and we can modify the author's field without considering the book associated with the author.

two。 Fully embedded (anti-stylized design)

Example 2:

View Code {"_ id": ObjectId ("5124b5d86041c7dca81917"), "title": "how to use MongoDB", "author": [{"name": "Ding Lei"age": 40, "nationality": "china" }, {"name": "Jack Ma", "age": 49, "nationality": "china",} {"name": "Zhang Zhaozhong"age": 59, "nationality": "china",},]}

In this example, we completely embed the author's field into the book, and when querying the book, we can get all the information of the corresponding author, but because an author may have more than one book, when modifying an author's information, we need to go through all the books to find the author and modify it.

3. Partial embedding (compromise)

Example 3:

View Code {"_ id": ObjectId ("5124b5d86041c7dca81917"), "title": "how to use MongoDB", "author": [{"_ id": ObjectId ("144b5d83041c7dca84416"), "name": "Ding Lei"} {"_ id": ObjectId ("144b5d83041c7dca84418"), "name": "Jack Ma"}, {"_ id": ObjectId ("144b5d83041c7dca84420") "name": "Zhang Zhaozhong",]}

This time we extract the most commonly used part of the author field. When we only need to get the name of the book and the author, we do not need to query the author collection again, we can get it only in the book collection query.

This method is a relative compromise, which ensures not only the query efficiency, but also the update efficiency. However, this method is obviously more difficult to master than the first two, and the difficulty lies in the need to combine with the actual business to find a suitable extraction field. As shown in example 3, the name is obviously not a frequently modified field, and it is fine to extract such a field, but if the extracted field is a frequently modified field (such as age), we still need to update this field extensively and update it accordingly.

On how to optimize the Mongodb database to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report