up vote 3 down vote favorite
Share on Facebook

Should i break sql queries in different lines ?For example in the project i am working 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 that my style is bad formating
WHy it is bad practice?

link|flag
The objection may be to the use of interpolated quotes (double quotes) and concatenation (.), which I've seen some programmers blame for performance costs. – Bruce Alderson Nov 22 at 5:34

7 Answers

up vote 0 down vote

My 2p worth:

SELECT
    a.field1
    ,a.field2
    ,b.field1
    ,c.field1
FROM
    tablea a
    INNER JOIN tableb b ON
        a.key = b.fkey
    LEFT JOIN tablec c ON
        b.key = c.fkey
ORDER BY
    a.field1 ASC
    ,b.field1 ASC
link|flag
up vote 1 down vote

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.

link|flag
+1, and I'd also consider making the query into a stored procedure – Larry Coleman Nov 22 at 19:54
up vote 2 down vote

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.

link|flag
+1 for mention of heredoc – Larry Coleman Nov 22 at 19:53
1  
For PHP, nowdocs are the single-quoted variety (i.e. no variable substitution). – Alan Nov 22 at 19:57
up vote 1 down vote

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 :)

link|flag
up vote 10 down vote

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 different.

link|flag
Good idea, this would make a small change stand out very nicely in a source control diff. – Carson63000 Nov 22 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 Nov 22 at 5:41
2  
Similar layout here, only difference being the leading comma, we have it at the end. – G3D Nov 22 at 12:02
For source control reasons? Can you please explain? – m.edmondson Nov 22 at 23:00
Commas at the start of the row instead of the end of the row to make commenting out easier too? – Jon Hopkins Nov 23 at 0:10
show 4 more comments
up vote 1 down vote

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.

link|flag
up vote 5 down vote

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.

link|flag
1  
Agreed, it all comes down to readability really. Performance etc doesnt get affected by this at all, its all just aesthetic. – Christian Nov 22 at 1:15
Agree that performance can't be a good argument. – Greg Nov 22 at 1:17
I dont know.. just told me to keep it in one line ,maybe because they do – Parhs Nov 22 at 2:09
They're probably afraid to touch it if it's "legacy" code. Just slowly back away and everything will be fine. – Greg Nov 22 at 2:32
Its fresh code ... – Parhs Nov 22 at 4:07
show 3 more comments

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.