milvus数据条数统计的一些问题

前言

用的milvus版本为2.3.7。
遇到的问题:
collection.query(expr=“”, output_fields=[“count(*)”]) 和 collection.query_iterator迭代后条数不一致

下面的统计的完整代码:

# milvus_consistency_check.py
# 用于验证 Milvus 中 count(*) 与 query_iterator 的数据一致性

from pymilvus import connections, Collection
import time

# ==================== 配置 ====================
MILVUS_HOST = "127.0.0.1"        # 修改为你的 Milvus 地址
MILVUS_PORT = 19530               # 默认端口
COLLECTION_NAME = "test_collection"  # 修改为你的集合名
DB_NAME = "your_db"
BATCH_SIZE = 1000

# ==================== 获取 count(*) ====================
def get_count_star(collection):
    try:
        res = collection.query(expr="", output_fields=["count(*)"])
        return res[0]["count(*)"]
    except Exception as e:
        print(f"❌ count(*) 查询失败: {
     
     e}")
        raise

# ==================== 获取 query_iterator 真实条数 ====================
def get_query_iterator_count(collection):
    try:
        # 创建迭代器(必须指定 output_fields)
        it = collection.query_iterator(
            expr="", 
            batch_size=BATCH_SIZE, 
            output_fields=["id"]  # 只需要 id 字段即可计数
        )

        count = 0
        batch_count = 0
        while True:
            result = it.next()
            if not result:
                break
            count += len(result)
            batch_count += 1
            print(f"  批次 {
     
     batch_count}: 读取 {
     
     len(result)} 条,累计 {
     
     count}")

        it.close()  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SunnyRivers

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值