我使用webgrind和xdebug來了解我的網站性能。 頁面加載時間的85%取自函數 php :: PDO - > __構造(大約1秒)...加速php :: PDO - > __構造
這是不可接受的。我可以以某種方式優化這個功能嗎? (緩存,mysql配置等)
我使用php,mysql和codeigniter與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;
}
如果這是不可接受的,那麼考慮一下你爲什麼要調用__construct這麼多次,然後從那裏調出來。 – 2013-03-01 15:19:32
[Google很棒,男士。花了我2秒](http://stackoverflow.com/questions/9800577/why-is-constructing-pdo-connection-slow) – 2013-03-01 15:26:32
順便說一句,字符集應設置在DSN – 2013-03-01 15:27:53