zoukankan      html  css  js  c++  java
  • mongodb分片介绍—— 基于范围(数值型)的分片 或者 基于哈希的分片

    数据分区

    MongoDB中数据的分片是以集合为基本单位的,集合中的数据通过 片键 被分成多部分.

    片键

    • 对集合进行分片时,你需要选择一个 片键 , shard key 是每条记录都必须包含的,且建立了索引的单个字段或复合字段,MongoDB按照片键将数据划分到不同的 数据块 中,并将 数据块 均衡地分布到所有分片中.为了按照片键划分数据块,MongoDB使用 基于范围的分片方式 或者 基于哈希的分片方式。

    以范围为基础的分片

    对于 基于范围的分片 ,MongoDB按照片键的范围把数据分成不同部分.假设有一个数字的片键:想象一个从负无穷到正无穷的直线,每一个片键的值都在直线上画了一个点.MongoDB把这条直线划分为更短的不重叠的片段,并称之为 数据块 ,每个数据块包含了片键在一定范围内的数据.

    在使用片键做范围划分的系统中,拥有”相近”片键的文档很可能存储在同一个数据块中,因此也会存储在同一个分片中.

    Diagram of the shard key value space segmented into smaller ranges or chunks.

    基于哈希的分片

    对于 基于哈希的分片 ,MongoDB计算一个字段的哈希值,并用这个哈希值来创建数据块.

    在使用基于哈希分片的系统中,拥有”相近”片键的文档 很可能不会 存储在同一个数据块中,因此数据的分离性更好一些.

    Diagram of the hashed based segmentation.

    基于范围的分片方式与基于哈希的分片方式性能对比

    基于范围的分片方式提供了更高效的范围查询,给定一个片键的范围,分发路由可以很简单地确定哪个数据块存储了请求需要的数据,并将请求转发到相应的分片中.

    不过,基于范围的分片会导致数据在不同分片上的不均衡,有时候,带来的消极作用会大于查询性能的积极作用.比如,如果片键所在的字段是线性增长的,一定时间内的所有请求都会落到某个固定的数据块中,最终导致分布在同一个分片中.在这种情况下,一小部分分片承载了集群大部分的数据,系统并不能很好地进行扩展.

    与此相比,基于哈希的分片方式以范围查询性能的损失为代价,保证了集群中数据的均衡.哈希值的随机性使数据随机分布在每个数据块中,因此也随机分布在不同分片中.但是也正由于随机性,一个范围查询很难确定应该请求哪些分片,通常为了返回需要的结果,需要请求所有分片.

    均衡

    The balancer is a background process that manages chunk migrations. The balancer can run from any of themongos instances in a cluster.

    当集群中数据的不均衡发生时,均衡器会将数据块从数据块数目最多的分片迁移到数据块最少的分片上,举例来讲:如果集合 users 在 分片1 上有100个数据块,在 分片2 上有50个数据块,均衡器会将数据块从 分片1一直向 分片2 迁移,一直到数据均衡为止.

    The shards manage chunk migrations as a background operation between an origin shard and a destination shard. During a chunk migration, the destination shard is sent all the current documents in the chunk from theorigin shard. Next, the destination shard captures and applies all changes made to the data during the migration process. Finally, the metadata regarding the location of the chunk on config server is updated.

    If there’s an error during the migration, the balancer aborts the process leaving the chunk unchanged on the origin shard. MongoDB removes the chunk’s data from the origin shard after the migration completes successfully.

    Diagram of a collection distributed across three shards. For this collection, the difference in the number of chunks between the shards reaches the *migration thresholds* (in this case, 2) and triggers migration.
  • 相关阅读:
    Sharepoint 2010:基于当前用户判断访问列表项目的权限 Determine access to SPListItem based on a Current User
    vs2010修改sharepoint部署站点
    写入数据内存溢出的错误ErrorCode=2147217900解决
    新鲜网络故障:网线测试器测试,居然2,3信号灯齐亮!解决
    CuteEditor出错了,居然是没有了lic文件
    路由表的故障?如何理解设置
    MSSQL如何快速清除数据库日志转,经实践有效
    iis中web站点无法启动:另一程序正在使用该文件,进程无法访问解决
    二次开发图片浏览系统应用Lightbox image viewer 2.03a并带javascript的字符串处理
    asp session丢失的分析和解决方法
  • 原文地址:https://www.cnblogs.com/bonelee/p/6282142.html
Copyright © 2011-2022 走看看