Spaces:
Runtime error
Runtime error
File size: 16,946 Bytes
e6be672 | 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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | # Copyright (C) 2026 Mattia Zatelli
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
Query Optimization Utilities
Provides query optimization patterns for improved performance:
- Query result caching
- Batch query execution
- Index hints
- Query plan analysis
- Connection pooling optimization
"""
import logging
import hashlib
import json
from typing import Any, Dict, List, Optional, Callable
from datetime import datetime, timedelta
from functools import wraps
import asyncio
from sqlalchemy import text, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload, joinedload
logger = logging.getLogger(__name__)
class QueryCache:
"""In-memory cache for query results with TTL support"""
def __init__(self, default_ttl: int = 300):
"""
Initialize query cache
Args:
default_ttl: Default time-to-live in seconds (default: 5 minutes)
"""
self._cache: Dict[str, Dict[str, Any]] = {}
self._default_ttl = default_ttl
logger.info(f"Initialized QueryCache with TTL={default_ttl}s")
def _generate_key(self, query: str, params: Dict[str, Any]) -> str:
"""Generate cache key from query and parameters"""
cache_data = f"{query}:{json.dumps(params, sort_keys=True)}"
return hashlib.md5(cache_data.encode()).hexdigest()
def get(self, query: str, params: Dict[str, Any] = None) -> Optional[Any]:
"""
Get cached result if available and not expired
Args:
query: Query string
params: Query parameters
Returns:
Cached result or None if not found/expired
"""
params = params or {}
key = self._generate_key(query, params)
if key in self._cache:
cache_entry = self._cache[key]
if datetime.now() < cache_entry["expires_at"]:
logger.debug(f"Cache HIT for key: {key[:8]}...")
return cache_entry["result"]
else:
# Expired - remove from cache
logger.debug(f"Cache EXPIRED for key: {key[:8]}...")
del self._cache[key]
logger.debug(f"Cache MISS for key: {key[:8]}...")
return None
def set(
self,
query: str,
params: Dict[str, Any],
result: Any,
ttl: Optional[int] = None
):
"""
Store result in cache
Args:
query: Query string
params: Query parameters
result: Query result to cache
ttl: Time-to-live in seconds (uses default if not specified)
"""
params = params or {}
key = self._generate_key(query, params)
ttl = ttl or self._default_ttl
self._cache[key] = {
"result": result,
"expires_at": datetime.now() + timedelta(seconds=ttl),
"created_at": datetime.now()
}
logger.debug(f"Cached result for key: {key[:8]}... (TTL={ttl}s)")
def clear(self):
"""Clear all cached entries"""
count = len(self._cache)
self._cache.clear()
logger.info(f"Cleared {count} cached entries")
def get_stats(self) -> Dict[str, Any]:
"""Get cache statistics"""
total = len(self._cache)
expired = sum(
1 for entry in self._cache.values()
if datetime.now() >= entry["expires_at"]
)
return {
"total_entries": total,
"active_entries": total - expired,
"expired_entries": expired,
"memory_estimate_mb": total * 0.01 # Rough estimate
}
# Global cache instance
query_cache = QueryCache(default_ttl=300)
def cached_query(ttl: int = 300):
"""
Decorator for caching query results
Args:
ttl: Cache time-to-live in seconds
Example:
@cached_query(ttl=600)
async def get_elements(session, project_id):
return await session.execute(query)
"""
def decorator(func: Callable) -> Callable:
@wraps(func)
async def wrapper(*args, **kwargs):
# Generate cache key from function name and arguments
cache_key = f"{func.__name__}:{args}:{kwargs}"
# Check cache
cached_result = query_cache.get(cache_key, {})
if cached_result is not None:
return cached_result
# Execute function
result = await func(*args, **kwargs)
# Cache result
query_cache.set(cache_key, {}, result, ttl=ttl)
return result
return wrapper
return decorator
class QueryOptimizer:
"""Query optimization utilities"""
@staticmethod
async def batch_fetch_elements(
session: AsyncSession,
element_ids: List[int],
batch_size: int = 100
) -> List[Any]:
"""
Fetch elements in batches to avoid large IN clauses
Args:
session: Database session
element_ids: List of element IDs to fetch
batch_size: Number of IDs per batch
Returns:
List of elements
"""
from app.models.element import Element
results = []
for i in range(0, len(element_ids), batch_size):
batch_ids = element_ids[i:i + batch_size]
query = select(Element).where(Element.id.in_(batch_ids))
batch_result = await session.execute(query)
results.extend(batch_result.scalars().all())
logger.info(
f"Batch fetched {len(results)} elements in "
f"{(len(element_ids) + batch_size - 1) // batch_size} batches"
)
return results
@staticmethod
async def eager_load_relationships(
session: AsyncSession,
query,
relationships: List[str]
):
"""
Add eager loading for relationships to avoid N+1 queries
Args:
session: Database session
query: SQLAlchemy query
relationships: List of relationship names to eager load
Returns:
Modified query with eager loading
"""
for relationship in relationships:
# Use selectinload for better performance with collections
query = query.options(selectinload(relationship))
return query
@staticmethod
def optimize_query_for_count(query) -> Any:
"""
Optimize query for count operations
Args:
query: SQLAlchemy query
Returns:
Optimized query for counting
"""
from sqlalchemy import func
# Use count(*) which is faster than count(column)
return select(func.count()).select_from(query.subquery())
@staticmethod
async def parallel_queries(
session: AsyncSession,
queries: List[Any]
) -> List[Any]:
"""
Execute multiple queries in parallel
Args:
session: Database session
queries: List of SQLAlchemy queries
Returns:
List of query results
"""
tasks = [session.execute(query) for query in queries]
results = await asyncio.gather(*tasks)
logger.info(f"Executed {len(queries)} queries in parallel")
return results
@staticmethod
def add_index_hints(query, table_name: str, index_name: str):
"""
Add index hints for query optimization (PostgreSQL)
Args:
query: SQLAlchemy query
table_name: Table name
index_name: Index name to hint
Returns:
Query with index hint
"""
# Note: PostgreSQL doesn't support index hints like MySQL
# This is a placeholder for documentation purposes
logger.warning(
f"Index hints not supported in PostgreSQL. "
f"Consider using EXPLAIN ANALYZE instead."
)
return query
class BatchProcessor:
"""Batch processing utilities for large datasets"""
@staticmethod
async def process_in_batches(
session: AsyncSession,
query,
batch_size: int = 1000,
processor: Callable = None
) -> List[Any]:
"""
Process query results in batches to reduce memory usage
Args:
session: Database session
query: SQLAlchemy query
batch_size: Number of records per batch
processor: Optional processing function for each batch
Returns:
Aggregated results from all batches
"""
results = []
offset = 0
while True:
# Fetch batch
batch_query = query.limit(batch_size).offset(offset)
batch_result = await session.execute(batch_query)
batch = batch_result.scalars().all()
if not batch:
break
# Process batch if processor provided
if processor:
processed = await processor(batch) if asyncio.iscoroutinefunction(processor) else processor(batch)
results.extend(processed if isinstance(processed, list) else [processed])
else:
results.extend(batch)
offset += batch_size
logger.debug(f"Processed batch at offset {offset}")
logger.info(f"Processed {len(results)} records in batches of {batch_size}")
return results
@staticmethod
async def parallel_batch_processing(
session: AsyncSession,
queries: List[Any],
batch_size: int = 1000,
max_workers: int = 5
) -> List[List[Any]]:
"""
Process multiple queries in parallel batches
Args:
session: Database session
queries: List of queries to process
batch_size: Batch size for each query
max_workers: Maximum parallel workers
Returns:
List of results for each query
"""
# Limit concurrent tasks
semaphore = asyncio.Semaphore(max_workers)
async def process_with_semaphore(query):
async with semaphore:
return await BatchProcessor.process_in_batches(
session, query, batch_size
)
tasks = [process_with_semaphore(query) for query in queries]
results = await asyncio.gather(*tasks)
logger.info(
f"Processed {len(queries)} queries in parallel "
f"with {max_workers} workers"
)
return results
class PerformanceMonitor:
"""Monitor and log query performance"""
def __init__(self):
self._query_times: Dict[str, List[float]] = {}
def log_query_time(self, query_name: str, duration: float):
"""
Log query execution time
Args:
query_name: Name/identifier for the query
duration: Execution time in seconds
"""
if query_name not in self._query_times:
self._query_times[query_name] = []
self._query_times[query_name].append(duration)
# Log warning if query is slow
if duration > 1.0:
logger.warning(
f"Slow query detected: {query_name} took {duration:.2f}s"
)
def get_statistics(self) -> Dict[str, Dict[str, float]]:
"""
Get performance statistics for all queries
Returns:
Dictionary with query statistics
"""
stats = {}
for query_name, times in self._query_times.items():
if times:
stats[query_name] = {
"count": len(times),
"total_time": sum(times),
"avg_time": sum(times) / len(times),
"min_time": min(times),
"max_time": max(times)
}
return stats
def clear_statistics(self):
"""Clear all performance statistics"""
self._query_times.clear()
# Global performance monitor
performance_monitor = PerformanceMonitor()
def monitor_performance(query_name: str = None):
"""
Decorator to monitor query performance
Args:
query_name: Optional name for the query (uses function name if not provided)
Example:
@monitor_performance("get_elements")
async def get_elements(session):
return await session.execute(query)
"""
def decorator(func: Callable) -> Callable:
name = query_name or func.__name__
@wraps(func)
async def wrapper(*args, **kwargs):
start_time = datetime.now()
try:
result = await func(*args, **kwargs)
return result
finally:
duration = (datetime.now() - start_time).total_seconds()
performance_monitor.log_query_time(name, duration)
return wrapper
return decorator
class ConnectionPoolOptimizer:
"""Optimize database connection pool settings"""
@staticmethod
def get_optimal_pool_size(
max_concurrent_requests: int = 100,
avg_query_time_ms: float = 50
) -> Dict[str, int]:
"""
Calculate optimal connection pool size
Args:
max_concurrent_requests: Maximum expected concurrent requests
avg_query_time_ms: Average query time in milliseconds
Returns:
Dictionary with recommended pool settings
"""
# Rule of thumb: pool_size = concurrent_requests * (query_time / request_time)
# Assuming 1 request = 2-3 queries on average
queries_per_request = 2.5
min_pool_size = max(5, int(max_concurrent_requests * 0.1))
max_pool_size = max(20, int(max_concurrent_requests * queries_per_request * 0.5))
overflow_size = int(max_pool_size * 0.5)
return {
"pool_size": min_pool_size,
"max_overflow": overflow_size,
"pool_pre_ping": True, # Verify connections before use
"pool_recycle": 3600, # Recycle connections after 1 hour
"echo_pool": False # Don't log pool checkouts
}
# Utility functions for common optimization patterns
async def fetch_with_cache(
session: AsyncSession,
query,
cache_key: str,
ttl: int = 300
) -> Any:
"""
Execute query with caching
Args:
session: Database session
query: SQLAlchemy query
cache_key: Cache key
ttl: Cache TTL in seconds
Returns:
Query result (cached or fresh)
"""
# Check cache
cached_result = query_cache.get(cache_key, {})
if cached_result is not None:
return cached_result
# Execute query
result = await session.execute(query)
data = result.scalars().all()
# Cache result
query_cache.set(cache_key, {}, data, ttl=ttl)
return data
async def fetch_with_pagination(
session: AsyncSession,
query,
page: int = 1,
page_size: int = 50
) -> Dict[str, Any]:
"""
Execute query with pagination
Args:
session: Database session
query: SQLAlchemy query
page: Page number (1-indexed)
page_size: Results per page
Returns:
Dictionary with paginated results and metadata
"""
from sqlalchemy import func, select
# Get total count
count_query = select(func.count()).select_from(query.subquery())
total = await session.scalar(count_query)
# Calculate pagination
offset = (page - 1) * page_size
total_pages = (total + page_size - 1) // page_size
# Fetch page
paginated_query = query.limit(page_size).offset(offset)
result = await session.execute(paginated_query)
items = result.scalars().all()
return {
"items": items,
"page": page,
"page_size": page_size,
"total_items": total,
"total_pages": total_pages,
"has_next": page < total_pages,
"has_prev": page > 1
}
# Export optimization utilities
__all__ = [
"QueryCache",
"query_cache",
"cached_query",
"QueryOptimizer",
"BatchProcessor",
"PerformanceMonitor",
"performance_monitor",
"monitor_performance",
"ConnectionPoolOptimizer",
"fetch_with_cache",
"fetch_with_pagination"
] |