Query analysis examines database work when request tracing shows that database time dominates the response. It narrows a broad performance complaint into a database-focused investigation rather than treating every slow request as application-code inefficiency. section 7.2.1
Find the database-dominated request
A healthy timing record reports total=45ms, db=12ms, and process=30ms. A problematic record reports total=1820ms, db=1780ms, and process=35ms. The second pattern makes database operations the next area to examine. section 7.2.1
Use request-level evidence to focus query analysis:
- Break the request into phases and time database work separately. section 7.2.1
- Compare database time with processing and total response time. section 7.2.1
- Send timing data to metrics so slow-response patterns and outliers trigger attention. section 7.2.1
Listing 7.1 Simple request timing instrumentation in Python
import time
import uuid
def handle_request(request):
request_id = str(uuid.uuid4())[:8] #A
start = time.time() #B
db_start = time.time()
users = fetch_users(request.filters)
db_time = time.time() - db_start
process_start = time.time()
result = process_users(users)
process_time = time.time() - process_start
total_time = time.time() - start
logger.info( #C
f"[{request_id}] total={total_time*1000:.0f}ms "
f"db={db_time*1000:.0f}ms process={process_time*1000:.0f}ms"
)
return resultThe Python listing demonstrates request identifiers, database-phase timing, processing timing, total timing, and structured logging for query-analysis triage. section 7.2.1
