56

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?

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

nickf
  • 537,072
  • 198
  • 649
  • 721
David
  • 7,487
  • 6
  • 32
  • 25

10 Answers10

88

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.

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.

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

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • 1
    What is the biggest difference between using mysql and mysqli then? I was taught to only use mysqli and i was never told why. Could you give me a brief explanation? Thanks – Drewdin Sep 22 '10 at 14:18
  • 1
    ext/mysqli supports more features of the underlying MySQL API, such as transactions and prepared queries with parameters. Also you can call mysqli functions in an object-oriented manner. – Bill Karwin Sep 22 '10 at 14:44
  • 4
    @Drewdin: The mysql extension is deprecated and will be removed in later versions. You should not ever be using mysql over mysqli any more. Also the older mysql extension has issues with string escaping as well as persistent connection problems on later versions of MySQL. – Geoffrey Jun 03 '13 at 04:17
  • 1
    @Geoffrey, yes good point if you're considering which extension to choose, that presumes you have the freedom to switch. Now almost five years after I originally answered the question, the mysql extension should not be considered if the project has any choice. – Bill Karwin Jun 03 '13 at 04:45
  • @Geoffrey, Actually it'll forever exist in PECL even after being deprecated. – Pacerier Oct 17 '14 at 20:04
  • @BillKarwin, Assuming we're using InnoDB or an engine that supports transaction, is MySQL not able to do transactions using the usual `mysql_query('start transaction')` and `mysql_query('commit')`? – Pacerier Aug 13 '15 at 10:49
  • 1
    @Pacerier, yes, and you can also run [PREPARE and EXECUTE statements](http://dev.mysql.com/doc/refman/5.6/en/sql-syntax-prepared-statements.html). But the mysql API in PHP is still deprecated. – Bill Karwin Aug 14 '15 at 01:28
  • @BillKarwin, Good to hear that `mysql_query` is not inherently limited at the lower-level. – Pacerier Aug 14 '15 at 06:40
  • @BillKarwin: Excellent quote: "Don't be penny wise and pound foolish!" :) – erol yeniaras Oct 28 '15 at 20:59
  • @BillKarwin you should look at this https://stackoverflow.com/questions/50330388/mysqli-query-too-slow-in-loop – snehm May 14 '18 at 12:58
10

"It depends."

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

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.

Alkini
  • 1,449
  • 2
  • 12
  • 26
6

See 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%.

Gordon
  • 312,688
  • 75
  • 539
  • 559
3

According to all the Google results for benchmarks linked by ceejayoz it 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.

Community
  • 1
  • 1
Jay
  • 41,768
  • 14
  • 66
  • 83
3

Maybe, this can be a reason to make the right choice :: 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. "

AgelessEssence
  • 6,395
  • 5
  • 33
  • 37
3

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.

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
3

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.

Features of MySQLi taken from php.net site:

  • Object-oriented interface
  • Support for Prepared Statements
  • Support for Multiple Statements
  • Support for Transactions
  • Enhanced debugging capabilities
  • Embedded server support
Pank
  • 13,800
  • 10
  • 32
  • 45
  • 9
    Not sure why this has been up-voted so many times. I’m down-voting it as it doesn’t answer the actual question. – Martin Bean Jun 22 '13 at 19:36
  • 3
    The question is about the speed, not about the beauty or additional features. – Fedir RYKHTIK Jul 04 '13 at 15:18
  • I guess this is the thing that should matter, even if MYSQL is 'slightly' faster the MySQLi, i will not build a system on MYSQL becuase sooner or later it might become depricated, hence rewrite all your your db engines and any related codes ! – Rami Dabain Jul 25 '13 at 13:28
  • @RonanDejhero, There is still PECL. Deprecated != unavailable. – Pacerier Oct 17 '14 at 20:05
1

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.

Taylor
  • 1,700
  • 4
  • 17
  • 18
0
<?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.

Josh
  • 11
  • When testing generally I would suggest using SQL_NO_CACHE to help make sure the queries are running. Additionally the test you have is not very robust and the slightly random RAM / Harddisk access performance could easily skew results. Realistically a battery of tests under the same conditions is required to be certain that one or other has a performance improvement. – Andrew Jan 27 '17 at 14:01
-2

MySQLi has 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.

If you are feeding in statements you mangle together yourself like SELECT * FROM users WHERE ID=$user_id the 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.

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

Deprecated features in PHP 5.5.x

ext/mysql deprecation

The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi or PDO_MySQL extensions.

A J
  • 3,970
  • 14
  • 38
  • 53
Donovanr
  • 81
  • 1
  • 3
  • you can make it `"SELECT * FROM users WHERE ID IN ($user_id, $user_id2)"` and have it even faster – Your Common Sense Jun 22 '13 at 19:44
  • 1
    And one can make a prepared statement out of mysql too. – Your Common Sense Jun 22 '13 at 19:45
  • This contains inaccurate if at some point SELECT * FROM users WHERE ID=1 had run, you have query caching enabled and this query was stored then running it again will get this from the cache regardless of if this statement was generated as a direct query string or a prepared statement. – Andrew Jan 27 '17 at 13:58