メインコンテンツまでスキップ
バージョン: User Guides (BYOC)

ランダムサンプリング

大規模なデータセットを扱う際、インサイトを得たりフィルタリングロジックをテストしたりするために、必ずしもすべてのデータを処理する必要はありません。ランダムサンプリングは、統計的に代表的なデータのサブセットを使用できるようにすることで、この課題を解決します。これにより、クエリ時間とリソース消費を大幅に削減できます。

ランダムサンプリングはセグメントレベルで動作し、コレクション全体のデータ分布にわたってサンプルのランダム性を保ちつつ、効率的なパフォーマンスを実現します。

主なユースケース:

  • データ探索: リソース使用量を最小限に抑えながら、コレクションの構造と内容をすばやくプレビュー

  • 開発テスト: 本番環境への完全展開前に、複雑なフィルタリングロジックを扱いやすいデータサンプルでテスト

  • リソース最適化: 探索的クエリや統計分析における計算コストを削減

構文

filter = "RANDOM_SAMPLE(sampling_factor)"

パラメータ:

  • sampling_factor: (0, 1) の範囲(境界値を除く)のサンプリング係数。たとえば、RANDOM_SAMPLE(0.001) は結果の約 0.1% を選択します。

重要なルール:

  • 式は大文字・小文字を区別しません(RANDOM_SAMPLE または random_sample のいずれでも可)

  • サンプリング係数は (0, 1) の範囲内(境界値を除く)でなければなりません

他のフィルターとの組み合わせ

ランダムサンプリング演算子は、論理演算子 AND を使って他のフィルター式と組み合わせる必要があります。フィルターを組み合わせる場合、Milvus はまず他の条件を適用し、その後結果セットに対してランダムサンプリングを実行します。

# Correct: Filter first, then sample
filter = 'color == "red" AND RANDOM_SAMPLE(0.001)'
# Processing: Find all red items → Sample 0.1% of those red items

# Incorrect: OR doesn't make logical sense
filter = 'color == "red" OR RANDOM_SAMPLE(0.001)' # ❌ Invalid logic
# This would mean: "Either red items OR sample everything" - which is meaningless

例 1: データ探索

コレクションの構造をすばやくプレビューします:

from pymilvus import MilvusClient

client = MilvusClient(uri="YOUR_CLUSTER_ENDPOINT")

# Sample approximately 1% of the entire collection
result = client.query(
collection_name="product_catalog",
filter="RANDOM_SAMPLE(0.01)",
output_fields=["id", "product_name"],
limit=10
)

print(f"Sampled {len(result)} products from collection")

例 2: ランダムサンプリングを用いた複合フィルタリング

扱いやすいサブセットに対してフィルタリングロジックをテストします:

# First filter by category and price, then sample 0.5% of results
filter_expression = 'category == "electronics" AND price > 100 AND RANDOM_SAMPLE(0.005)'

result = client.query(
collection_name="product_catalog",
filter=filter_expression,
output_fields=["product_name", "price", "rating"],
limit=10
)

print(f"Found {len(result)} electronics products in sample")

例3: クイック分析

フィルタリングされたデータに対して迅速な統計分析を実行します:

# Get insights from ~0.1% of premium customer data
filter_expression = 'customer_tier == "premium" AND region == 'North America' AND RANDOM_SAMPLE(0.001)'

result = client.query(
collection_name="customer_profiles",
filter=filter_expression,
output_fields=["purchase_amount", "satisfaction_score", "last_purchase_date"],
limit=10
)

# Analyze sample for quick insights
if result:
average_purchase = sum(r["purchase_amount"] for r in result) / len(result)
average_satisfaction = sum(r["satisfaction_score"] for r in result) / len(result)

print(f"Sample size: {len(result)}")
print(f"Average purchase amount: ${average_purchase:.2f}")
print(f"Average satisfaction score: {average_satisfaction:.2f}")

フィルター検索のシナリオでランダムサンプリングを使用する:

# Search for similar products within a sampled subset
search_results = client.search(
collection_name="product_catalog",
data=[[0.1, 0.2, 0.3, 0.4, 0.5]], # query vector
filter='category == "books" AND RANDOM_SAMPLE(0.01)',
search_params={"metric_type": "L2", "params": {}},
output_fields=["title", "author", "price"],
limit=10
)

print(f"Found {len(search_results[0])} similar books in sample")

Best practices

  • Start small: 初期の探索段階では、小さいサンプリング係数(0.001~0.01)から始めましょう

  • 開発ワークフロー: 開発ワークフローでは開発中にサンプリングを使用し、本番クエリではこれを削除します

  • 統計的妥当性: 大きいサンプルほど、より正確な統計的妥当性を提供します

  • パフォーマンステスト: パフォーマンステストを行い、必要に応じてサンプリング係数を調整してください