AgentDB Performance Optimization について
AgentDB の利用者向けに、量子化、HNSW インデックス、キャッシュ、バッチ処理による性能最適化を解説するスキルです。メモリ最適化、性能監視、パラメーター調整も扱います。
主な機能
- 01バイナリー、スカラー、積量子化
- 02HNSW インデックスの設定と調整
- 03LRU パターンキャッシュ
- 04バッチ挿入と並列検索
- 05データベース統計と実行時指標
- 06パターンの統合、剪定、最適化
活用シーン
- 01大規模ベクトルデータベースのメモリ削減
- 02ベクトル類似検索の高速化
- 03データ規模に応じた量子化とインデックス設定
- 04検索遅延や精度の問題の調査
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ ruvnet/ ruflo --skill agentdb-performance-optimizationインストール後、Claude Code で/agentdb-performance-optimizationを入力して読み込まれたか確認します。
詳細
AgentDB パフォーマンス最適化
このスキルでできること
AgentDB ベクトルデータベースの包括的なパフォーマンス最適化手法を提供します。量子化、HNSW インデックス、キャッシュ戦略、バッチ操作により、パフォーマンスを150倍〜12,500倍向上させます。精度を維持しながらメモリ使用量を4〜32分の1に削減します。
パフォーマンス:ベクトル検索は100µs未満、パターン取得は1ms未満、100個のベクトルのバッチ挿入は2msです。
前提条件
- Node.js 18+
- AgentDB v1.0.7+(agentic-flow 経由)
- 既存の AgentDB データベースまたはアプリケーション
クイックスタート
パフォーマンスベンチマークを実行する
# Comprehensive performance benchmarking
npx agentdb@latest benchmark
# Results show:
# ✅ Pattern Search: 150x faster (100µs vs 15ms)
# ✅ Batch Insert: 500x faster (2ms vs 1s for 100 vectors)
# ✅ Large-scale Query: 12,500x faster (8ms vs 100s at 1M vectors)
# ✅ Memory Efficiency: 4-32x reduction with quantization
最適化を有効にする
import { createAgentDBAdapter } from 'agentic-flow$reasoningbank';
// Optimized configuration
const adapter = await createAgentDBAdapter({
dbPath: '.agentdb$optimized.db',
quantizationType: 'binary', // 32x memory reduction
cacheSize: 1000, // In-memory cache
enableLearning: true,
enableReasoning: true,
});
量子化戦略
1. バイナリ量子化(32分の1に削減)
最適な用途:大規模なデプロイメント(100万個以上のベクトル)、メモリに制約のある環境
トレードオフ:精度が約2〜5%低下、メモリを32分の1に削減、10倍高速化
const adapter = await createAgentDBAdapter({
quantizationType: 'binary',
// 768-dim float32 (3072 bytes) → 96 bytes binary
// 1M vectors: 3GB → 96MB
});
ユースケース:
- モバイル/エッジでのデプロイメント
- 大規模なベクトルストレージ(数百万個のベクトル)
- メモリ制約下でのリアルタイム検索
パフォーマンス:
- メモリ:32分の1
- 検索速度:10倍高速(ビット演算)
- 精度:元の95〜98%
2. スカラー量子化(4分の1に削減)
最適な用途:パフォーマンスと精度のバランス、中規模データセット
トレードオフ:精度が約1〜2%低下、メモリを4分の1に削減、3倍高速化
const adapter = await createAgentDBAdapter({
quantizationType: 'scalar',
// 768-dim float32 (3072 bytes) → 768 bytes (uint8)
// 1M vectors: 3GB → 768MB
});
ユースケース:
- 高い精度が求められる本番アプリケーション
- 中規模のデプロイメント(1万〜100万個のベクトル)
- 汎用的な最適化
パフォーマンス:
- メモリ:4分の1
- 検索速度:3倍高速
- 精度:元の98〜99%
3. 積量子化(8〜16分の1に削減)
最適な用途:高次元ベクトル、バランスの取れた圧縮
トレードオフ:精度が約3〜7%低下、メモリを8〜16分の1に削減、5倍高速化
const adapter = await createAgentDBAdapter({
quantizationType: 'product',
// 768-dim float32 (3072 bytes) → 48-96 bytes
// 1M vectors: 3GB → 192MB
});
ユースケース:
- 高次元の埋め込み(512次元超)
- 画像/動画の埋め込み
- 大規模な類似検索
パフォーマンス:
- メモリ:8〜16分の1
- 検索速度:5倍高速
- 精度:元の93〜97%
4. 量子化なし(完全精度)
最適な用途:最大限の精度、小規模なデータセット
トレードオフ:精度の低下なし、メモリをすべて使用
const adapter = await createAgentDBAdapter({
quantizationType: 'none',
// Full float32 precision
});
HNSW インデックス
Hierarchical Navigable Small World — 検索計算量は O(log n)
自動 HNSW
AgentDB は HNSW インデックスを自動的に構築します。
const adapter = await createAgentDBAdapter({
dbPath: '.agentdb$vectors.db',
// HNSW automatically enabled
});
// Search with HNSW (100µs vs 15ms linear scan)
const results = await adapter.retrieveWithReasoning(queryEmbedding, {
k: 10,
});
HNSW パラメーター
// Advanced HNSW configuration
const adapter = await createAgentDBAdapter({
dbPath: '.agentdb$vectors.db',
hnswM: 16, // Connections per layer (default: 16)
hnswEfConstruction: 200, // Build quality (default: 200)
hnswEfSearch: 100, // Search quality (default: 100)
});
パラメーターの調整:
- M(接続数):値が大きいほど再現率が向上し、メモリ使用量が増加します
- 小規模なデータセット(1万未満):M = 8
- 中規模なデータセット(1万〜10万):M = 16
- 大規模なデータセット(10万超):M = 32
- efConstruction:値が大きいほどインデックスの品質が向上し、構築が遅くなります
- 高速構築:100
- バランス重視:200(デフォルト)
- 高品質:400
- efSearch:値が大きいほど再現率が向上し、検索が遅くなります
- 高速検索:50
- バランス重視:100(デフォルト)
- 高再現率:200
キャッシュ戦略
インメモリパターンキャッシュ
const adapter = await createAgentDBAdapter({
cacheSize: 1000, // Cache 1000 most-used patterns
});
// First retrieval: ~2ms (database)
// Subsequent: <1ms (cache hit)
const result = await adapter.retrieveWithReasoning(queryEmbedding, {
k: 10,
});
キャッシュの調整:
- 小規模なアプリケーション:100〜500パターン
- 中規模なアプリケーション:500〜2000パターン
- 大規模なアプリケーション:2000〜5000パターン
LRU キャッシュの動作
// Cache automatically evicts least-recently-used patterns
// Most frequently accessed patterns stay in cache
// Monitor cache performance
const stats = await adapter.getStats();
console.log('Cache Hit Rate:', stats.cacheHitRate);
// Aim for >80% hit rate
バッチ操作
バッチ挿入(500倍高速)
// ❌ SLOW: Individual inserts
for (const doc of documents) {
await adapter.insertPattern({ /* ... */ }); // 1s for 100 docs
}
// ✅ FAST: Batch insert
const patterns = documents.map(doc => ({
id: '',
type: 'document',
domain: 'knowledge',
pattern_data: JSON.stringify({
embedding: doc.embedding,
text: doc.text,
}),
confidence: 1.0,
usage_count: 0,
success_count: 0,
created_at: Date.now(),
last_used: Date.now(),
}));
// Insert all at once (2ms for 100 docs)
for (const pattern of patterns) {
await adapter.insertPattern(pattern);
}
バッチ取得
// Retrieve multiple queries efficiently
const queries = [queryEmbedding1, queryEmbedding2, queryEmbedding3];
// Parallel retrieval
const results = await Promise.all(
queries.map(q => adapter.retrieveWithReasoning(q, { k: 5 }))
);
メモリの最適化
自動統合
// Enable automatic pattern consolidation
const result = await adapter.retrieveWithReasoning(queryEmbedding, {
domain: 'documents',
optimizeMemory: true, // Consolidate similar patterns
k: 10,
});
console.log('Optimizations:', result.optimizations);
// {
// consolidated: 15, // Merged 15 similar patterns
// pruned: 3, // Removed 3 low-quality patterns
// improved_quality: 0.12 // 12% quality improvement
// }
手動最適化
// Manually trigger optimization
await adapter.optimize();
// Get statistics
const stats = await adapter.getStats();
console.log('Before:', stats.totalPatterns);
console.log('After:', stats.totalPatterns); // Reduced by ~10-30%
プルーニング戦略
// Prune low-confidence patterns
await adapter.prune({
minConfidence: 0.5, // Remove confidence < 0.5
minUsageCount: 2, // Remove usage_count < 2
maxAge: 30 * 24 * 3600, // Remove >30 days old
});
パフォーマンスの監視
データベース統計
# Get comprehensive stats
npx agentdb@latest stats .agentdb$vectors.db
# Output:
# Total Patterns: 125,430
# Database Size: 47.2 MB (with binary quantization)
# Avg Confidence: 0.87
# Domains: 15
# Cache Hit Rate: 84%
# Index Type: HNSW
ランタイムメトリクス
const stats = await adapter.getStats();
console.log('Performance Metrics:');
console.log('Total Patterns:', stats.totalPatterns);
console.log('Database Size:', stats.dbSize);
console.log('Avg Confidence:', stats.avgConfidence);
console.log('Cache Hit Rate:', stats.cacheHitRate);
console.log('Search Latency (avg):', stats.avgSearchLatency);
console.log('Insert Latency (avg):', stats.avgInsertLatency);
最適化レシピ
レシピ1:速度を最大化する(精度を犠牲にする)
const adapter = await createAgentDBAdapter({
quantizationType: 'binary', // 32x memory reduction
cacheSize: 5000, // Large cache
hnswM: 8, // Fewer connections = faster
hnswEfSearch: 50, // Low search quality = faster
});
// Expected: <50µs search, 90-95% accuracy
レシピ2:パフォーマンスのバランスを取る
const adapter = await createAgentDBAdapter({
quantizationType: 'scalar', // 4x memory reduction
cacheSize: 1000, // Standard cache
hnswM: 16, // Balanced connections
hnswEfSearch: 100, // Balanced quality
});
// Expected: <100µs search, 98-99% accuracy
レシピ3:精度を最大化する
const adapter = await createAgentDBAdapter({
quantizationType: 'none', // No quantization
cacheSize: 2000, // Large cache
hnswM: 32, // Many connections
hnswEfSearch: 200, // High search quality
});
// Expected: <200µs search, 100% accuracy
レシピ4:メモリ制約下(モバイル/エッジ)
const adapter = await createAgentDBAdapter({
quantizationType: 'binary', // 32x memory reduction
cacheSize: 100, // Small cache
hnswM: 8, // Minimal connections
});
// Expected: <100µs search, ~10MB for 100K vectors
スケーリング戦略
小規模(ベクトル数1万未満)
const adapter = await createAgentDBAdapter({
quantizationType: 'none', // Full precision
cacheSize: 500,
hnswM: 8,
});
中規模(ベクトル数1万〜10万)
const adapter = await createAgentDBAdapter({
quantizationType: 'scalar', // 4x reduction
cacheSize: 1000,
hnswM: 16,
});
大規模(ベクトル数10万〜100万)
const adapter = await createAgentDBAdapter({
quantizationType: 'binary', // 32x reduction
cacheSize: 2000,
hnswM: 32,
});
超大規模(ベクトル数100万超)
const adapter = await createAgentDBAdapter({
quantizationType: 'product', // 8-16x reduction
cacheSize: 5000,
hnswM: 48,
hnswEfConstruction: 400,
});
トラブルシューティング
問題:メモリ使用量が多い
# Check database size
npx agentdb@latest stats .agentdb$vectors.db
# Enable quantization
# Use 'binary' for 32x reduction
問題:検索パフォーマンスが遅い
// Increase cache size
const adapter = await createAgentDBAdapter({
cacheSize: 2000, // Increase from 1000
});
// Reduce search quality (faster)
const result = await adapter.retrieveWithReasoning(queryEmbedding, {
k: 5, // Reduce from 10
});
問題:精度が低い
// Disable or use lighter quantization
const adapter = await createAgentDBAdapter({
quantizationType: 'scalar', // Instead of 'binary'
hnswEfSearch: 200, // Higher search quality
});
Performance Benchmarks
Test System: AMD Ryzen 9 5950X, 64GB RAM
| Operation | Vector Count | No Optimization | Optimized | Improvement |
|---|---|---|---|---|
| Search | 10K | 15ms | 100µs | 150x |
| Search | 100K | 150ms | 120µs | 1,250x |
| Search | 1M | 100s | 8ms | 12,500x |
| Batch Insert (100) | - | 1s | 2ms | 500x |
| Memory Usage | 1M | 3GB | 96MB | 32x (binary) |
Learn More
- Quantization Paper: docs$quantization-techniques.pdf
- HNSW Algorithm: docs$hnsw-index.pdf
- GitHub: https:/$github.com$ruvnet$agentic-flow$tree$main$packages$agentdb
- Website: https:/$agentdb.ruv.io
Category: Performance / Optimization Difficulty: Intermediate Estimated Time: 20-30 minutes