Spaces:
Sleeping
Sleeping
File size: 12,327 Bytes
79ca9ba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | """
Query optimization and streaming utilities for MongoDB operations.
Implements cursor-based pagination and memory-efficient query execution.
"""
import asyncio
import logging
from typing import Dict, List, Any, Optional, AsyncGenerator, Tuple
from datetime import datetime
import pymongo
from app.nosql import db
from app.utils.simple_log_sanitizer import get_simple_sanitized_logger
logger = get_simple_sanitized_logger(__name__)
class QueryOptimizer:
"""Optimizes MongoDB queries for better performance and memory usage"""
def __init__(self):
self.query_cache = {}
self.cache_ttl = 300 # 5 minutes
def optimize_pipeline(self, pipeline: List[Dict]) -> List[Dict]:
"""Optimize aggregation pipeline for better performance"""
optimized = []
match_stages = []
other_stages = []
# Separate $match stages from other stages
for stage in pipeline:
if "$match" in stage:
match_stages.append(stage)
else:
other_stages.append(stage)
# Combine multiple $match stages into one
if len(match_stages) > 1:
combined_match = {"$match": {}}
for match_stage in match_stages:
combined_match["$match"].update(match_stage["$match"])
optimized.append(combined_match)
elif match_stages:
optimized.extend(match_stages)
# Add other stages
optimized.extend(other_stages)
# Ensure $match comes first for index utilization
final_pipeline = []
match_added = False
for stage in optimized:
if "$match" in stage and not match_added:
final_pipeline.insert(0, stage)
match_added = True
elif "$match" not in stage:
final_pipeline.append(stage)
return final_pipeline
def add_index_hints(self, pipeline: List[Dict], collection_name: str) -> List[Dict]:
"""Add index hints to optimize query execution"""
# Note: $hint is not available in aggregation pipeline
# Index hints are applied at the collection.aggregate() level
# This method is kept for future enhancement but currently returns pipeline as-is
return pipeline
async def execute_optimized_query(
self,
collection_name: str,
pipeline: List[Dict],
limit: Optional[int] = None,
use_cursor: bool = True
) -> List[Dict]:
"""Execute optimized query with optional cursor-based streaming"""
try:
# Optimize the pipeline
optimized_pipeline = self.optimize_pipeline(pipeline)
collection = db[collection_name]
if use_cursor and limit and limit > 100:
# Use cursor for large result sets
return await self._execute_with_cursor(collection, optimized_pipeline, limit)
else:
# Use regular aggregation for small result sets
results = await collection.aggregate(optimized_pipeline).to_list(length=limit)
return results
except Exception as e:
logger.error(f"Error executing optimized query on {collection_name}: {e}")
# Fallback to original pipeline if optimization fails
try:
logger.info(f"Falling back to original pipeline for {collection_name}")
collection = db[collection_name]
results = await collection.aggregate(pipeline).to_list(length=limit)
return results
except Exception as fallback_error:
logger.error(f"Fallback query also failed for {collection_name}: {fallback_error}")
raise fallback_error
async def _execute_with_cursor(
self,
collection,
pipeline: List[Dict],
limit: int,
batch_size: int = 100
) -> List[Dict]:
"""Execute query using cursor-based pagination to manage memory"""
results = []
processed = 0
# Add batch processing to pipeline
cursor = collection.aggregate(pipeline, batchSize=batch_size)
async for document in cursor:
results.append(document)
processed += 1
if processed >= limit:
break
# Yield control periodically to prevent blocking
if processed % batch_size == 0:
await asyncio.sleep(0) # Yield to event loop
return results
async def stream_query_results(
self,
collection_name: str,
pipeline: List[Dict],
batch_size: int = 100
) -> AsyncGenerator[List[Dict], None]:
"""Stream query results in batches to manage memory usage"""
optimized_pipeline = self.optimize_pipeline(pipeline)
collection = db[collection_name]
try:
cursor = collection.aggregate(optimized_pipeline, batchSize=batch_size)
batch = []
async for document in cursor:
batch.append(document)
if len(batch) >= batch_size:
yield batch
batch = []
await asyncio.sleep(0) # Yield to event loop
# Yield remaining documents
if batch:
yield batch
except Exception as e:
logger.error(f"Error streaming query results from {collection_name}")
raise
async def execute_paginated_query(
self,
collection_name: str,
pipeline: List[Dict],
page_size: int = 20,
cursor_field: str = "_id",
cursor_value: Optional[Any] = None,
sort_direction: int = 1
) -> Tuple[List[Dict], Optional[Any]]:
"""Execute cursor-based paginated query"""
# Add cursor-based pagination to pipeline
paginated_pipeline = pipeline.copy()
# Add cursor filter if provided
if cursor_value is not None:
cursor_filter = {
cursor_field: {"$gt" if sort_direction == 1 else "$lt": cursor_value}
}
# Add to existing $match or create new one
match_added = False
for stage in paginated_pipeline:
if "$match" in stage:
stage["$match"].update(cursor_filter)
match_added = True
break
if not match_added:
paginated_pipeline.insert(0, {"$match": cursor_filter})
# Add sort and limit
paginated_pipeline.extend([
{"$sort": {cursor_field: sort_direction}},
{"$limit": page_size + 1} # Get one extra to check if there are more
])
# Execute query
results = await self.execute_optimized_query(
collection_name,
paginated_pipeline,
limit=page_size + 1,
use_cursor=False
)
# Determine next cursor
next_cursor = None
if len(results) > page_size:
next_cursor = results[-1].get(cursor_field)
results = results[:-1] # Remove the extra document
return results, next_cursor
def get_query_stats(self) -> Dict[str, Any]:
"""Get query optimization statistics"""
return {
"cache_size": len(self.query_cache),
"cache_ttl": self.cache_ttl,
"optimizations_applied": [
"Pipeline stage reordering",
"Multiple $match stage combination",
"Index hint addition",
"Cursor-based pagination",
"Memory-efficient streaming"
]
}
class MemoryEfficientAggregator:
"""Memory-efficient aggregation operations"""
def __init__(self, max_memory_mb: int = 100):
self.max_memory_mb = max_memory_mb
self.batch_size = 1000
async def aggregate_with_memory_limit(
self,
collection_name: str,
pipeline: List[Dict],
max_results: int = 10000
) -> List[Dict]:
"""Aggregate with memory usage monitoring"""
collection = db[collection_name]
results = []
processed = 0
# Add allowDiskUse for large aggregations
cursor = collection.aggregate(
pipeline,
allowDiskUse=True,
batchSize=self.batch_size
)
try:
async for document in cursor:
results.append(document)
processed += 1
# Check memory usage periodically
if processed % self.batch_size == 0:
import psutil
memory_usage = psutil.Process().memory_info().rss / 1024 / 1024 # MB
if memory_usage > self.max_memory_mb:
logger.warning(f"Memory usage ({memory_usage:.1f}MB) exceeds limit ({self.max_memory_mb}MB)")
break
await asyncio.sleep(0) # Yield to event loop
if processed >= max_results:
break
logger.info(f"Processed {processed} documents with memory-efficient aggregation")
return results
except Exception as e:
logger.error(f"Error in memory-efficient aggregation: {e}")
raise
async def count_with_timeout(
self,
collection_name: str,
filter_criteria: Dict,
timeout_seconds: int = 30
) -> int:
"""Count documents with timeout to prevent long-running operations"""
collection = db[collection_name]
try:
# Use asyncio.wait_for to add timeout
count = await asyncio.wait_for(
collection.count_documents(filter_criteria),
timeout=timeout_seconds
)
return count
except asyncio.TimeoutError:
logger.warning(f"Count operation timed out after {timeout_seconds}s")
# Return estimated count using aggregation
pipeline = [
{"$match": filter_criteria},
{"$count": "total"}
]
result = await collection.aggregate(pipeline).to_list(length=1)
return result[0]["total"] if result else 0
except Exception as e:
logger.error(f"Error counting documents: {e}")
return 0
# Global instances
query_optimizer = QueryOptimizer()
memory_aggregator = MemoryEfficientAggregator()
async def execute_optimized_aggregation(
collection_name: str,
pipeline: List[Dict],
limit: Optional[int] = None,
use_streaming: bool = False
) -> List[Dict]:
"""Execute optimized aggregation with automatic optimization and fallback"""
try:
if use_streaming and limit and limit > 1000:
# Use streaming for large result sets
results = []
async for batch in query_optimizer.stream_query_results(collection_name, pipeline):
results.extend(batch)
if len(results) >= limit:
results = results[:limit]
break
return results
else:
# Use regular optimized query
return await query_optimizer.execute_optimized_query(
collection_name,
pipeline,
limit=limit,
use_cursor=False # Disable cursor for now to avoid complexity
)
except Exception as e:
logger.error(f"Optimized aggregation failed for {collection_name}: {e}")
# Final fallback - direct database call
collection = db[collection_name]
results = await collection.aggregate(pipeline).to_list(length=limit)
return results |