COUNT_SCAN 和 IXSCAN 有什么区别? [英] What is the difference between COUNT_SCAN and IXSCAN?
问题描述
每当我使用解释在 MongoDB 上运行计数查询时,我都会看到两个不同的阶段 COUNT_SCAN 和 IXSCAN.我想根据性能了解它们之间的区别以及如何改进查询.字段已编入索引.
Whenever I run a count query on MongoDB with explain I can see two different stages COUNT_SCAN and IXSCAN. I want to know the difference between them according to performance and how can I improve the query. field is indexed.
query db.collection.explain(true).count({field:1}}) 使用 COUNT_SCAN 和查询像
query db.collection.explain(true).count({field:1}}) uses COUNT_SCAN and query like
db.collection.explain(true).count({field:"$in":[1,2]}) 使用 IXSCAN.
db.collection.explain(true).count({field:"$in":[1,2]}) uses IXSCAN.
推荐答案
The short: COUNT_SCAN
是通过从索引中读取值来获取计数的最有效方法,但只能执行在某些情况下.否则,IXSCAN
会在对文档进行一些过滤和内存中的计数之后执行.
The short: COUNT_SCAN
is the most efficient way to get a count by reading the value from an index, but can only be performed in certain situations. Otherwise, IXSCAN
is performed following by some filtering of documents and a count in memory.
当从二级读取时,使用读取关注 available
.此关注级别不考虑分片集群中的孤立文档,因此不会执行 SHARDING_FILTER
阶段.这是您看到 COUNT_SCAN
的时候.
When reading from secondary the read concern available
is used. This concern level doesn't consider orphan documents in sharded clusters, and so no SHARDING_FILTER
stage will be performed. This is when you see COUNT_SCAN
.
但是,如果我们使用读取关注local
,我们需要获取文档以执行 SHARDING_FILTER 过滤阶段.在这种情况下,有多个阶段来完成查询:IXSCAN
,然后是 FETCH
,然后是 SHAARDING_FILTER
.
However, if we use read concern local
, we need to fetch the documents in order to perform the SHARDING_FILTER filter stage. In this case, there are multiple stages to fulfill the query: IXSCAN
, then FETCH
then SHARDING_FILTER
.
这篇关于COUNT_SCAN 和 IXSCAN 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!