We know that LAMPLinux + Apache + Mysql + PHP) is a very common WEB server combination. PHP has manyMySQL databaseTo facilitate our use, for example:Pdo-Mysql,MysqliAnd so on. Which method has the best performance? In this article, we compare the performance of the above Connection Methods:
1. Create a Mysql database, create an InnoDB table, and refer to the core table of a project.) then, use the stored procedure to generate 1 million pieces of data.
2. In a PHP environment, each access will randomly query a piece of data in the database.
3. verify the correctness of the query results after the access is completed.
Pdo-Mysql persistent connection $ dbh = new PDO (......, Array (PDO: ATTR_PERSISTENT => true ));
Pdo-Mysql short connection $ dbh = new PDO ($ host, $ username, $ password );
Mysqli $ dbh = new mysqli ($ host, $ username, $ password, $ dbname, $ port );
Mysqli_connect $ dbh = mysqli_connect ($ host, $ username, $ password, $ dbname, $ port );
$ Dbh = mysql_connect ($ host, $ username, $ password );
Conclusion:
1. Use the persistent connection mode of Pdo-Mysql to achieve optimal performance;
2. Mysql has better performance than Pdo-Mysql instant connection;
3. There is no significant difference in the real-time connection performance of Pdo-Mysql compared with the "built-in" method.
Appendix: Figure of transactions per second between Pdo-Mysql and Mysqli:
Appendix, the memory usage diagram of the Web server during the corresponding time period:
We can see from the two figures above:
1. The reason why Pdo-Mysql is faster than Mysqli is that Pdo-Mysql has fewer database operations than Mysqli;
2. In the Pdo-Mysql persistent connection test, the Web server memory continues to rise.
After a simple check, the httpd process memory of the Aapche service is increased, and the module name is displayed as [anonymous]. The reason will be further checked later)
From the perspective of performance, the persistent connection performance of Pdo-Mysql is the best, but the disadvantages are still to be verified.
This article introduces the performance comparison between MySQL and Mysql In the MySQL database in PHP. I hope this introduction will help you gain some benefits!