I am using codeigniter and mysql and i am trying to get some data from mssql server and updating to my system. Its very slow. please let me know if anything wrong i am doing..
class systemupdate extends MY_Controller{
function systemupdate() {
return parent::MY_Controller();
}
function index(){
$message = '';
$message = "CRON Information::COMMON INVENTORY SYSTEM: QUANTITY UPDATE FOR TRADE WH STARTED.";
log_message('info', $message);
$this->load->model("cis/sapinventorymodel");
$objMainResult = $this->sapinventorymodel->FetchDetailsByUnit(420);
$mainResult = json_decode($objMainResult);
if((isset($mainResult)) && ($mainResult->results > 0))
{
$this->load->model("cron/systemupdatemodel");
foreach($mainResult->rows as $main)
{
$arrQuantity = '';
$arrQuantity = $this->systemupdatemodel->FetchTradeWhQuantity($main->number);
if((isset($arrQuantity)) && (count($arrQuantity) > 0))
{
if((isset($arrQuantity[0]->quantity)) && ($arrQuantity[0]->quantity != NULL)){
$objMainResult = $this->sapinventorymodel->SaveTradeWhQuantity($main->a_umber, $main->a_plant,
$main->a_unit,$arrQuantity[0]->quantity);
}
}
}
}
$message = "CRON Information::COMMON INVENTORY SYSTEM: QUANTITY UPDATE FOR TRADE WH FINSIHED.";
log_message('info', $message);
}
}
Here is the model
class systemupdatemodel extends MY_Model {
function systemupdatemodel() {
parent::__construct();
$CI->sapData = $this->load->database("sapinventory", TRUE);
$this->sapData = &$CI->sapData;
}
function FetchTradeWhQuantity($p_intNumber){
$query = $this->sapData->query("SELECT quantity FROM master1 WHERE NUMBER = '$p_intNumber'");
if(($query->num_rows() > 0) && ($query->num_rows() == 1)){
return $query->result();
}else
{
$query1 = $this->sapData->query("SELECT quantity FROM master2 WHERE EANNUMBER = '$p_intNumber'");
if(($query1->num_rows() > 0) && ($query1->num_rows() == 1)){
return $query1->result();
}
}
}
}
Total number of records are 7857 i.e this many times its in loop. sapinventory is a mssql server.
FetchTradeWhQuantityandSaveTradeWhQuantitydoes a lot of runs in this case. That means (probably) at least 3 mysql queries per row. If you’d consider running this through 1 or 2 big queries instead, your performance would go up very noticeable.Now this might be something easy to do, or something hard to do.
For instance;
Could be
But the best thing you can do is to do all of this with 2-3 queries only. Fetch all data, use their IDs and run a
UPDATEorINSERT INTO ... ON DUPLICATE KEY UPDATE. Then the server only have to connect to the mysql database a few times and doesn’t have to play pinball with the php for each little row.