Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is fastest in PHP- MySQL or MySQLi?

Tags:

php

mysql

mysqli

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.

like image 684
David Avatar asked Oct 05 '08 02:10

David


People also ask

Is MySQLi faster than MySQL?

Basically, MySQL is the old database driver, and MySQLi is the Improved driver. The "i" stands for "improved" so it is MySQL improved. MySQLi can be done procedural and object-oriented whereas MySQL can only be used procedurally. Mysqli also supports prepared statements which protect from SQL Injection.

Which is faster PHP or MySQL?

MySQL is faster in scope of SQL query. PHP is faster in PHP code. If you make SQL query to find out SQRT() it should be definitely slower (unless PHP is broken) because MySQL parser and networking overhead.

Should I use MySQLi or MySQL?

Reasons why you should use MySQLi extension instead of the MySQL extension are many: MySQLi gives you prepared statements - a safer way of sending data to MySQL and protecting you from SQL injection. This alone should be enough for always choosing MySQLi over MySQL. MySQLi enables most of the MySQL features.

Which is faster PDO or MySQLi?

Performance. While both PDO and MySQLi are quite fast, MySQLi performs insignificantly faster in benchmarks - ~2.5% for non-prepared statements, and ~6.5% for prepared ones. Still, the native MySQL extension is even faster than both of these.


2 Answers

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! :-)

like image 105
Bill Karwin Avatar answered Sep 25 '22 07:09

Bill Karwin


"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.

like image 23
Alkini Avatar answered Sep 26 '22 07:09

Alkini