为什么查询中的额外空格和换行符不好?
我有时会看到人们说从客户端应用程序发送到服务器的 SQL 查询不应包含任何额外的换行符或空格。我听到的原因之一是“为什么要浪费网络流量?”。
是否有真正的理由让代码更难以阅读和编辑以支持删除所有空格?
有空格:
$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);
无空格:
$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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
确实,这会消耗网络流量和服务器时间;但除了最极端的情况外,在所有情况下它都可以忽略不计。
现在,如果您正在编辑 FaceBook(或 Google 或类似的)的代码,并以这种方式优化 10 个最常见的查询,那么就有一点了,因为它们每天将运行数十亿次。
但在所有其他情况下,我认为考虑删除空格是浪费时间。
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.
这是主观的,但在我看来,可读性胜过任何额外的空格和换行符。如果编码标准要求每次都断开字符串,我可能会发疯。
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.
如果您绝对必须优化空间等,请不要在源代码中执行此操作。相反,将其通过自动化中间工具进行处理。
如果我们谈论网络,我会说对于静态内容(很少更改的脚本文件等)来说,这样做的额外成本可能是值得的,但我对动态内容这样做持怀疑态度。
在所有情况下:
也许与您的情况无关,但无论如何我都会提到它:一种完全不同的方法可能是使用紧密封装的消息格式而不是查询 ID,而不是每次都传输查询。
If you absolutely must optimize spaces and such away, do not do it in your source code. Instead put it through an automated intermediate tool.
If we were talking about web, I'd say that the extra cost in doing might potentially be worth it for static content (script files that rarely change and such) but I would be skeptical about doing it for dynamic content.
In all cases:
Perhaps not relevant in your case, but I'll mention it anyway: a completely different approach might be to use a tightly packed message format instead with a query ID, instead of transferring the query every time.
绝对地。我经常这样做。我还:
因此,
因此,
AS
。无论如何,所有表名都很短(规则 2!)因此
,我会将上述查询编写为:
Tags:
joke
Absolutely. I do that all the times. I also:
Therefore,
Therefore,
AS
. All table names are short anyway (rule 2 !)Therefore,
As a result, I would have written the above query as:
Tags:
joke
虽然删除不必要的空格和换行符确实会减少发送到数据库服务器的数据量,但您不应该为此担心。
相反,您应该关心代码的可读性和可维护性。这是编写软件代码时需要牢记的两件非常重要的事情!
如果减少网络流量是唯一的好处,那么我们可以认为您应该创建一个 存储过程。
例如。您可以将以下查询更改
为
Now that would be ~80-95% 减少。但这值得吗?
绝对不行。
做这样的事情宁愿让开发者的日子过得很痛苦,也不会增加任何显着的价值!
话虽如此,仅在没有人会更改代码的地方使用缩小版本的代码。例如。 CSS 库和 JS 库你永远不会改变!
我希望您明白了这一点,并且您将继续编写可读和可维护的代码!
Although it is true that removing the unnecessary spaces and line breaks would reduce the amount of data that you send to the database server, but you should not bother about that.
Rather you should bother about the Readability and maintainability of the code. These are the two very important things you need to keep in mind while writing software code!
If reducing network traffic was the only good thing, then we could argue that you should make a Stored Procedure for every query that you write.
For eg. You could change the following query
to
Now that would be ~80-95% reduction. But is it worth it?
Definitely No.
Doing things like these would rather make the developers' life miserable without adding any significant value!
That being said, use minified version of code only at places where nobody would be changing the code. For eg. CSS libraries and JS libraries that you wont changing ever!
I hope you got the point, and you will continue to write Readable and Maintainable code!