Skip to content

fix(mongo) Change count_documents to estimated_document_count - #3243

Open
zhuatuzilo wants to merge 2 commits into
hhyo:masterfrom
zhuatuzilo:patch-1
Open

fix(mongo) Change count_documents to estimated_document_count#3243
zhuatuzilo wants to merge 2 commits into
hhyo:masterfrom
zhuatuzilo:patch-1

Conversation

@zhuatuzilo

@zhuatuzilo zhuatuzilo commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

数据量较大时,使用count_documents 精确统计行数会影响到生产的负载,用estimated_document_count替换

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cb24b7124d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread sql/engines/mongo.py Outdated
@RankRao

RankRao commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

count = db[table_name].count_documents({})
这一段是在get_table_conut函数(注:函数名应该改为正确英文get_table_count)中,
然后调用的是mongo的精确计数count(),其实在高版本mongo中已经不再推荐使用count()方法。

get_table_conut函数被调用的主要是两个地方:
1、创建索引,判断是否为大表,增加校验background:true。
2、drop, dropIndex, dropIndexes, createIndex, createIndexes, ensureIndex等方法,返回全表的影响行数。

可以考虑更优化下get_table_conut函数
如果
count = db[table_name].estimated_document_count({})
大于100万
直接返回count,
否则小表还是用精确计数
count = db[table_name].count_documents({})

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ca2d2c9c56

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread sql/engines/mongo.py Outdated
@zhuatuzilo

Copy link
Copy Markdown
Contributor Author

count = db[table_name].count_documents({}) 这一段是在get_table_conut函数(注:函数名应该改为正确英文get_table_count)中, 然后调用的是mongo的精确计数count(),其实在高版本mongo中已经不再推荐使用count()方法。

get_table_conut函数被调用的主要是两个地方: 1、创建索引,判断是否为大表,增加校验background:true。 2、drop, dropIndex, dropIndexes, createIndex, createIndexes, ensureIndex等方法,返回全表的影响行数。

可以考虑更优化下get_table_conut函数 如果 count = db[table_name].estimated_document_count({}) 大于100万 直接返回count, 否则小表还是用精确计数 count = db[table_name].count_documents({})

有道理,我改下

Optimize document count retrieval for large tables by using estimated count when above 1 million.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants