翻译或纠错本页面

分布式查询

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

分片集群中的读操作

分片集群

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

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.

如果某个查询不包括片键, 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 Preference Modes 来了解更多关于复制集读选项或者复制集读选项模式的信息。

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

复制集上的读操作

By default, clients reads from a replica set’s primary; however, clients can specify a read preference to direct read operations to other members. For example, clients can configure read preferences to read from secondaries or from nearest member to:

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

  • perform backup operations, and/or
  • allow reads until a new primary is elected.
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 from secondary members of replica sets may not reflect the current state of the primary. Read preferences that direct read operations to different servers may result in non-monotonic reads.

You can configure the read preferece on a per-connection or per-operation basis. For more information on read preference or on the read preference modes, see 复制集读选项 and Read Preference Modes.