为什么查询中的额外空格和换行符不好?

发布于 2024-11-15 01:03:13 字数 764 浏览 3 评论 0原文

我有时会看到人们说从客户端应用程序发送到服务器的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡莣 2024-11-22 01:03:13

确实,这会消耗网络流量和服务器时间;但除了最极端的情况外,在所有情况下它都可以忽略不计。
现在,如果您正在编辑 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.

慵挽 2024-11-22 01:03:13

这是主观的,但在我看来,可读性胜过任何额外的空格和换行符。如果编码标准要求每次都断开字符串,我可能会发疯。

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.

晚风撩人 2024-11-22 01:03:13

如果您绝对必须优化空间等,请不要在源代码中执行此操作。相反,将其通过自动化中间工具进行处理。

如果我们谈论网络,我会说对于静态内容(很少更改的脚本文件等)来说,这样做的额外成本可能是值得的,但我对动态内容这样做持怀疑态度。

在所有情况下:

  • 如果您更改源,这将是维护的噩梦。
  • 如果您使用压缩/解压缩工具,您将比简单地删除空间节省更多(平均而言),但代价是延迟和 CPU 时间。
  • 除非你有一些真正病态的结构,否则它基本上只占总成本的一小部分,即使我们只考虑 TCP 数据包的大小和返回的查询数据。

也许与您的情况无关,但无论如何我都会提到它:一种完全不同的方法可能是使用紧密封装的消息格式而不是查询 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:

  • If you change the source, it will be a maintenance nightmare.
  • If you put it through a compression/decompression tool, you'll save significantly more (on average) than simply removing spaces but at a cost of latency and CPU time.
  • Unless you have some really pathological structure, it basically constitutes a tiny fraction compared to the total cost, even if we only considered the size of TCP packets, and query data returned.

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.

维持三分热 2024-11-22 01:03:13

绝对地。我经常这样做。我还:

  • 删除反引号。谁需要它们?

因此,

`po` ----- becomes -----> po 
  • 数据库、表、字段、索引等使用尽可能小的名称。

因此,

postponed_operations  --- becomes ---> po    --- p is already taken for posts
will_be_deleted_after --- becomes ---> wi    --- w is already taken for words
  • 完全删除不必要的关键字,例如AS。无论如何,所有表名都很短(规则 2!)

因此

LEFT JOIN `users` AS `u`  --- becomes --->   LEFT JOIN u 

,我会将上述查询编写为:

$q='SELECT po.*,ni,lo FROM po LEFT JOIN u ON i=ui ORDER BY wi'

Tags:

joke

Absolutely. I do that all the times. I also:

  • remove backquotes. Who needs them?

Therefore,

`po` ----- becomes -----> po 
  • use as small as possible names for databases, tables, fields, indices, etc.

Therefore,

postponed_operations  --- becomes ---> po    --- p is already taken for posts
will_be_deleted_after --- becomes ---> wi    --- w is already taken for words
  • Completely drop unnecessary keywords like AS. All table names are short anyway (rule 2 !)

Therefore,

LEFT JOIN `users` AS `u`  --- becomes --->   LEFT JOIN u 

As a result, I would have written the above query as:

$q='SELECT po.*,ni,lo FROM po LEFT JOIN u ON i=ui ORDER BY wi'

Tags:

joke

抹茶夏天i‖ 2024-11-22 01:03:13

虽然删除不必要的空格和换行符确实会减少发送到数据库服务器的数据量,但您不应该为此担心。

相反,您应该关心代码的可读性和可维护性。这是编写软件代码时需要牢记的两件非常重要的事情!

如果减少网络流量是唯一的好处,那么我们可以认为您应该创建一个 存储过程

例如。您可以将以下查询更改

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`;

CALL GetLoginData();

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

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`;

to

CALL GetLoginData();

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文