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 BSON to achieve its goal

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how BSON is optimized to achieve its goals. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

BSON is a data format developed by 10gen, which is mainly used in MongoDB and is the data storage format of MongoDB. BSON is based on JSON format, and the main reason for choosing JSON for transformation is the versatility of JSON and the schemaless characteristics of JSON.

1. Faster traversal speed

For JSON formats, too large a JSON structure can cause data traversal to be very slow. In JSON, to skip a document for data reading, you need to scan the document, and you need to do troublesome data structure matching, such as parenthesis matching, and BSON's great improvement to JSON is that it will store the length of each element of JSON in the head of the element, so that you only need to read the length of the element to seek directly to the specified point for reading.

MongoDB optimization: for MongoDB, because MMAP is used to map memory to data files, when updating or obtaining a field of Document, if you need to read all the fields in front of it first, it will cause physical memory to be loaded into unnecessary fields, resulting in unreasonable allocation of resources. With BSON, you just need to read to the appropriate location and then step over the useless content to read the required content.

two。 Easier to operate

For JSON, the data store is untyped, for example, if you want to modify a basic value from 9 to 10, because it has changed from one character to two, it is possible that everything behind it needs to be moved back one bit. With BSON, you can specify this column of numbers, so whether the number grows from 9 to 10 or 100, we only modify the bit in which the number is stored, without causing the total length of the data to become larger. Of course, in MongoDB, if the number increases from the shaping to the long integer, it will still cause the total length of the data to become larger.

MongoDB optimization: so one trick for using MongoDB is to name fields that can vary in length as far back as possible (MongoDB rearranges the fields alphabetically after the update operation, so later means to name them in the order of amerz). In this way, if the number becomes longer when it is updated, there is no need to move a large amount of data. A typical example is that if the file name or file description may become longer when storing files in binary types, it is a wise choice to name this field later as far as possible, otherwise when the file name or file description field changes, it will cause long binary data to be moved, resulting in unnecessary waste.

How to optimize BSON to achieve its goal

3. Added additional data types

JSON is a convenient data exchange format, but its type is limited. BSON adds the "bytearray" data type to it. This eliminates the need for binary storage to be converted to base64 and then saved to JSON. The computing overhead and data size are greatly reduced.

Of course, there are times when BSON has no space advantage over JSON. For example, for {"field": 7}, 7 only uses one byte on JSON storage, while if BSON is used, it is at least 4 bytes (32 bits).

MongoDB optimization: in MongoDB, if your field is numeric and involves data addition and subtraction, it is recommended to have int, but if it is a fixed number and less than four digits, consider saving it as a string. This will save space.

At present, thanks to the efforts of 10gen, BSON already has encoding and decoding packages for multiple languages. And they are all open source under Apache2license. And is still in with the further development of MongoDB. You can get more information about BSON on its official website, bsonspec.org.

Thank you for reading! This is the end of the article on "how to optimize BSON to achieve its goals". I hope the above content can be of some help to you, so that 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

Database

Wechat

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

12
Report