5

我使用 webgrind 和 xdebug 来破坏我的网站性能。php::PDO->__construct 函数花费了 85% 的页面加载时间(大约 1 秒)...

这是无法接受的。我可以以某种方式优化此功能吗?(缓存,mysql配置等)

我正在使用 php、mysql 和 codeigniter 与 redbean。redbean 使用该 pdo 构造函数...

这是函数源代码

/** 
 * 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; 
 } 
4

1 回答 1

8

解决方法很简单...

PDO 连接到 localhost -> 1 秒

PDO 连接到 127.0.0.1 -> 50 毫秒...

不要问我为什么...似乎与尝试和等待 ipv6 连接有关,然后回退到良好的旧 ipv4 ... ipv4 地址不尝试 ipv6 ...

于 2013-03-01T16:49:07.217 回答