以下是我如何设置
class Core
{
public $dbh;
private static $instance;
private function __construct()
{
$dsn = 'mysql:host=' . CoreConfig::read('db.host') .
';dbname=' . CoreConfig::read('db.basename') .
';port=' . CoreConfig::read('db.port') .
';connect_timeout=15';
$user = CoreConfig::read('db.user');
$password = CoreConfig::read('db.password');
$this->dbh = new PDO($dsn, $user, $password);
}
public static function getInstance()
{
if (!isset(self::$instance))
{
$object = __CLASS__;
self::$instance = new $object;
}
return self::$instance;
}
}
class CoreConfig
{
static $confArray;
public static function read($name)
{
return self::$confArray[$name];
}
public static function write($name, $value)
{
self::$confArray[$name] = $value;
}
}
包含文件: db.php
CoreConfig::write('db.host', SERVER);
CoreConfig::write('db.port', '3306');
CoreConfig::write('db.basename', DB);
CoreConfig::write('db.user', USER);
CoreConfig::write('db.password', PASSWORD);
现在我正在尝试将我的网站从使用MySQL转换为PDO。但是,在查看了我的一些查询和页面加载时间后,我发现我的查询速度慢了很多。这是我在我的一个类中使用MySQL的简单示例:
MySQL版本
class lotInfo
{
protected $_lot;
public function __construct($lot)
{
$this->_lot = $lot;
}
public function numJobsMil()
{
$JOB_IDS = array();
$SQL = mysql_query("SELECT job_id FROM job_materials_lot WHERE lot='".$this->_lot."'") or die(mysql_error());
while($data=mysql_fetch_array($SQL))
{
array_push($JOB_IDS,$data['job_id']);
}
$SQL2 = mysql_query("SELECT job_id FROM job_materials_prop WHERE lot='".$this->_lot."'") or die(mysql_error());
while($data2=mysql_fetch_array($SQL))
{
array_push($JOB_IDS,$data2['job_id']);
}
return $JOB_IDS;
}
}
从我页面上的循环内调用方法时,数据加载时间为0.6秒。例如:
require_once 'db.php'
$lots = array("SIB","ABC","DEF","ZSP"); // etc etc etc
foreach($lots as $x => $lot)
{
$info = new lotInfo($lot);
$jobs[] = $info->numJobsMil();
}
// do stuff
现在,我班级的PDO版本如下:
PDO版本:
class lotInfo
{
protected $_lot;
protected $_pdo;
public function __construct($lot)
{
$core = Core::getInstance();
$this->_pdo = $core;
$this->_lot = $lot;
}
public function numJobsMil()
{
$JOB_IDS = array();
$STH = $this->_pdo->dbh->query("SELECT job_id FROM job_materials_lot WHERE lot='".$this->_lot."'");
$STH->setFetchMode(PDO::FETCH_ASSOC);
$SQL = mysql_query("SELECT job_id FROM job_materials_lot WHERE lot='".$this->_lot."'") or die(mysql_error());
while($data = $STH->fetch())
{
array_push($JOB_IDS,$data['job_id']);
}
$STH_sub = $this->_pdo->dbh->query("SELECT job_id FROM job_materials_prop WHERE lot='".$this->_lot."'");
while($sub_data = $STH_sub->fetch())
{
array_push($JOB_IDS,$sub_data['job_id']);
}
return $JOB_IDS;
}
}
到目前为止,这种方法非常有效,但是当通过同一个循环运行时,只要用户访问页面,数据就会平均加载3.6秒。现在,我不确定为什么会这样,但我想对此有所了解。
答案 0 :(得分:2)
实现相同目标的最多PDO方式(也利用UNION
聚合数据库层中的结果):
public function numJobsMil()
{
$STH = $this->_pdo->dbh->prepare('
SELECT job_id FROM job_materials_lot WHERE lot = ?
UNION ALL
SELECT job_id FROM job_materials_prop WHERE lot = ?
');
$STH->execute([$this->_lot, $this->_lot]);
return $STH->fetchAll(PDO::FETCH_COLUMN, 0);
}
每当我看到UNION
以这种方式使用时,我不禁想知道这两个表是否应该合并在一起,用一个列来标记基础记录的起源。