Table of Contents
一、低效的分片键
1.1 分布差
1.2 缺乏局部性
3. 无法拆分的块
二、理想的分片键
Home Database Mysql Tutorial 【MongoDB】在windows平台下mongodb的分片集群(五)

【MongoDB】在windows平台下mongodb的分片集群(五)

Jun 07, 2016 pm 04:09 PM
mongodb windows Fragmentation

本篇接着上面的四篇继续讲述在window平台下mongodb的分片集群搭建。在分片集群中也照样可以创建索引,创建索引的方式与在单独数据库中创建索引的方式一样。因此这不再多说。本篇主要聚焦在分片键的选取问题上。 分片键通俗来说就是分割海量数据的标记符。 如

本篇接着上面的四篇继续讲述在window平台下mongodb的分片集群搭建。在分片集群中也照样可以创建索引,创建索引的方式与在单独数据库中创建索引的方式一样。因此这不再多说。本篇主要聚焦在分片键的选取问题上。

分片键通俗来说就是分割海量数据的标记符。 如果更高效的划分海量数据往往依赖于分片键的选择。 分片键选得不好,应用程序就无法利用分片集群所提供的诸多优势。在这种情况下,查询和插入得系能都回显著下降。

一、低效的分片键

1.1 分布差

BSON对象ID是每个mongodb文档的默认主键。所有的对象ID最重要的组成部分是时间戳,也就是说对象ID是升序的,遗憾的是升序对于分片键来说是很糟糕的。由于分片是基于范围的。使用升序的分片键后,所有最近插入的文档会落在某个很小的连续范围内。如果想让插入负载分不到多个分片上,就不能使用升序分片键,应需某些随机性更强发的的东西。

1.2 缺乏局部性

升序分片键由明确的方向,完全随机的分片键根部没有方向。前者无法分散插入,而后者则可能将插入分散太慢。假设分片集合中每个文档都包含一个MD5,而MD5字段就是分片键。因为MD5随着文档的不同而进行变化。所有该分片键能确保插入的文档均匀分布在集群的分片上。但是有个问题,对于每个分片的MD5字段索引进行的插入过程中,索引中每个虚拟内存分页都有可能被访问到。这就意外着有可能所有的索引和数据都装在内存中。从而超出了物理内存。

3. 无法拆分的块

随机分片键和升序分片键都不好用,那么就尝试一下粗粒度分片键。举个例子,例如用户Id上传了100张照片,那么分片键就是用户ID,第一原因对于每张照片来说具有随机性,同时可以通过局部性引用来提升效率。但有个问题就是当用户ID上传的照片太大时候,以至于不得不分块。而系统又不能把一个用户的照片拆分成多个快。

二、理想的分片键

通过上面分析,理想的分片键应该满足:
1. 将插入数据均匀分布到各个分片上 2.保证crud操作能够利用局部性 3. 有足够的粒度进行块划分
举个例子:创建一个网站分析系统,一个不错的数据模型就是每个网页每月保存一个文档,随后在那个文档中保持该月每天的数据,每次访问某个页面增加一些计数器字段。下面是于分片键有关的实例分析文档:
{ _id: objectId("34535353245eraf32223sdarwe") domin:"org.mongod" url:"download" perid:"2011-12" }
最简单的分片就是包含每个网页的域名,随后是url{domain:1, url:1}所有来自指定域的页面通常都落在一个分片上,但是一些特殊的域拥有大量页面,在必要时候仍会被拆分到分片上。

备注:本篇内容大多引自《MongoDB in action》 Kyle Banker著
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

What is the CentOS MongoDB backup strategy? What is the CentOS MongoDB backup strategy? Apr 14, 2025 pm 04:51 PM

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

How to encrypt data in Debian MongoDB How to encrypt data in Debian MongoDB Apr 12, 2025 pm 08:03 PM

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

How to solve complex BelongsToThrough relationship problem in Laravel? Use Composer! How to solve complex BelongsToThrough relationship problem in Laravel? Use Composer! Apr 17, 2025 pm 09:54 PM

In Laravel development, dealing with complex model relationships has always been a challenge, especially when it comes to multi-level BelongsToThrough relationships. Recently, I encountered this problem in a project dealing with a multi-level model relationship, where traditional HasManyThrough relationships fail to meet the needs, resulting in data queries becoming complex and inefficient. After some exploration, I found the library staudenmeir/belongs-to-through, which easily installed and solved my troubles through Composer.

How to choose a database for GitLab on CentOS How to choose a database for GitLab on CentOS Apr 14, 2025 pm 04:48 PM

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

See all articles