Improve performance. #762

This commit is contained in:
Korina Cordero 2023-09-20 15:31:46 +08:00
parent a7c6988607
commit 19840c0305

View file

@ -61,11 +61,12 @@ class GetJobOrderArchiveDataCommand extends Command
$db = $this->em->getConnection(); $db = $this->em->getConnection();
// TODO: improve performance. out of memory exception
$query_sql = 'SELECT * $query_sql = 'SELECT *
FROM job_order FROM job_order
WHERE YEAR(date_create) = :year WHERE YEAR(date_create) = :year
ORDER BY date_create ORDER BY date_create
LIMIT 100000'; LIMIT 150000';
$query_stmt = $db->prepare($query_sql); $query_stmt = $db->prepare($query_sql);
$query_stmt->bindValue('year', $year, PDO::PARAM_STR); $query_stmt->bindValue('year', $year, PDO::PARAM_STR);
@ -93,13 +94,22 @@ class GetJobOrderArchiveDataCommand extends Command
$this->deleteDataFiles('invoice_item'); $this->deleteDataFiles('invoice_item');
$archive_files = []; $archive_files = [];
$jo_id_list = [];
while ($row = $results->fetchAssociative()) while ($row = $results->fetchAssociative())
{ {
$jo_data['job_order'][$row['id']] = $this->createJobOrderArchiveData($row); $jo_data['job_order'][$row['id']] = $this->createJobOrderArchiveData($row);
// add jo id to jo_id_list
$jo_id_list[] = $row['id'];
}
// get all related data for job order
foreach ($jo_id_list as $jo_id)
{
// foreach job order id we got from the first query, we get the JO related // foreach job order id we got from the first query, we get the JO related
// data for that id from jo_event, invoice, ticket, jo_rejection, rider_rating // data for that id from jo_event, invoice, ticket, jo_rejection, rider_rating
if (is_callable($callbackJO)) if (is_callable($callbackJO))
{ {
foreach ($related_tables as $table_name) foreach ($related_tables as $table_name)
@ -118,7 +128,7 @@ class GetJobOrderArchiveDataCommand extends Command
$db = $this->em->getConnection(); $db = $this->em->getConnection();
$query_stmt = $db->prepare($query_sql); $query_stmt = $db->prepare($query_sql);
$query_stmt->bindValue('id', $row['id']); $query_stmt->bindValue('id', $jo_id);
$files = call_user_func($callbackJO, $row, $query_stmt, $table_name, $year); $files = call_user_func($callbackJO, $row, $query_stmt, $table_name, $year);
@ -149,6 +159,7 @@ class GetJobOrderArchiveDataCommand extends Command
$data = []; $data = [];
$files = []; $files = [];
$invoice_id_list = [];
while ($q_row = $results->fetchAssociative()) while ($q_row = $results->fetchAssociative())
{ {
@ -156,10 +167,8 @@ class GetJobOrderArchiveDataCommand extends Command
// all invoice items for a specific invoice too // all invoice items for a specific invoice too
if ($table_name == 'invoice') if ($table_name == 'invoice')
{ {
// get invoice item related data // add invoice id to list
$ii_file = $this->getInvoiceItemArchiveData($q_row, $year); $invoice_id_list[] = $q_row['id'];
$files['invoice_item'] = $ii_file;
} }
$fields = []; $fields = [];
@ -173,6 +182,14 @@ class GetJobOrderArchiveDataCommand extends Command
$data[$table_name][$q_row['id']] = $fields; $data[$table_name][$q_row['id']] = $fields;
} }
// get the invoice items for archiving
foreach ($invoice_id_list as $i_id)
{
$ii_file = $this->getInvoiceItemArchiveData($i_id, $year);
$files['invoice_item'] = $ii_file;
}
// write the array into the file // write the array into the file
$file = $this->createDataFileRelatedArchiveData($data, $table_name, 'a'); $file = $this->createDataFileRelatedArchiveData($data, $table_name, 'a');
@ -182,14 +199,14 @@ class GetJobOrderArchiveDataCommand extends Command
return $files; return $files;
} }
protected function getInvoiceItemArchiveData($row, $year) protected function getInvoiceItemArchiveData($id, $year)
{ {
$db = $this->em->getConnection(); $db = $this->em->getConnection();
$query_sql = 'SELECT * FROM invoice_item WHERE invoice_id = :id'; $query_sql = 'SELECT * FROM invoice_item WHERE invoice_id = :id';
$query_stmt = $db->prepare($query_sql); $query_stmt = $db->prepare($query_sql);
$query_stmt->bindValue('id', $row['id']); $query_stmt->bindValue('id', $id);
$results = $query_stmt->executeQuery(); $results = $query_stmt->executeQuery();