Should I break SQL queries in different lines? For example in the project I am working on, we have a query that is taking 1600 columns! 1600 + tab chars. I wrote queries like this:
"SELECT bla , bla2 , bla FROM bla " .
"WHERE bla=333 AND bla=2" .
"ORDER BY nfdfsd ...";
But they demanded me to put them in one line and said that my style is bad formatting. Why it is bad practice?
6 Answers 6
For source control reasons, we have linebreaks after every where clause, or comma. So your above turns into
SELECT bla
, bla2
, bla
FROM bla
WHERE bla=333
AND bla=2
ORDER BY nfdfsd
, asdlfk;
(tabbing and alignment has no standard here, but commas are usually leading)
Still, makes no performance difference.
-
5Good idea, this would make a small change stand out very nicely in a source control diff.Carson63000– Carson630002010年11月22日 03:45:04 +00:00Commented Nov 22, 2010 at 3:45
-
Pretty much the same formatting as I use, though I usually put all of the select list on a single line (or multiple lines if there's a lot of columns)Dean Harding– Dean Harding2010年11月22日 05:41:28 +00:00Commented Nov 22, 2010 at 5:41
-
9Similar layout here, only difference being the leading comma, we have it at the end.DBlackborough– DBlackborough2010年11月22日 12:02:19 +00:00Commented Nov 22, 2010 at 12:02
-
4@m.edmondson - Diff-ing between version in source control highlights changes on a line by line basis. With this format each line contains a single bit of information - a column name, a table name, a join or order clause - which means that the diff will point right at what's changed, not just to a line with many things on and leave you to work out what's different.Jon Hopkins– Jon Hopkins2010年11月23日 00:11:51 +00:00Commented Nov 23, 2010 at 0:11
-
2This format also makes it easy to comment out single items during development and to use cut and paste to change the ordering.Chris Nava– Chris Nava2011年02月14日 05:30:28 +00:00Commented Feb 14, 2011 at 5:30
A query that is 1600 columns sounds like it needs some serious review by a good DBA.
If a query is complex I'll wrap it. If it's straightforward I'll leave it as a single line unless it's going to be too long, then I'll start wrapping it again.
It's all about manageability and comprehending what it is supposed to do so wrapping or not wrapping can be decided on the fly, unless your organization has some code formatting rules about it.
Re: it being bad coding practice. Hardly! It's very good practice. There are no good reasons I know of to use a query that long, and many good reasons to reformat it. As I said before, a skilled DBA probably needs to work on it.
-
3Agreed, it all comes down to readability really. Performance etc doesnt get affected by this at all, its all just aesthetic.Christian– Christian2010年11月22日 01:15:54 +00:00Commented Nov 22, 2010 at 1:15
-
Agree that performance can't be a good argument.the Tin Man– the Tin Man2010年11月22日 01:17:54 +00:00Commented Nov 22, 2010 at 1:17
-
I dont know.. just told me to keep it in one line ,maybe because they doGorillaApe– GorillaApe2010年11月22日 02:09:42 +00:00Commented Nov 22, 2010 at 2:09
-
They're probably afraid to touch it if it's "legacy" code. Just slowly back away and everything will be fine.the Tin Man– the Tin Man2010年11月22日 02:32:46 +00:00Commented Nov 22, 2010 at 2:32
-
Its fresh code ...GorillaApe– GorillaApe2010年11月22日 04:07:48 +00:00Commented Nov 22, 2010 at 4:07
The only advantage of single line queries that comes to mind is that those queries may be somewhat easier to grep for. Other than that, though, I am stumped. Personally, I prefer the more readable, split up queries.
Multiline comments are good, almost vital when dealing with large volumes of SQL. And if your programming language has heredoc quotes, it's even better (as many editors can highlight SQL syntax in them).
Example:
$a = SQL<<<
SELECT a, b, c, d
FROM Foo f
WHERE f.a = ?
SQL;
When working with queries of dozens of lines (or hundreds) both the indentation and whitespace make the text workable.
-
1For PHP, nowdocs are the single-quoted variety (i.e. no variable substitution).Alan Pearce– Alan Pearce2010年11月22日 19:57:12 +00:00Commented Nov 22, 2010 at 19:57
It seems this is specifically about defining a big query inside a programming language of sorts, seeing you put the query inside a string literal and concatenate it.
If it's a compiled language, it should make no difference at all - one of the first optimizations the compiler would do is to automatically concatenate the string literals together, so you end up with a big string anyways.
As for the syntax, you should actually consider moving the query outside of your code - store it in a separate .sql resource file, and have your software read that file. Use prepared statements for the variables, if it's not a query that's built dynamically (i.e. where-clauses etc added depending on certain parameters). If it is built dynamically, you could add in replacement variables of your own, inserting extra parameters where and when needed.
As for the 1600 columns, I seriously recommend building a view for that, so instead of
SELECT column1, column2, .... column1600 from X where Y
you'd get
SELECT * FROM viewX WHERE y
Much more concise in your own code.
-
+1, and I'd also consider making the query into a stored procedureLarry Coleman– Larry Coleman2010年11月22日 19:54:11 +00:00Commented Nov 22, 2010 at 19:54
I often use the format put forth by @glasnt to troubleshoot a complicated query, however usually have queries in a single line.
This might not answer your question, but I'd also strongly suggest breaking down your query into smaller queries. Obviously this depends on the query, but the more clauses and joins you add to your query - the less the SQL engine is able to optimise your query.
Your database vendor should have tools like MySQL's EXPLAIN (or MSSQL's SHOWPLAN_ALL setting) which will show you what the database is doing behind the scenes to optimise your query, every time the database has to create a temporary table or some such, you're adding huge delays when you're talking about multiple concurrent users.
By moving what might seem like trivial logic out of the SQL and into your code, you can provide dramatic performance increases - SQL is great at simple operations.
The obvious benefit to this as it might relate to you, is that your queries are much less complex and easy to read - easy to manage (not>1600 columns), and faster. Definately an all-round win.
Hope this helps :)
.
), which I've seen some programmers blame for performance costs.