外部循环的PDO事务比自动交易需要更多的时间吗?

时间:2016-01-24 14:51:01

标签: php mysql pdo transactions

我知道在插入大数据时,手动启动和提交事务会更好。

但我现在测试了3次,

自动提交启用时

14秒, 自动提交禁用 18秒,交易开始 循环 >循环。

这怎么可能?为什么?

这是我在没有人工交易的情况下测试的代码:

<?php
include "../php/class.connect.php";
include "../php/class.xmltodb.php";
include "../php/class.getxml.php";
include "helper.php";

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
ini_set("user_agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
@ini_set("max_execution_time", 0); @set_time_limit(30); // Time limit is non-existant for this process
$xmldb = new XMLtoDB();
$con = new Connect();
$PDO = $con->PDO;

clearfile();

$xml_url= 'http://oddsfeed.bet365.com/Soccer.asp?MarketID=50138';
$betname = "Handicap"; $sport = "Football"; $sportid=1; 
$xml_content = file_get_contents($xml_url) or die("can't get file");
$xml = simplexml_load_string($xml_content);
$countryid=0; $formatted_date=""; $time_goal=""; $local_id=""; $visitor_id="";

/* ISKLJUCUJEM AUTO SNIMANJE */
//$sql = "SET AUTOCOMMIT=0";
//$dbs = $PDO->prepare($sql);
//$dbs->execute();

/* POCINJEM TRANSAKCIJU */
//$PDO->beginTransaction();
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Going through leagues
foreach ($xml->xpath('EventGroup') as $eventGroup) { // every separate league is an EventGroup
    $leaguetemp = getLeague((string)$eventGroup->attributes()->Name,(string) $eventGroup->attributes()->ID);

// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Going through matches
    if ($leaguetemp != false) {
    $leaguename = $leaguetemp[0]; // League name
    $leagueID = $leaguetemp[1]; // League ID    
    if ($leagueID != NULL) {
    $explodedtags = explode (" ", $leaguename); 
    $country = str_replace(":","",$explodedtags[0]);  // First word is (in most cases) the country name
        // Assigning custom Country names for leagues which don't start with a country name
    if ($country=="Barclays") $country="England"; if ($country=="Capital") $country="England";
    if ($country=="UK") $country="England"; if ($country=="Community") $country="England";  

    echo "<b>" , $leagueID , " " , $country , " - " , $leaguename , "</b>" , "<BR/>";
    foreach ($eventGroup->Event as $event) { // every separate match is an Event
        $eventName = (string) $event->attributes()->Name;
        $eventNameOrig = $eventName;
        $eventID = (string) $event->attributes()->ID;
        $teams = explode (" v ", $eventName); $homeTeam = $teams[0]; $awayTeam = $teams[1];
        $homeTeamOrig = $homeTeam; $awayTeamOrig = $awayTeam;
        // ovdje ide GetMap funkcija, zasebno za $homeTeam i za $awayTeam
        $homeTeam = getMap($country,$homeTeam);
        $awayTeam = getMap($country,$awayTeam);
        $eventName = $homeTeam . " - " . $awayTeam;
        $originaldate = (string) $event->attributes()->StartTime;
        $newdate = DateTime::createFromFormat('d/m/y H:i:s' , $originaldate)->format('Y-m-d H:i:s');
        $date = DateTime::createFromFormat('d/m/y H:i:s' , $originaldate)->format('Y-m-d');
        $time = DateTime::createFromFormat('d/m/y H:i:s' , $originaldate)->format('H:i:s');

        foreach ($event->Market as $market) { // Market
            $num = 1;
            foreach ($market->Participant as $participant) {
                $oddsname[$num] = $participant->attributes()->Name;
                $oddseu[$num] = $participant->attributes()->OddsDecimal;
                $num++;
                if ($participant->attributes()->Name == $homeTeamOrig) {
                    $HomeOddsName = "Home"; 
                    $HomeOddsEU = $participant->attributes()->OddsDecimal;
                    $HomeOddsUK = $participant->attributes()->Odds;
                    $HomeOddsID = $participant->attributes()->ID;
                    $HomeHandicap = $participant->attributes()->Handicap;
                    $num=1;
                }
                if ($participant->attributes()->Name == $awayTeamOrig) {
                    $AwayOddsName = "Away";
                    $AwayOddsEU = $participant->attributes()->OddsDecimal;
                    $AwayOddsUK = $participant->attributes()->Odds;
                    $AwayOddsID = $participant->attributes()->ID;
                    $num=2;
                    $DrawOddsName = "";
                    $DrawOddsEU = "";
                    $DrawOddsUK = "";
                    $DrawOddsID = "";
                }
        if ($num == 2){
        if($tipgin = getTipGinInfo($homeTeam,$awayTeam,$newdate)){      
          $fixtureid = $tipgin[0];
          $hometeamid = $tipgin[1];
          $awayteamid = $tipgin[2];
          $tgcountry = $tipgin[3];
          $subid = $tipgin[4];
          $cup = $tipgin[5];

          $xmldb->execOdds($fixtureid,NULL,$betname,$tgcountry,$leaguename,$leagueID,$subid,$cup,"football",
          $homeTeam,$hometeamid,$awayTeam,$awayteamid,$newdate,$date,$time,$time,NULL,$HomeOddsEU,$DrawOddsEU,$AwayOddsEU,"Bet366",78881,$HomeHandicap,0,"bet365",$eventID,$HomeOddsUK,$DrawOddsUK,$AwayOddsUK);

          echo $newdate , " " , $eventID , " " , "(" , $eventNameOrig , ") " , $homeTeam , " - " , $awayTeam;
          echo " AH (" . $HomeHandicap . ") ";
          echo "<font color='#3333dd'>";
          echo " " , $HomeOddsName , " " , $HomeOddsEU , " " , "<font size='1'>" , $HomeOddsID , "</font>";
          echo " " , $DrawOddsName , " " , $DrawOddsEU , " " , "<font size='1'>" , $DrawOddsID , "</font>";
          echo " " , $AwayOddsName , " " , $AwayOddsEU , " " , "<font size='1'>" , $AwayOddsID , "</font>";
          echo "</font>";

        }
        else{
            writebody($newdate . " " . $eventID . " " . $homeTeam . " - " . $awayTeam);
        }
            }


            }
        }

        echo '<BR/>';
    } // foreach $event
    } // if leagueid != null
    }
}

try{
    //$PDO->commit(); //snimam
    echo "SAVED";
}
catch(Exception $e){
    //$PDO->rollBack(); //odbacujem
    echo $e;
}

/* VRACAM AUTO SNIMANJE */
/*$sql = "SET AUTOCOMMIT=1";
$dbs = $PDO->prepare($sql);
$dbs->execute();
*/
$con->close();
?>

这是带有交易的代码:

<?php
include "../php/class.connect.php";
include "../php/class.xmltodb.php";
include "../php/class.getxml.php";
include "helper.php";

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
ini_set("user_agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
@ini_set("max_execution_time", 0); @set_time_limit(30); // Time limit is non-existant for this process

$xmldb = new XMLtoDB();
$con = new Connect();
$PDO = $con->PDO;

clearfile();

$xml_url= 'http://oddsfeed.bet365.com/Soccer.asp?MarketID=50138';
$betname = "Handicap"; $sport = "Football"; $sportid=1; 
$xml_content = file_get_contents($xml_url) or die("can't get file");
$xml = simplexml_load_string($xml_content);
$countryid=0; $formatted_date=""; $time_goal=""; $local_id=""; $visitor_id="";

/* ISKLJUCUJEM AUTO SNIMANJE */
$sql = "SET AUTOCOMMIT=0";
$dbs = $PDO->prepare($sql);
$dbs->execute();

/* POCINJEM TRANSAKCIJU */
$PDO->beginTransaction();
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Going through leagues
foreach ($xml->xpath('EventGroup') as $eventGroup) { // every separate league is an EventGroup
    $leaguetemp = getLeague((string)$eventGroup->attributes()->Name,(string) $eventGroup->attributes()->ID);

// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Going through matches
    if ($leaguetemp != false) {
    $leaguename = $leaguetemp[0]; // League name
    $leagueID = $leaguetemp[1]; // League ID    
    if ($leagueID != NULL) {
    $explodedtags = explode (" ", $leaguename); 
    $country = str_replace(":","",$explodedtags[0]);  // First word is (in most cases) the country name
        // Assigning custom Country names for leagues which don't start with a country name
    if ($country=="Barclays") $country="England"; if ($country=="Capital") $country="England";
    if ($country=="UK") $country="England"; if ($country=="Community") $country="England";  

    echo "<b>" , $leagueID , " " , $country , " - " , $leaguename , "</b>" , "<BR/>";
    foreach ($eventGroup->Event as $event) { // every separate match is an Event
        $eventName = (string) $event->attributes()->Name;
        $eventNameOrig = $eventName;
        $eventID = (string) $event->attributes()->ID;
        $teams = explode (" v ", $eventName); $homeTeam = $teams[0]; $awayTeam = $teams[1];
        $homeTeamOrig = $homeTeam; $awayTeamOrig = $awayTeam;
        // ovdje ide GetMap funkcija, zasebno za $homeTeam i za $awayTeam
        $homeTeam = getMap($country,$homeTeam);
        $awayTeam = getMap($country,$awayTeam);
        $eventName = $homeTeam . " - " . $awayTeam;
        $originaldate = (string) $event->attributes()->StartTime;
        $newdate = DateTime::createFromFormat('d/m/y H:i:s' , $originaldate)->format('Y-m-d H:i:s');
        $date = DateTime::createFromFormat('d/m/y H:i:s' , $originaldate)->format('Y-m-d');
        $time = DateTime::createFromFormat('d/m/y H:i:s' , $originaldate)->format('H:i:s');

        foreach ($event->Market as $market) { // Market
            $num = 1;
            foreach ($market->Participant as $participant) {
                $oddsname[$num] = $participant->attributes()->Name;
                $oddseu[$num] = $participant->attributes()->OddsDecimal;
                $num++;
                if ($participant->attributes()->Name == $homeTeamOrig) {
                    $HomeOddsName = "Home"; 
                    $HomeOddsEU = $participant->attributes()->OddsDecimal;
                    $HomeOddsUK = $participant->attributes()->Odds;
                    $HomeOddsID = $participant->attributes()->ID;
                    $HomeHandicap = $participant->attributes()->Handicap;
                    $num=1;
                }
                if ($participant->attributes()->Name == $awayTeamOrig) {
                    $AwayOddsName = "Away";
                    $AwayOddsEU = $participant->attributes()->OddsDecimal;
                    $AwayOddsUK = $participant->attributes()->Odds;
                    $AwayOddsID = $participant->attributes()->ID;
                    $num=2;
                    $DrawOddsName = "";
                    $DrawOddsEU = "";
                    $DrawOddsUK = "";
                    $DrawOddsID = "";
                }
        if ($num == 2){
        if($tipgin = getTipGinInfo($homeTeam,$awayTeam,$newdate)){      
          $fixtureid = $tipgin[0];
          $hometeamid = $tipgin[1];
          $awayteamid = $tipgin[2];
          $tgcountry = $tipgin[3];
          $subid = $tipgin[4];
          $cup = $tipgin[5];

          $xmldb->execOdds($fixtureid,NULL,$betname,$tgcountry,$leaguename,$leagueID,$subid,$cup,"football",
          $homeTeam,$hometeamid,$awayTeam,$awayteamid,$newdate,$date,$time,$time,NULL,$HomeOddsEU,$DrawOddsEU,$AwayOddsEU,"Bet366",78881,$HomeHandicap,0,"bet365",$eventID,$HomeOddsUK,$DrawOddsUK,$AwayOddsUK);

          echo $newdate , " " , $eventID , " " , "(" , $eventNameOrig , ") " , $homeTeam , " - " , $awayTeam;
          echo " AH (" . $HomeHandicap . ") ";
          echo "<font color='#3333dd'>";
          echo " " , $HomeOddsName , " " , $HomeOddsEU , " " , "<font size='1'>" , $HomeOddsID , "</font>";
          echo " " , $DrawOddsName , " " , $DrawOddsEU , " " , "<font size='1'>" , $DrawOddsID , "</font>";
          echo " " , $AwayOddsName , " " , $AwayOddsEU , " " , "<font size='1'>" , $AwayOddsID , "</font>";
          echo "</font>";

        }
        else{
            writebody($newdate . " " . $eventID . " " . $homeTeam . " - " . $awayTeam);
        }
            }


            }
        }

        echo '<BR/>';
    } // foreach $event
    } // if leagueid != null
    }
}

try{
    $PDO->commit(); //snimam
    echo "SAVED";
}
catch(Exception $e){
    $PDO->rollBack(); //odbacujem
    echo $e;
}

/* VRACAM AUTO SNIMANJE */
$sql = "SET AUTOCOMMIT=1";
$dbs = $PDO->prepare($sql);
$dbs->execute();

$con->close();
?>

0 个答案:

没有答案