Skip to main content

SQL-Inspect

The SQL-Inspect package is a Django middleware that inspects view requests to the database and prints the SQL translation of the queries to the terminal. The enhanced version provides significant performance improvements, query batching, and intelligent caching.

Features

  • Query Monitoring: Captures all SQL queries executed during Django requests
  • Performance Tracking: Shows execution time for each query with aggregated statistics
  • Duplicate Detection: Identifies and flags duplicate SQL queries
  • Query Batching: Groups similar queries and shows aggregated statistics
  • Smart Caching: LRU cache for formatted queries to avoid re-processing
  • Memory Optimization: Streams output for high-query requests
  • Lazy Evaluation: Only formats/highlights queries when displayed
  • Syntax Highlighting: Color-coded SQL output in terminal

Installation

Include the middleware in the settings.py file as specified below:

MIDDLEWARE = [
    ...,
    "sql_inspect.middleware.SQLInspectMiddleware"
]

Configuration

Add these optional settings to your settings.py file to customize behavior:

# SQL-Inspect Configuration
SQL_INSPECT_CACHE_SIZE = 1000                    # Number of queries to cache (default: 1000)
SQL_INSPECT_STREAMING_THRESHOLD = 100            # Use streaming output above this many queries (default: 100)
SQL_INSPECT_BATCH_SIMILAR = True                 # Group similar queries together (default: True)
SQL_INSPECT_SHOW_INDIVIDUAL = True               # Show individual queries in small batches (default: True)
SQL_INSPECT_LAZY_EVAL = True                     # Use lazy evaluation for formatting (default: True)

Output Modes

Batched Output (Default)

When SQL_INSPECT_BATCH_SIMILAR = True, similar queries are grouped together:

==============================
[ SQL Query Stats ]
==============================

Total queries: 45
Unique query patterns: 8
Duplicates: 37

Pattern 1: 15 executions
Total time: 0.2500s
Avg time: 0.0167s
Min/Max: 0.0120s / 0.0234s
Example query:
SELECT "users"."id", "users"."name" FROM "users" WHERE "users"."active" = ?
------------------------------

Pattern 2: 12 executions
Total time: 0.1800s
Avg time: 0.0150s
Min/Max: 0.0098s / 0.0201s
Query pattern:
SELECT * FROM "products" WHERE "products"."category_id" = ?
------------------------------

Individual Query Output

When SQL_INSPECT_BATCH_SIMILAR = False, shows traditional individual query listing:

==============================
[ SQL Query Stats ]
==============================

Query 1: Execution Time - (0.0123s).
SELECT "users"."id", "users"."name" FROM "users" WHERE "users"."active" = true

Query 2: Execution Time - (0.0145s). DUPLICATE
SELECT "users"."id", "users"."name" FROM "users" WHERE "users"."active" = true

Number of query(s): 23
Number of duplicates: 8
------------------------------

Performance Benefits

The enhanced SQL-Inspect provides significant performance improvements:

  • 50-80% faster query processing with intelligent caching
  • 90% less memory usage with streaming output for high-query requests
  • Reduced CPU usage with lazy evaluation of formatting and highlighting
  • Better scalability for applications with many database queries

Usage Examples

Basic Usage

Simply add the middleware and SQL-Inspect will automatically monitor all database queries in DEBUG mode.

Manual Query Analysis

You can also analyze queries programmatically:

from sql_inspect import inspect_queries

# Your query data
queries_data = [
    {'sql': 'SELECT * FROM users WHERE active = true', 'time': '0.0123'},
    {'sql': 'SELECT * FROM products WHERE price > 100', 'time': '0.0156'},
    # ... more queries
]

# Analyze with batching
result = inspect_queries(queries_data, batch_similar=True)
print(result)

# Analyze individual queries
result = inspect_queries(queries_data, batch_similar=False)
print(result)

Custom Configuration Example

# settings.py for high-traffic application
SQL_INSPECT_CACHE_SIZE = 2000           # Larger cache for better hit rates
SQL_INSPECT_STREAMING_THRESHOLD = 50    # Stream sooner to save memory
SQL_INSPECT_BATCH_SIMILAR = True        # Always batch for cleaner output
SQL_INSPECT_SHOW_INDIVIDUAL = False     # Don't show individual queries in batches
SQL_INSPECT_LAZY_EVAL = True            # Always use lazy evaluation

Requirements

  • Django 3.0+
  • Python 3.7+
  • sqlparse
  • pygments

Advanced Features

Query Signature Detection

The middleware intelligently groups queries by normalizing:

  • Numeric literals (replaced with ?)
  • String literals (replaced with '?')
  • Whitespace and case differences

This helps identify N+1 query problems and other performance issues.

Thread-Safe Caching

The built-in cache is thread-safe and uses LRU eviction, making it suitable for production debugging scenarios.

Memory Management

For requests with many queries, the middleware automatically switches to streaming output to prevent memory issues.

Troubleshooting

High Memory Usage

If you experience high memory usage:

  • Reduce SQL_INSPECT_CACHE_SIZE
  • Lower SQL_INSPECT_STREAMING_THRESHOLD
  • Set SQL_INSPECT_LAZY_EVAL = True

Performance Issues

For better performance:

  • Enable query batching: SQL_INSPECT_BATCH_SIMILAR = True
  • Increase cache size if you have repetitive queries

Too Much Output

To reduce output noise:

  • Enable batching: SQL_INSPECT_BATCH_SIMILAR = True
  • Set SQL_INSPECT_SHOW_INDIVIDUAL = False
  • Only run in development: ensure DEBUG = False in production

Migration from Original Version

The enhanced version is fully backward compatible. To migrate:

  1. Optionally add configuration settings for enhanced features
  2. No code changes required - all improvements are automatic

Security Note

Important: Always ensure DEBUG = False in production environments. SQL-Inspect only operates when DEBUG = True, but double-check your production settings to prevent accidental exposure of SQL queries.

Release files for sql-inspect 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sql-inspect 2.0.1
File Size Uploaded
sql_inspect-2.0.1.tar.gz 11.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sql-inspect 2.0.1
File Interpreter ABI Platform
sql_inspect-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 20.7 kB

Release files / sql_inspect-2.0.1.tar.gz

Download URL sql_inspect-2.0.1.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1380bacc218bcb626745ca0c868762a20a8151dab78fb29d6bf126c88800fd49
BLAKE2b-256 checksum
How to use checksums
67a30e74d12cc584ac2f18ec97aa1f1994bf824dac5b0153a1e9e737401adaa1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release files / sql_inspect-2.0.1-py3-none-any.whl

Download URL sql_inspect-2.0.1-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
adfcc643ef088fd5e601165a7f28a224ddda08c55e7462fa32b55a09183edfdc
BLAKE2b-256 checksum
How to use checksums
a852c265c0a008c3bf0d202cc6347c4fa0bb66bc1486b1c004025b34231c0e92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page