Mongodbshard Tablet Key Selection

Source: Internet
Author: User

Choosing a tablet key requires caution, because once selected, it cannot be changed.

There are two types of options commonly used:

(1) Ascending tab key, common such as _id,date, from other databases of the self-increasing primary key and so on. If you use the ascending slice key, then the data will be physically contiguous and benefit from a range-based query, because the database takes in a number of blocks at a time, and because it is continuously arranged, the subsequent data does not need to take new chunks through IO. But the obvious problem with ascending keys is that the data will only be inserted into the largest chunk. There was only one block at the beginning, then with the data inserted, the block will be divided into two blocks, one is a smaller range value, one is larger, the subsequent data because it is added to the order, it will only be inserted into the larger fast, so, each time the division, will result in the smaller data value of the block without any write, all the write will only focus on the largest chunk, This can degrade write performance. Furthermore, the space utilization of these blocks is not high because there are no write requests after the other block splits.

(2) Random chip key, you can use the hash key mechanism of mongodb, so that all write requests will be balanced forward to each block of data. Solves the problem of low write efficiency. But the range-based query efficiency becomes lower. Because the data is not physically emitted according to the slice key value, the adjacent data values may be in different blocks of data, which requires more IO times to be taken to the memory.


There is a better way to combine the advantages of both, that is, the composite chip key strategy.

The slice key is composed of two parts, the first part is a random value, but the value of the degree is less than the number of shards, or often several times. The second part is the ascending value. In this way, each piece of data will be distributed according to the first part, distribution after each piece of the internal is ascending, so that when looking, you can follow the Shard to check, each shard inside is ascending, the efficiency will become higher.

For example, the first part is {a,b,c}, the second part is ascending, then the slice key may be A1 B2 C3 A4 B5 C6 A10 B15 C20.

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.