Loading Techie Utils…

SQL Minifier

Collapse formatted SQL into compact text. Free, and runs entirely in your browser — nothing you enter is uploaded, stored or sent to a server.

About SQL Minifier

The opposite trip: a formatted query squeezed onto as few characters as SQL allows. That is worth doing when a query has to travel somewhere that counts characters — a configuration value, a URL, a JSON field, a dashboard definition, a log line that would otherwise wrap over twelve rows.

Comments go, because that is where most of the size is and none of the meaning. Whitespace goes wherever two tokens do not need separating, which is decided by what the tokens are rather than by a regular expression over the text — a-b and a - b mean the same thing and - -b does not.

What never goes is anything inside a string, a quoted identifier or a dollar-quoted block. Whitespace in those is data, and a minifier that collapsed it would change results rather than formatting. A line comment, if you keep comments, still forces a newline, because it would otherwise swallow the rest of the statement.

Where it helps

  • Never collapses whitespace inside a string or a quoted identifier, where it is data rather than formatting.
  • Decides spacing from what the tokens are, so operators do not run together into a different operator.
  • Understands the dialect's comment and quoting rules, including MySQL # comments and PostgreSQL dollar quotes.
  • Reports what it actually saved, which is the question a minifier is being asked.
  • Can keep comments where they are part of the query — an optimiser hint, for example.

Limits

  • It does not rewrite or optimise the query; it only removes characters that carry no meaning.
  • A minified query is harder to read, which is the trade — keep the formatted original wherever it is the one being maintained.
  • It cannot validate the SQL, so a query that was already broken stays broken and gets shorter.

Frequently asked questions

Is it safe to run the minified query?

It should behave identically: only whitespace between tokens is removed, and quoted content is untouched. The exception is a query that was already invalid, where there is no correct transformation to make.

Why does a comment still produce a line break?

Because a -- or # comment runs to the end of the line. Putting anything after it on the same line would comment that out too, so keeping comments means keeping the newline that ends them.

Does this make my query faster?

No. The database parses the query either way and the plan is identical. What it saves is characters in whatever is carrying the query around.

Related tools

More Web Development tools