Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why extra spaces and linebreaks in queries are bad?

I see, from time to time, that people say that SQL query that is sent to a server from client application should not contain any extra linebreaks or spaces. One of the reason I've heard is "why waste network traffic?".

Is there a real reason to make code harder to read and edit in favor of removing all spaces?

With spaces:

$q = 'SELECT
            `po`.*,
            `u`.`nickname`,
            `u`.`login`
        FROM
            `postponed_operations` AS `po`
            LEFT JOIN `users` AS `u` ON `u`.`id` = `po`.`user_id`
        ORDER BY `will_be_deleted_after`';
return mysql_query($q);

Without spaces:

$q = 'SELECT '.
            '`po`.*,'.
            '`u`.`nickname`,'.
            '`u`.`login`'.
        'FROM '.
            '`postponed_operations` AS `po` '.
            'LEFT JOIN `users` AS `u` ON `u`.`id`=`po`.`user_id` '.
        'ORDER BY `will_be_deleted_after`';
return mysql_query($q);
like image 923
Silver Light Avatar asked Jun 09 '11 10:06

Silver Light


2 Answers

It is true, it will cost network traffic and server time; but it will be negligible on all except the most extreme cases. Now, if you are editing the code of FaceBook (or Google, or similar), and optimize in this way the 10 most common queries, then there is a point, since they will be run billions of times per day. But in all the other cases I think it is a waste of time to consider removing spaces.

like image 70
carlo.borreo Avatar answered Oct 14 '22 02:10

carlo.borreo


This is subjective, but readability beats the few extra spaces and line breaks anytime in my opinion. And if coding standards would dictate to break of out the string every time, I'd probably go insane.

like image 44
Wesley van Opdorp Avatar answered Oct 14 '22 02:10

Wesley van Opdorp