最优化的MySQL语法

发布于 2024-12-10 14:05:32 字数 442 浏览 0 评论 0原文

虽然我在网上搜索了很多,但还没有找到答案。在 PHP 中,您可以优化输出,例如,输入

echo 'This is a Sentence by ',$name,' which is fast';

而不是

echo 'This is a moment by ',$name,' which is fast'; $name 这是非常低效的';

我的基本示例代码是

$server=english
mysql_query("INSERT INTO uz(id,email,pw,cook) VALUES('$id','$mail','$pw','$cook')");

在语句内使用双引号和 var 名称。有没有办法优化这个或者它是“最佳”版本?我尝试过的每一次尝试都会导致 mysql 错误。

While I've searched the web a lot I haven't quite found the answer yet. In PHP you can optimise your output, for example, by inputting

echo 'This is a sentence by ',$name,' which is fast';

instead of

echo 'This is a sentence by $name which is very inefficient';

My basic example code is

$server=english
mysql_query("INSERT INTO uz(id,email,pw,cook) VALUES('$id','$mail','$pw','$cook')");

Which uses double quotes and var names inside of statements. Is there a way to optimise this or is it the "best" version? Every attempt I've tried resulted in mysql errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

九八野马 2024-12-17 14:05:32

没关系。

这种优化对您的实际性能没有任何影响。各种串联方法之间的性能差异以微秒为单位。他们根本不扮演现实世界的角色。

相反,请使用最具可读性且与您的编码风格最同步的内容。

您展示的 echo 示例也是如此。除非您运行 100,000 次(在这种情况下,无论如何您都会做错事),否则在性能方面没有可测量的差异。如果您愿意,可以使用 microtime() 进行自己的基准测试。

It does not matter.

This kind of optimization has no real-life impact on your performance whatsoever. The performance differences between the various concatenation methods are in the microseconds. They play no real-world role at all.

Use what is most readable, and most in sync with your coding style, instead.

The same goes for the echo example you show. There is no measurable difference performance-wise unless you run it 100,000 times (in which case you would be something doing something wrong anyway). Do your own benchmarks using microtime() if you want to.

嘴硬脾气大 2024-12-17 14:05:32

真实地以微秒为单位进行交易;使用 {} 包围扩展字符串中的变量将为解析器节省一两个周期,并且在使用时还可以减少常量和关联数组键之间的混淆。

例如:

$string = 'hi ' . $name . ' how are you?'; //fast
$string = "hi $name how are you?"; //very slightly slower
$string = "hi {$name} how are you?"; //very slightly faster than second example

$string = 'hi ' . $person['name'] . ' how are you?'; //fast
$string = "hi $person[name] how are you?"; //slightly slower and prone constant clashes
$string = "hi {$person['name']} how are you?"; //very slightly faster and not prone to clashes

在使用数组键的第二个示例中,如果在某处声明了 const name ,则可能会遇到问题,因为解释器不知道您引用的是哪一个,并且默认使用该常量。

在数据库方面,您可以通过在字段和表名称周围使用“`”来优化查询,这既可以防止关键字冲突,也可以实现更快的解释(仍然以微秒为单位)。

例如:

mysql_query("SELECT `id`,`name`,`date` FROM `table` WHERE `id` = {$id}");

truthfully dealing in microseconds; surrounding variables in expanding strings with {} will save the parser a cycle or two and also cuts down on confusion between constants and associative array keys when used.

For Instance:

$string = 'hi ' . $name . ' how are you?'; //fast
$string = "hi $name how are you?"; //very slightly slower
$string = "hi {$name} how are you?"; //very slightly faster than second example

$string = 'hi ' . $person['name'] . ' how are you?'; //fast
$string = "hi $person[name] how are you?"; //slightly slower and prone constant clashes
$string = "hi {$person['name']} how are you?"; //very slightly faster and not prone to clashes

in the second example with an array key you can run into issues if there is a const name declared somewhere since the interpreter doesn't know which one you are referring two and uses the constant by default.

On the database side of things you can optimize your query by using "`"'s around your field and table names which both protects against keyword clashing and also allows for faster interpretation (still in microseconds).

For Instance:

mysql_query("SELECT `id`,`name`,`date` FROM `table` WHERE `id` = {$id}");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文