-1

Here's how I've set up

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;
    }

}

Include File: 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);

Now I'm trying to convert my site from using MySQL to PDO. However, after reviewing some of my queries and page loading time, I've found that my queries are a lot slower. Here's a simplified example of how I used to use MySQL in one of my classes:

MySQL Version

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;
    }

}

When calling the method from within a loop on my page, the data loads in 0.6 seconds. For example:

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

Now, the PDO version of my class as follows:

PDO Version:

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;
    }

}

So far, this works perfectly, however when running this through the same loop, the data loads on average of 3.6 seconds whenever a user accesses the page. Now, I'm not sure why this is happening, but I'd like to get some opinions on this.

4

1 回答 1

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以这种方式使用时,我不禁想知道是否应该将这两个表组合在一起,并用一列来标记底层记录来自哪个表。

于 2013-07-12T17:59:34.443 回答