i used webgrind and xdebug to messure my site performance. 85% of page loading time is taken for the function php::PDO->__construct (about 1 second) ...
this is unacceptable. can i somehow optimize this function? (caching, mysql configuration etc.)
i am using php, mysql and codeigniter with redbean. redbean uses that pdo construct function...
here is the function source code
/**
* Establishes a connection to the database using PHP PDO
* functionality. If a connection has already been established this
* method will simply return directly. This method also turns on
* UTF8 for the database and PDO-ERRMODE-EXCEPTION as well as
* PDO-FETCH-ASSOC.
*
* @return void
*/
public function connect() {
if ($this->isConnected) return;
$user = $this->connectInfo['user'];
$pass = $this->connectInfo['pass'];
//PDO::MYSQL_ATTR_INIT_COMMAND
$this->pdo = new PDO(
$this->dsn,
$user,
$pass,
array(1002 => 'SET NAMES utf8',
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
)
);
$this->pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
$this->isConnected = true;
}
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.
PHP 8.1: PDO::FETCH_SERIALIZE is deprecated This functionality, however, is broken and is unusable.
PDO will not run since upgrading PHP to 5.4.
The PHP Data Objects (PDO) defines a lightweight interface for accessing databases in PHP. It provides a data-access abstraction layer for working with databases in PHP. It defines consistent API for working with various database systems.
The solution is quite simple ...
PDO connecting to localhost -> 1 second
PDO connecting to 127.0.0.1 -> 50 miliseconds...
dont aks me why ... seems to have something to do with try & wait for ipv6 connection, then fall back to good old ipv4 ... the ipv4 adresse does not try ipv6 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With