How to minify SQL without changing what it does
By Hammad Rasool · November 11, 2026
Quick answer: minifying SQL means removing comments, line breaks and extra spaces outside strings, so a query fits on one line in a JSON file, an environment variable, a log line or a string in code. It doesn't make the query faster. Most queries minify safely, but a minifier that simply joins lines can change the result: in testing, a MySQL # comment swallowed the rest of the query, and removing a /*! */ comment changed a result from 2 to 1. Use the SQL minifier with the right database selected, and run the minified query once before you ship it.
How this was tested: on 11 November 2026, ten queries were minified with ToolStash's minifier code and the original and minified versions were run side by side on MySQL 8.4.9 and SQLite 3.50.6, comparing the results. Testing found six problems in the minifier itself; they were fixed before this guide was published, and the before-and-after results are below. The PostgreSQL, SQL Server and Oracle cases were checked against the minifier's output only, not run on those databases.
What a minified query looks like
-- Active customers with their last order
SELECT c.id,
c.name,
MAX(o.created_at) AS last_order /* newest first */
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE c.active = 1
GROUP BY c.id, c.name
ORDER BY last_order DESC;Minified, 258 characters became 171, a third smaller:
SELECT c.id,c.name,MAX(o.created_at) AS last_order FROM customers c JOIN orders o ON o.customer_id = c.id WHERE c.active = 1 GROUP BY c.id,c.name ORDER BY last_order DESC;Text inside 'strings', "quoted names", `backticks`, [brackets] and PostgreSQL $$ bodies is left alone, so 'it''s -- not a comment' survived intact and returned the same row on both databases.
When minifying is useful
- Config and JSON. A one-line query needs no
\nescapes:{"report":"SELECT c.id,c.name,MAX(o.created_at) AS last_order FROM customers c JOIN orders o ON o.customer_id = c.id WHERE c.active = 1 GROUP BY c.id,c.name ORDER BY last_order DESC;"} - Environment variables and CLI arguments, where line breaks are awkward or impossible.
- Logs. One query per log line keeps
grepand log search tools simple. - URLs and query strings for tools that accept SQL in a link. Shorter text means a shorter encoded URL; it still needs URL encoding.
When not to minify
- Source code you maintain. Keep SQL files and migrations formatted; reviewers need the line breaks. Minify at the point where you embed the query, not in the repository.
- For speed. The database parses the query once either way. This test didn't find, and wasn't looking for, a speed difference; the point is size and shape.
- To hide a query. Minified SQL is still readable, and the SQL formatter expands it again in a second.
- Instead of parameters. If you are minifying so you can paste user values into a string, stop: that is how SQL injection happens. Keep the query fixed and pass values separately:
// Instead of putting values into a minified string: const sql = "SELECT id, name FROM customers WHERE email = ? AND active = 1"; const [rows] = await db.execute(sql, [email]);
Six things that broke when lines were joined
These are the problems found in the original version of ToolStash's minifier. Each one is a trap for any minifier that treats SQL as plain text.
1. MySQL # comments swallowed the rest of the query
SELECT 1 AS a # first column
, 2 AS b;Joined onto one line, it became:
SELECT 1 AS a # first column,2 AS b;MySQL returned only column a: everything after # was now a comment, including , 2 AS b. No error, just a missing column. The fixed minifier keeps the line break after a # comment, and the minified query returned a and b like the original. (It doesn't delete the comment, because in PostgreSQL # is an operator.)
2. MySQL executable comments changed the result
SELECT 1 /*!80000 + 1 */ AS v; returned 2 on MySQL 8.4, because MySQL runs the code inside /*!80000 ... */ on version 8.0.0 and later. With the comment stripped it returned 1. These comments are common in mysqldump files, so the fixed minifier keeps every /*! ... */.
3. Optimizer hints disappeared
/*+ INDEX(o idx_orders_date) */ looks like a comment but tells Oracle or MySQL which index to use. Removing it doesn't change the rows returned, but it can change the plan and the speed. The fixed minifier keeps every /*+ ... */.
4. 5--3 isn't always a comment
In MySQL, -- starts a comment only when a space or control character follows it, so SELECT 5--3 AS v; returned 8 (five minus minus three). The original minifier treated --3 AS v; as a comment and produced SELECT 5. In PostgreSQL and SQL Server it really is a comment, so the fixed minifier keeps that line as written and starts a new line after it, which means the same thing in every database.
5. Backslashes in strings depend on the database
MySQL reads \' inside a string as an escaped quote. SQLite, PostgreSQL, SQL Server and Oracle don't: there, 'C:\' is a complete string. This SQLite query:
SELECT 'C:\' AS p, 'it''s' AS q -- don't
, 2 AS r;returned three columns. Minified with MySQL's rules, the minifier misread where the string ended, joined the comment onto the next line, and SQLite returned only p and q. With the standard rules it returned all three. The minifier now has a database setting for this: MySQL / MariaDB, or PostgreSQL, SQLite, SQL Server and Oracle.
6. GO lines were merged
GO separates batches in SSMS and sqlcmd and must be alone on its line. The original minifier produced CREATE TABLE #t(id int) GO INSERT INTO #t VALUES(1) GO. The fixed one keeps each GO on its own line.
Still unsupported: nested comments in PostgreSQL
PostgreSQL allows /* outer /* inner */ still comment */; MySQL and SQLite end the comment at the first */. The minifier follows MySQL and SQLite here, so a nested PostgreSQL comment leaves still comment */ behind. Remove nested comments by hand before minifying PostgreSQL code.
Checklist before you embed a minified query
- Select the right database in the minifier.
- Check that
/*+ */hints and/*! */comments are still there if the original had them. - Run the minified query once and compare the row count and columns with the original.
- Pass user input as parameters, never by editing the minified string.
- Keep the formatted version in your repository as the source of truth.
FAQ
Does minifying SQL improve performance?
Not in a way you would notice; the database parses the text either way. Minify for size and shape, not speed.
How do I turn a minified query back into readable SQL?
Paste it into the SQL formatter. Comments removed during minifying can't be recovered, which is another reason to keep the original.
Is it safe to minify a mysqldump file?
There is little reason to: dumps are loaded by tools that don't care about line breaks. If you do, keep the /*!...*/ comments, which the fixed minifier does, and test the import.
Is my SQL uploaded when I use the minifier?
No. It runs in your browser.