liveBook · Manning

Query analysis

As explained in Fast by Default

Query analysis is the examination of database queries and their execution behavior to identify inefficient or slow database operations.

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 result

The Python listing demonstrates request identifiers, database-phase timing, processing timing, total timing, and structured logging for query-analysis triage. section 7.2.1

How Fast by Default covers it

How deep it goes
Explains phase-level timing and triage but stops short of query-level optimization or database internals.
Examples
Uses contrasting timing records and Python instrumentation to expose database-dominated requests.
What it assumes
Assumes basic request timing and logging, while demonstrating how to separate database and processing phases.
What you can do after
Diagnose which requests justify deeper database investigation by comparing timed request phases.
How it connects
Connects query analysis with request tracing, logs, metrics, response time, and database performance.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast by Default. The section links point to the parts of the book it draws on.