エンティティ数のカウント
この記事では、コレクション内のエンティティ数をカウントする方法と、エンティティ数が実際の値と異なる可能性がある理由について説明します。
概要
Zilliz Cloud では、コレクション内のエンティティ数をカウントする方法が2つ提供されています。
-
count(*)を出力フィールドとして使用したクエリコレクション内の正確なエンティティ数を取得するには、この方法を使用し、以下の条件を満たす必要があります。
- 対象のコレクションがロード済みであること。
- クエリリクエストで
consistency_levelをStrongに設定すること。 output_fieldを['count(*)']に設定すること。
このようなクエリを受信すると、Zilliz Cloud はクエリノードにリクエストを送信し、メモリにすでにロードされているエンティティをカウントします。
複数のパーティション名をクエリ内で指定することで、それらのパーティションごとのエンティティ数を取得できます。詳細については、「Query with count(*) as the output field」を参照してください。
-
get_collection_stats()の使用上記の方法でコレクションの正確なエンティティ数を取得できますが、すべての場面でこの方法を使用することは推奨されません。この処理は基本的にクエリであり、頻繁に呼び出すとネットワークの不安定化や、ビジネスに関連する検索・クエリへの影響を引き起こす可能性があります。
精度が最重要でない場合は、代わりに
get_collection_stats()およびget_partition_stats()を使用することを推奨します。この呼び出しは推定されたエンティティ数を提供しますが、対象のコレクションをロードする必要がなく、内部トラッカーが記録している情報を報告するだけなので、コストは非常に小さく無視できるほどです。なお、すべてのデータ操作は非同期で実行されるため、内部トラッカーがエンティティ数をリアルタイムで反映できない点にご注意ください。詳細については、「Use get_collection_stats()」を参照してください。
上記のどちらの方法も、同じプライマリキーを持つエンティティを別々のエンティティとしてカウントします。
プログラムによるエンティティ数の取得に加えて、Zilliz Cloud コンソール上でクラスター、コレクション、またはパーティションごとのエンティティ数を確認することもできます。詳細については、「Entity counts on the Zilliz Cloud console」をご覧ください。
count(*) を出力フィールドとして使用したクエリ
正確なエンティティ数を取得するには、コレクションをロードし、count(*) を出力フィールドとして指定してクエリを実行し、そのクエリの整合性レベル(consistency level)を Strong に設定します。
- Python
- Java
- Go
- NodeJS
- cURL
# Count without the entities in growing segments
res = client.query(
collection_name="test_collection",
output_fields=['count(*)']
)
# Count with the entities in growing segments
res = client.query(
collection_name="test_collection",
output_fields=['count(*)'],
consistency_level="Strong"
)
# Count the entities in a specific partition
res = client.query(
collection_name="test_collection",
output_fields=['count(*)'],
partition_names=['_default']
)
# Get the entity count
print(res[0]['count(*)'])
# Output
# 20
import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp
// Count without the entities in growing segments
QueryResp count = client.query(QueryReq.builder()
.collectionName("test_collection")
.filter("")
.outputFields(Collections.singletonList("count(*)"))
.build());
// Count with the entities in growing segments
count = client.query(QueryReq.builder()
.collectionName("test_collection")
.filter("")
.outputFields(Collections.singletonList("count(*)"))
.consistencyLevel(ConsistencyLevel.STRONG)
.build());
// Count the entities in a specific partition
countR = client.query(QueryReq.builder()
.collectionName("test_collection")
.filter("")
.outputFields(Collections.singletonList("count(*)"))
.partitionNames(Collections.singletonList("_default"))
.build());
System.out.print(count.getQueryResults().get(0).getEntity().get("count(*)"));
// Output
// 20
resultSet, err := client.Query(ctx, milvusclient.NewQueryOption("test_collection").
WithFilter("").
WithOutputFields("count(*)").
WithConsistencyLevel(entity.ClStrong))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println("count: ", resultSet.GetColumn("count").FieldData().GetScalars())
import { MilvusClient, DataType } from "@zilliz/milvus2-sdk-node";
const address = "YOUR_CLUSTER_ENDPOINT";
const token = "YOUR_CLUSTER_TOKEN";
const client = new MilvusClient({address, token});
// Count with the entities in growing segments
let res = await client.query({
collection_name: "test_collection",
output_fields: ["count(*)"],
consistency_level: 'Strong'
});
// Count the entities in a specific partition
res = await client.query({
collection_name: "test_collection",
output_fields: ["count(*)"],
partition_names: ['_default']
});
// Get the entity count
console.log(res.data[0]['count(*)'])
// Output
// 20
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "test_collection",
"filter": "",
"outputFields": ["count(*)"]
}'
#{"code":0,"cost":0,"data":[{count: 20}]}
get_collection_stats() の使用
前述のとおり、get_collection_stats() はコレクション内のエンティティ数の推定値を返します。この値は実際のエンティティ数と異なる場合があります。このメソッドはコレクションをロードせずに参照として使用できます。
以下の例では、test_collection という名前のコレクションがすでに存在しているものと仮定しています。
- Python
- Java
- Go
- NodeJS
- cURL
from pymilvus import MilvusClient
# 1. Set up a milvus client
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
# 2. Get the entity count of a collection
client.get_collection_stats(collection_name="test_collection")
# Output
#
# {
# 'row_count': 1000
# }
# 3. Get the entity count of a partition
client.get_partition_stats(
collection_name="test_collection",
partition_name="_default"
)
# Output
#
# {
# 'row_count': 1000
# }
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.collection.request.GetCollectionStatsReq;
import io.milvus.v2.service.collection.response.GetCollectionStatsResp;
import io.milvus.v2.service.partition.request.GetPartitionStatsReq;
import io.milvus.v2.service.partition.response.GetPartitionStatsResp;
// 1. Set up a milvus client
MilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
.uri("YOUR_CLUSTER_ENDPOINT")
.token("YOUR_CLUSTER_TOKEN")
.build());
// 2. Get the entity count of a collection
GetCollectionStatsResp stats = client.getCollectionStats(GetCollectionStatsReq.builder()
.collectionName("test_collection")
.build());
System.out.print(stats.getNumOfEntities());
// 3. Get the entity count of a partition
GetPartitionStatsResp partitionStats = client.getPartitionStats(GetPartitionStatsReq.builder()
.collectionName("test_collection")
.partitionName("_default")
.build());
System.out.print(partitionStats.getNumOfEntities());
// go
import { MilvusClient } from '@zilliz/milvus2-sdk-node';
// 1. Set up a milvus client
const milvusClient = new MilvusClient({
address: 'YOUR_CLUSTER_ENDPOINT',
token: 'YOUR_CLUSTER_TOKEN'
});
// 2. Get the entity count
milvusClient.getCollectionStats({
collection_name: 'test_collection',
partition_name: '_default'
});
// Output
//
// {
// data: {'row_count': 1000 }
// }
# curl
Zilliz Cloud コンソールでのエンティティ数
プログラムでエンティティ数をカウントする代わりに、Zilliz Cloud コンソールにアクセスして、クラスター、コレクション、またはパーティションのエンティティ数を以下のページで確認することもできます。
メトリクス
クラスターの メトリクス タブで、エンティティ数 および ロードされたエンティティ (Approx.) を確認できます。どちらの値も推定値です。グラフ内の値は、こちらで説明されているget_collection_stats() を使用して取得されます。これ以降データの挿入や削除が行われない場合、エンティティ数 のグラフは最終的に現在のコレクション内の実際のエンティティ数を反映します。

コレクションの詳細
コレクションの詳細タブで、そのコレクションの正確なエンティティ数を確認できます。この値は、出力フィールドとして count(*) を使用したクエリによって取得されます。

パーティション
コレクションの パーティション タブを使用して、その子パーティションにロードされたエンティティの推定数を確認することもできます。この値は get_partition_stats() を使用して取得されます。

よくある質問
-
get_collection_stats()またはget_partition_stats()を使用して取得したエンティティ数が、エンティティを挿入した後にターゲットのコレクションまたはパーティション内の実際のエンティティ数を反映しないのはなぜですか?これらのメソッドは内部トラッカーが記録している内容のみを報告します。すべてのデータ操作は非同期で実行されるため、実際のエンティティ数と異なる場合があります。
-
コレクションのメトリクスタブにある エンティティ数 のグラフが、エンティティを挿入または削除した後も変化しないのはなぜですか?
エンティティ数 グラフの値は特定の時点での推定値です。すべてのデータ操作は非同期で実行されるため、グラフに反映されるまでに遅延が生じる可能性があります。
-
コレクションの パーティション タブにある エンティティ数 (Approx.) 列の値が、エンティティを挿入または削除した後も変化しないのはなぜですか?
表示されているパーティションの値はすべて推定値です。すべてのデータ操作は非同期で実行されるため、グラフに反映されるまでに遅延が生じる可能性があります。
-
コレクションの Overview タブに表示される ロードされたエンティティ の値が、コレクション内の実際のエンティティ数を反映していないのはなぜですか?
ロードされたエンティティ に表示される値は正確です。この値と通常のクエリで取得されるエンティティ数との間にギャップがある場合、コレクション内に重複する主キーを持つエンティティが存在する可能性があります。
count(*)を出力フィールドとするクエリは、主キーが同一のエンティティを別々のエンティティとして扱いますが、他のクエリでは同じ主キーを持つエンティティを最終結果を返す前に除外します。