-1

我刚刚从mysql_*To迁移了我的代码,PDO并且很高兴看到巨大的性能影响。我一直在阅读http://woptoo.com/blog/pdo-vs-mysqli-performance-comparison/并且认为PDO会更有效率。

但我认为我可能做错了什么,这使我的代码比预期的要慢得多。

我创建了一个singleton

class db
      {

      static $dbinstance=null;  

      public static function getinstance()
      {

      if (!self::$dbinstance) 
         {
          try
            {
            self::$dbinstance=new PDO("mysql:host=127.0.0.1;dbname=inventory",'user','pass');
            self::$dbinstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            }
          catch( PDOException $e)
           {
            die( '<b>Errors:</b> '.$e->getMessage());
           }
       }
          return self::$dbinstance;
     }

下面是我用来插入的代码(为简单起见,我将代码缩短了)

    if(isset($_POST['Build']))
{
    $InCart=db::getinstance()->prepare("SELECT `Vendor`, `ItemType`, `ItemCode`, `ItemDesc`, `SerialNo`, `Asset_Code`, `ItmUnit`, `Qty`,`SiteId`, `id`, `Ownership`, `wh`,  `Phase`, `Category`, `Issue_Vendor`, `AssetName`, `po`, `FaultInfo`, `willreturn`, `Person`, `remarks`, `grnno`,stockid FROM temptable WHERE id=?");

    $insertinSIR=db::getinstance()->prepare("INSERT INTO Sir(SirNo,SiteId,Vendor,Type,ItemDesc,ItemCode,SerialNo,Unit,AssetCode,Qty,Region,Phase,Category,Issue_vendor,AssetName,ownership,Dated,PersonName,Remarks,po,invuser,grnno,WarehouseType,WarehouseLocation) values( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW(),?,?,?,?,?,?,?)");

  foreach ($_POST['id'] as $id)
      {
     $InCart->execute(array($id)); 
     $arr=$InCart->fetch(PDO::FETCH_NUM);

        $insertinSIR->execute(array($a,$arr[8],$arr[0],$arr[1],$arr[3],$arr[2],$arr[4],$arr[6],$arr[5],$arr[7],$user,$arr[12],$arr[13],$vendor,$arr[15],$arr[10],$person,$remarks,$arr[16],$invuser,$arr[21],$whtype,$whlocation));

      } 

如果有人指出性能击球手,我会很感激?
编辑

这是我准备语句和循环执行的正确方式吗?

4

1 回答 1

1

提议:

INSERT INTO Sir (SirNo, ...)
     SELECT Vendor, ...
       FROM temptable
      WHERE id IN (?, ?, ...)

开始时不需要通过 PHP 传输所有数据。请参阅http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

于 2013-06-28T06:01:39.293 回答