最优化的MySQL语法
虽然我在网上搜索了很多,但还没有找到答案。在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没关系。
这种优化对您的实际性能没有任何影响。各种串联方法之间的性能差异以微秒为单位。他们根本不扮演现实世界的角色。
相反,请使用最具可读性且与您的编码风格最同步的内容。
您展示的 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.真实地以微秒为单位进行交易;使用
{}
包围扩展字符串中的变量将为解析器节省一两个周期,并且在使用时还可以减少常量和关联数组键之间的混淆。例如:
在使用数组键的第二个示例中,如果在某处声明了 const name ,则可能会遇到问题,因为解释器不知道您引用的是哪一个,并且默认使用该常量。
在数据库方面,您可以通过在字段和表名称周围使用“`”来优化查询,这既可以防止关键字冲突,也可以实现更快的解释(仍然以微秒为单位)。
例如:
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:
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: