Distributed Queries

On this page本页内容

Read Operations to Replica Sets对复制集的读取操作

By default, clients reads from a replica set's primary; however, clients can specify a read preference to direct read operations to other members. 默认情况下,客户端从副本集的primary读取数据;但是,客户端可以指定读取首选项,以便将读取操作定向到其他成员。For example, clients can configure read preferences to read from secondaries or from nearest member to:例如,客户端可以将读取首选项配置为从第二个成员或最近的成员读取到:

  • reduce latency in multi-data-center deployments,减少多数据中心部署的延迟,
  • improve read throughput by distributing high read-volumes (relative to write volume),通过分配高读卷(相对于写卷),提高读吞吐量,
  • perform backup operations, and/or执行备份操作,和/或
  • allow reads until a new primary is elected.允许读取,直到选出新的primary
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.将读取操作定向到不同服务器的读取首选项可能会导致非单调读取。

Changed in version 3.6.在版本3.6中更改

Starting in MongoDB 3.6, clients can use causally consistent sessions, which provides various guarantees, including monotonic reads.从MongoDB 3.6开始,客户端可以使用因果一致的会话,这提供了各种保证,包括单调读取。

You can configure the read preference on a per-connection or per-operation basis. 您可以根据每个连接或每个操作配置读取首选项。For more information on read preference or on the read preference modes, see Read Preference and Read Preference Modes.有关读取首选项读取首选项模式的更多信息,请参阅读取首选项和读取首选项模式。

Write Operations on Replica Sets副本集上的写入操作

In replica sets, all write operations go to the set's primary. 副本集中,所有写入操作都会转到副本集的primaryThe primary applies the write operation and records the operations on the primary's operation log or oplog. primary应用写操作,并将操作记录在主的操作日志或oplog上。The oplog is a reproducible sequence of operations to the data set. oplog是数据集的一个可复制的操作序列。secondary members of the set continuously replicate the oplog and apply the operations to themselves in an asynchronous process.集合的secondary成员持续复制oplog,并在异步过程中将操作应用于自己。

Diagram of default routing of reads and writes to the primary.

For more information on replica sets and write operations, see Replication and Write Concern.有关副本集和写入操作的更多信息,请参阅副本写入关注

Read Operations to Sharded Clusters将操作读取到分片群集

Sharded clusters allow you to partition a data set among a cluster of mongod instances in a way that is nearly transparent to the application. 分片集群允许您以对应用程序几乎透明的方式在mongod实例集群中划分数据集。For an overview of sharded clusters, see the Sharding section of this manual.有关分片集群的概述,请参阅本手册的分片部分。

For a sharded cluster, applications issue operations to one of the mongos instances associated with the cluster.对于分片集群,应用程序向与集群关联的一个mongos实例发出操作。

Diagram of a sharded cluster.

Read operations on sharded clusters are most efficient when directed to a specific shard. 分片集群上的读取操作在定向到特定分片时效率最高。Queries to sharded collections should include the collection's shard key. 对分片集合的查询应包括集合的分片键When a query includes a shard key, the mongos can use cluster metadata from the config database to route the queries to shards.当查询包含分片键时,mongos可以使用配置数据库中的集群元数据将查询路由到分片。

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.

If a query does not include the shard key, the mongos must direct the query to all shards in the cluster. 如果查询不包括分片键,mongos必须将查询指向集群中的所有分片。These scatter gather queries can be inefficient. 这些分散-聚集查询可能效率低下。On larger clusters, scatter gather queries are unfeasible for routine operations.在较大的集群上,分散-聚集查询对于常规操作是不可行的。

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.

For replica set shards, 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.将读取操作定向到不同服务器的读取首选项可能会导致非单调读取。

Note注意

Starting in MongoDB 3.6,从MongoDB 3.6开始,

  • Clients can use causally consistent sessions, which provides various guarantees, including monotonic reads.客户端可以使用因果一致的会话,这提供了各种保证,包括单调读取。
  • All members of a shard replica set, not just the primary, maintain the metadata regarding chunk metadata. 分片复制集的所有成员,而不仅仅是主成员,都维护有关块元数据的元数据。This prevents reads from the secondaries from returning orphaned data if not using read concern "available". 如果不使用读取关注点"available",这将防止从二级读取返回孤立数据In earlier versions, reads from secondaries, regardless of the read concern, could return orphaned documents.在早期版本中,从二级读入,无论读关注点是什么,都可能返回孤立文档。

For more information on read operations in sharded clusters, see the mongos and Shard Keys sections.有关分片集群中读取操作的更多信息,请参阅mongos分片键部分。

Write Operations on Sharded Clusters分片集群上的写操作

For sharded collections in a sharded cluster, the mongos directs write operations from applications to the shards that are responsible for the specific portion of the data set. 对于分片集群中的分片集合,mongos将写操作从应用程序引导到负责数据集特定部分的分片。The mongos uses the cluster metadata from the config database to route the write operation to the appropriate shards.mongos使用配置数据库中的集群元数据将写操作路由到适当的分片。

Diagram of a sharded cluster.

MongoDB partitions data in a sharded collection into ranges based on the values of the shard key. MongoDB根据分片键的值将分片集合中的数据划分为多个范围Then, MongoDB distributes these chunks to shards. 然后,MongoDB将这些块分配给分片。The shard key determines the distribution of chunks to shards. 分片键决定了块到分片的分布。This can affect the performance of write operations in the cluster.这可能会影响群集中写入操作的性能。

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

Update operations that affect a single document must include the shard key or the _id field. 影响单个文档的更新操作必须包括分片键_id字段。Updates that affect multiple documents are more efficient in some situations if they have the shard key, but can be broadcast to all shards.影响多个文档的更新在某些情况下更有效,如果它们具有分片键,但可以广播到所有分片。

If the value of the shard key increases or decreases with every insert, all insert operations target a single shard. 如果分片键的值随着每次插入而增加或减少,则所有插入操作都以单个分片为目标。As a result, the capacity of a single shard becomes the limit for the insert capacity of the sharded cluster.因此,单个分片的容量成为分片集群插入容量的限制。

For more information, see Sharding and Bulk Write Operations.有关详细信息,请参阅分片批量写入操作

Tip提示
See also: 参阅:
←  Atomicity and TransactionsField Names with Periods (.) and Dollar Signs ($) →