OPTIONS
翻译或纠错本页面

分布式查询

分片集群中的读操作

分片集群

对于一个分片集群而言,应用会将操作分发到与该集群相关的 mongos 实例之一。

Diagram of a sharded cluster.

Diagram of a sharded cluster.

当直接导入到一个特定的分片时,分片集群上的写操作是最高效的。对分片集合的查询应该包括该集合的 shard key 。当某个查询包括一个片键时, mongos 可以使用从 config database 中获得的集群元数据来将这些查询路由到分片。

Read operations to a sharded cluster. Query criteria includes the shard key. The query router ``mongos`` can target the query to the appropriate shard or shards.

Read operations to a sharded cluster. Query criteria includes the shard key. The query router mongos can target the query to the appropriate shard or shards.

如果某个查询不包括片键, mongos 一定会将该查询引导到集群中的 所有 分片中。这种 散集 式查询时非常低效的。在更大的集群上,散集式查询对于路由操作而言是难以执行的。

Read operations to a sharded cluster. Query criteria does not include the shard key. The query router ``mongos`` must broadcast query to all shards for the collection.

Read operations to a sharded cluster. Query criteria does not include the shard key. The query router mongos must broadcast query to all shards for the collection.

请查阅 集群的查询路由分发片键 部分以了解更多关于分片集群中写操作更多的信息。

复制集上的写操作

Replica sets use read preferences to determine where and how to route read operations to members of the replica set. By default, MongoDB always reads data from a replica set’s primary. You can modify that behavior by changing the read preference mode.

您可以在每个连接及每个操作的基础上对 read preference mode 进行配置,允许将数据从 从节点 中读取到:

  • 降低多数据中心部署中的延迟,

  • 通过分发高读取量(相对于写入量而言)提高读取的吞吐量,

  • 对于备份操作,以及/或者

  • failover 的情况下允许读取操作。

Read operations to a replica set. Default read preference routes the read to the primary. Read preference of ``nearest`` routes the read to the nearest member.

Read operations to a replica set. Default read preference routes the read to the primary. Read preference of nearest routes the read to the nearest member.

复制集从节点成员上的读操作并不能保证能够反映出主节点当前的状态,因为从节点的状态会落后于主节点一段时间。很多情况下,应用不依赖于这种类型的严格一致性,但是应用的开发者在设置复制集读选项之前,应该要经常考虑应用的需求。

请查阅 复制集读选项 以及 Read Preference Modes 来了解更多关于复制集读选项或者复制集读选项模式的信息。

←   查询计划 写操作  →