Developer Blog

How to Read and Format Nested SQL Logs for Faster Debugging

Raw SQL logs become difficult to debug when an ORM prints joins, filters, and parameters on one long line. The fastest workflow is to split the query structure, inspect embedded JSON values, and verify that filters match the expected request.

Published May 10, 2026 Updated By Netvibes Infotech Ltd.
SQL logs ORM debugging Django database queries developer tools
Quick Answer

Raw SQL logs become difficult to debug when an ORM prints joins, filters, and parameters on one long line. The fastest workflow is to split the query structure, inspect embedded JSON values, and verify that filters match the expected request.

Raw SQL logs become difficult to debug when an ORM prints joins, filters, and parameters on one long line. The fastest workflow is to split the query structure, inspect embedded JSON values, and verify that filters match the expected request.

Direct answer

When a SQL log is unreadable, do not start by rewriting the query. First format the structure mentally into SELECT, FROM, JOIN, WHERE, ORDER BY, and LIMIT blocks. Then inspect any embedded JSON, IDs, or dynamic filters separately.

ScriptZip currently helps with the adjacent parts of this workflow. Use JSON Formatter for JSON parameters inside logs and Regex Tester to isolate repeated IDs, emails, slugs, or query fragments.

Unreadable ORM log example

SELECT users.id,users.email,orders.id,orders.total FROM users INNER JOIN orders ON users.id=orders.user_id LEFT JOIN order_items ON orders.id=order_items.order_id WHERE users.is_active=1 AND orders.status='paid' AND orders.meta='{"source":"web","coupon":"SPRING"}' ORDER BY orders.created_at DESC LIMIT 50;

The query is valid, but it is not easy to read during an incident.

Readable version

SELECT
    users.id,
    users.email,
    orders.id,
    orders.total
FROM users
INNER JOIN orders
    ON users.id = orders.user_id
LEFT JOIN order_items
    ON orders.id = order_items.order_id
WHERE users.is_active = 1
  AND orders.status = 'paid'
  AND orders.meta = '{"source":"web","coupon":"SPRING"}'
ORDER BY orders.created_at DESC
LIMIT 50;

Debugging checklist

  • Confirm whether the query is slow or only hard to read.
  • Check whether the WHERE clause uses indexed columns.
  • Look for accidental joins that multiply rows.
  • Format JSON parameters separately before assuming they are valid.
  • Compare the ORM filter call with the SQL output.

Django example

queryset = Order.objects.filter(
    user__is_active=True,
    status="paid",
).select_related("user").prefetch_related("items")

print(queryset.query)

This prints SQL, but not always in a format that is friendly for debugging. Keep a formatted copy in your incident notes so the team can quickly review the query path.

FAQ

Can formatting SQL change the query?

Formatting should not change the query if only whitespace and line breaks are changed. Be careful not to edit quotes, operators, or parameter values.

Why do ORM queries become so long?

ORMs often expand relationships, selected columns, filters, annotations, and ordering into one final SQL string.

Useful ScriptZip tools for this workflow