Home Text Tools Math & Numbers Developer Tools Date & Time Image Tools Blog
SQL Formatter Online

Make Sense of One-Line SQL from Slow Query Logs and ORMs

Slow-query logs and ORMs produce SQL on a single line. Format it to find joins, filters and the reason a query is slow.

A slow-query log shows a statement as one very long line with no breaks. ORMs do the same with generated SQL. Before you can judge whether the query is wasteful, you need to see its shape, and formatting does that in a moment.

SQL Formatter Online FREE online toolRuns in your browser. No sign-up, nothing to install. Open the tool

What formatting reveals

Once each clause sits on its own line, patterns pop out. You can see which tables are joined and on what conditions, whether a WHERE filter uses a function on a column, and how many columns are selected. ORM-generated queries often list every column of every table, with aliases like t1.col2 that are hard to read until they are broken apart.

Capitalized keywords and clauses on separate lines also make diffs useful. When a teammate changes one condition, a formatted query shows a change in one clause instead of a whole changed line, which makes review in version control much easier.

Walk through it

  1. Copy the statement from the log. Remove any prefix such as timestamps or duration lines.
  2. Paste it into the SQL Formatter and click Format.
  3. Read the FROM and JOIN lines first to understand the tables involved.
  4. Look at the WHERE clause for columns used in comparisons, then check whether those columns have indexes.
  5. Check ORDER BY and LIMIT, since sorting a large unindexed set is a common slowdown.
  6. Run EXPLAIN in your database on the formatted query to see the real plan.

Things worth spotting

Formatting does not make the query faster, but it makes the warning signs visible:

Do not judge a query by length alone. A long statement with proper indexes can run in milliseconds, and a short one that scans a large table can hang the database. Use the log's reported duration and rows examined alongside what you see in the formatted text to decide where to look first.

  • SELECT * pulling more columns than the code uses.
  • A function wrapped around an indexed column, such as LOWER(email) = ..., which can prevent index use.
  • A LIKE pattern that starts with a wildcard.
  • Joins missing an ON condition, which can multiply rows.
  • OR conditions across different columns.
  • Large IN lists generated by loops, a sign of an N+1 pattern in the ORM.

Parameters and placeholders

Logs often show placeholders such as ? or $1 with the real values logged separately. Format the query with the placeholders in place, then mentally substitute the values. If you need exact values to run EXPLAIN, substitute them yourself.

Be careful when pasting production queries into any tool. Literal values can contain emails, tokens or customer data. Strip or replace them before sharing the query with colleagues or in a ticket.

After formatting

Save the formatted version in the issue you are working on, so everyone sees the same layout. If the problem comes from an ORM call, trace it back to the code that generates it. Often the fix is to select fewer columns, add an index or batch the queries, not to rewrite the SQL by hand.

Keep the original unformatted statement too. The formatter collapses runs of whitespace, including inside string literals, so it is worth keeping the source for comparison when you are chasing a subtle bug.

Try the free SQL Formatter Online
Runs in your browser. No sign-up, nothing to install.

Comments (0)

No comments yet. Be the first to share a thought.