PHP-MySQL 或 MySQLi 中哪个最快?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/171400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:45:47  来源:igfitidea点击:

Which is fastest in PHP- MySQL or MySQLi?

phpmysqlmysqli

提问by David

I'd like to know if anyone has any first-hand experience with this dichotomy. A few blogs say the mysql extension is faster than mysqli. Is this true?

我想知道是否有人对这种二分法有任何第一手经验。一些博客说mysql扩展比mysqli快。这是真的?

And I'm only asking about speed. I know mysqli has features that are not present in the older extension.

我只问速度。我知道 mysqli 具有旧扩展中不存在的功能。

回答by Bill Karwin

The MySQL extension is very slightly faster than MySQLi in most benchmarks I've seen reported. The difference is so slight, however, that this should probably not be your criterion for deciding between the two.

在我所看到的大多数基准测试中,MySQL 扩展比 MySQLi 快得多。然而,差异是如此之小,以至于这可能不是您在两者之间做出决定的标准。

Other factors dwarf the difference in performance between mysql and mysqli. Using mod_php or FastCGI, a bytecode cache like APC, or using data caching judiciously to reduce database hits, are far more beneficial for overall performance of PHP scripts than the choice of MySQL extension.

其他因素使 mysql 和 mysqli 之间的性能差异相形见绌。使用 mod_php 或 FastCGI,像 APC 这样的字节码缓存,或者明智地使用数据缓存来减少数据库命中,对于 PHP 脚本的整体性能比选择 MySQL 扩展更有益。

Don't be penny wise and pound foolish! :-)

不要吝啬和愚蠢!:-)

回答by Alkini

"It depends."

“这取决于。”

For example, PHP MySQL vs MySQLi Database Access Metricsand the subsequent comments point out arguments both ways.

例如,PHP MySQL vs MySQLi Database Access Metrics和随后的评论指出了两种方式的论据。

If you have a mature database and codebase, do some testing and see what works in your system. If not, stop worrying about premature optimization.

如果您有成熟的数据库和代码库,请进行一些测试,看看哪些在您的系统中有效。如果没有,请不要担心过早的优化。

回答by Gordon

See http://php.net/manual/en/mysqlinfo.api.choosing.php

http://php.net/manual/en/mysqlinfo.api.choosing.php

The overall performance of all three extensions is considered to be about the same. Although the performance of the extension contributes only a fraction of the total run time of a PHP web request. Often, the impact is as low as 0.1%.

所有三个扩展的整体性能被认为大致相同。尽管扩展的性能只占 PHP Web 请求总运行时间的一小部分。通常,影响低至 0.1%。

回答by AgelessEssence

Maybe, this can be a reason to make the right choice :: The Plot to Kill PHP MySQL Extension

也许,这可能是做出正确选择的理由 :: The Plot to Kill PHP MySQL Extension

" Yes, you read it right. Recently, Phillip Olson sent to the PHP internals mailing list a proposal to kill the original PHP MySQL extension in future PHP versions. "

“是的,你没看错。最近,Phillip Olson 向 PHP 内部邮件列表发送了一份建议,在未来的 PHP 版本中终止原有的 PHP MySQL 扩展。”

回答by Pank

In relation to PHP programming language, MySQL is the old database driver, and MySQLi is the Improved driver. MySQLi takes advantage of the newer features of MySQL 5.

就 PHP 编程语言而言,MySQL 是旧的数据库驱动程序,而 MySQLi 是改进的驱动程序。MySQLi 利用了 MySQL 5 的新特性。

Features of MySQLi taken from php.net site:

MySQLi 的特性取自 php.net 站点:

  • Object-oriented interface
  • Support for Prepared Statements
  • Support for Multiple Statements
  • Support for Transactions
  • Enhanced debugging capabilities
  • Embedded server support
  • 面向对象的接口
  • 支持准备好的报表
  • 支持多语句
  • 支持交易
  • 增强的调试功能
  • 嵌入式服务器支持

回答by Jay

According to all the Google results for benchmarks linked by ceejayozit looks like MySQL is at least slightly faster than MySQLi in all the benchmark tests. I do recommend reading the results for details but just figured I'd post something that directly answered the question and bumps up ceejayoz's answer.

根据ceejayoz链接的所有Google 基准测试结果,在所有基准测试中,MySQL 看起来至少比MySQLi 快一点。我确实建议阅读结果以了解详细信息,但只是想我会发布一些直接回答问题并提高 ceejayoz 答案的内容。

回答by Don Kirkby

The PHP documentation has a good comparison of mysql, mysqli, and PDO. I know you only asked about speed, but others might find this useful. It talks about the feature differences between the options.

PHP 文档对 mysql、mysqli 和 PDO 进行了很好的比较。我知道您只询问了速度,但其他人可能会觉得这很有用。它讨论了选项之间的功能差异。

回答by Taylor

Unless milliseconds matter then don't worry. Surely if you have no need for the extra functionality provided by mysqli then just stick with the tried and tested mysql.

除非毫秒很重要,否则不要担心。当然,如果您不需要 mysqli 提供的额外功能,那么只需坚持使用久经考验的 mysql。

回答by Josh

<?php
$start = microtime();
$c = new mysqli('localhost', 'username', 'userpass', 'username_dbname');
$c -> select_db('username_dbname');


$q = $c -> query("SELECT * FROM example");

while ($r = $q -> fetch_array(MYSQLI_ASSOC))
  {
  echo $r['col1'] . "<br/>\n";
  }

$me = $c -> query("SELECT col1 FROM example WHERE id='11'") -> fetch_array(MYSQLI_ASSOC);

echo $me['col1'];
echo (microtime() - $start);
?>

Why when using mysqli oop is there a slight speed increase from using this script with mysql or mysqli procedural style? When testing the above script I get .0009 seconds consistantly more than when using the other 2. When using mysql or mysqli procedural, I loaded the scripts 20x in each different style and the two were always above .001. I load the above script 20x and I get below .001 5x.

为什么在使用 mysqli oop 时,使用这个脚本与 mysql 或 mysqli 程序风格相比,速度会略有提高?测试上述脚本时,我得到的时间比使用其他 2 个时长 0.0009 秒。当使用 mysql 或 mysqli 程序时,我以 20 倍的速度加载了每种不同风格的脚本,而这两个总是高于 0.001。我将上述脚本加载 20 倍,结果低于 0.001 5 倍。

回答by Donovanr

MySQLihas two basic advantages over MySQL; prepared statements are a great way to avoid SQL injection attacks. Secondly MySQL (or mariaDB) will do their best to optimize prepared statements and thus you have the potential for speed optimizations there. Speed increases from making the database happy will vastly outsize the tiny difference between MySQL and MySQLi.

库MySQLi有超过两个基本优点MySQL的; 准备好的语句是避免 SQL 注入攻击的好方法。其次,MySQL(或 mariaDB)将尽最大努力优化准备好的语句,因此您有可能在那里进行速度优化。使数据库满意而提高的速度将大大超过 MySQL 和 MySQLi 之间的微小差异。

If you are feeding in statements you mangle together yourself like SELECT * FROM users WHERE ID=$user_idthe database will treat this as a unique statement with each new value of $user_id. But a prepared statement SELECT * FROM users WHERE ID=?stands a much better chance of having some optimizations/caching performed by the database.

如果您输入语句,您自己SELECT * FROM users WHERE ID=$user_id将自己弄乱,就像数据库会将其视为具有每个新值的唯一语句$user_id。但是准备好的语句SELECT * FROM users WHERE ID=?更有可能让数据库执行一些优化/缓存。

But comparisons are fairly moot as MySQL is now officially deprecated. From the horse's mouth:

但由于 MySQL 现在正式弃用,因此进行比较是没有意义的。从马嘴里说:

Deprecated features in PHP 5.5.x

PHP 5.5.x 中已弃用的功能

ext/mysql deprecation

ext/mysql 弃用

The original MySQL extension is now deprecated, and will generate E_DEPRECATEDerrors when connecting to a database. Instead, use the MySQLior PDO_MySQLextensions.

原始 MySQL 扩展现已弃用,并且E_DEPRECATED在连接到数据库时会产生错误。相反,使用MySQLiPDO_MySQL扩展。