Reputation: 25
I have a running PHP program which handles supplies inventory.
I want this code to process faster to update supplies' balances when opening the page.
SUPPLIES contains the inventory of supplies and their balances.
TRANSACTIONS contains the transactions of receive, return and issued.
The program below retrieves tables' SUPPLIES and TRANSACTIONS and computes the balances from all TRANSACTIONS which updates the MYSQL table of SUPPLIES.
<?php
//Update supplies balance starts here
$supplies = DB::getInstance()->query("SELECT * FROM supplies ORDER BY id DESC");
foreach($supplies->results() as $supply) {
$balance = 0;
$transactions = DB::getInstance()->query("SELECT * FROM transactions ORDER BY id ASC");
foreach($transactions->results() as $transaction) {
if($transaction->code === $supply->id){
if($transaction->transaction_type === "1"){
$balance = $balance + $transaction->quantity;
} else if($transaction->transaction_type === "2"){
$balance = $balance - $transaction->quantity;
} else if($transaction->transaction_type === "3"){
$balance = $balance + $transaction->quantity;
}
}
}
$supplied = new Supply();
$supplied->find($supply->id);
try {
$supplied->update(array(
'balance' => $balance,
));
} catch (Exception $e) {
die($e->getMessage());
}
}
//Update supplies balance ends here
?>
Upvotes: 0
Views: 133
Reputation: 25
I did what @Phylogenesis had suggested to include updating $balance
, straight to the database of $supplies
based on '$transactions'.
To avoid delays on the client side to update all $balance
of `$supplies', I used cron jobs to run scripts based on desired schedule.
Installed cronjob for Centos 7 by following steps from this link:
https://www.rosehosting.com/blog/automate-system-tasks-using-cron-on-centos-7/
To check if cron's installed, type this: systemctl status crond.service
Then used https://crontab.guru/
to generate expressions to schedule automatically running of PHP scripts.
If still confused on how it works, check out this link for examples:
https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
To install or create or edit your own cron jobs:
Type crontab -e
then ENTER
Press i
, then start typing your expression
Type :wq!
then ENTER to save your cron job.
Type crontab -l
to check for running cron jobs.
That's it! Hope this would help others!
Upvotes: 0
Reputation: 1013
As said @Phylogenesis change in Transaction should trigger update of Supplies.
BEGIN TRANSACTION;
UPDATE transactions SET quantity = ..., code = ..., ....;
UPDATE supplies SET balance = balance +/- ....;
COMMIT;
So you do not need to update supplies every time the page was opened, and do not need to recompute entire table. It is important to do this in one transaction. Ideally it should be placed in database in transaction table feature TRIGGER ON UPDATE if possible.
If you can't, try to compute updated value of supplies on database side, example:
UPDATE supplies s
SET balance =
(SELECT SUM(quantity) FROM transactions t WHERE t.code = s.id AND transaction_type IN (1, 3)) -
(SELECT SUM(quantity) FROM transactions t WHERE t.code = s.id AND transaction_type = 2);
It is still slower but much faster then PHP.
EDITED: I update the query example to meet question.
Upvotes: 1