简体   繁体   English

PHP代码可以更快地处理

[英]PHP Code to Process Faster

I have a running PHP program which handles supplies inventory. 我有一个正在运行的PHP程序,可以处理耗材库存。

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. 下面的程序检索表的SUPPLIES和TRANSACTIONS,并从所有TRANSACTIONS计算余额,这将更新SUPPLIES的MYSQL表。

<?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
?>

As said @Phylogenesis change in Transaction should trigger update of Supplies. 如前所述,@ Phylogenesis的更改应触发耗材的更新。

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. 理想情况下,应尽可能将其放在事务表功能“ TRIGGER ON UPDATE”中的数据库中。

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. 它仍然比PHP慢,但快得多。

EDITED: I update the query example to meet question. 编辑:我更新查询示例以满足问题。

I did what @Phylogenesis had suggested to include updating $balance , straight to the database of $supplies based on '$transactions'. 我做了@Phylogenesis建议包含的更新$balance ,直接将其添加到基于'$ transactions'的$supplies数据库中。

To avoid delays on the client side to update all $balance of `$supplies', I used cron jobs to run scripts based on desired schedule. 为了避免客户端更新$ supplies的所有$balance的延迟,我使用cron作业根据所需的时间表运行脚本。

Installed cronjob for Centos 7 by following steps from this link: 通过此链接中的以下步骤为Centos 7安装了cronjob:

https://www.rosehosting.com/blog/automate-system-tasks-using-cron-on-centos-7/ 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 要检查是否安装了cron,请键入以下内容: systemctl status crond.service

Then used https://crontab.guru/ to generate expressions to schedule automatically running of PHP scripts. 然后使用https://crontab.guru/生成表达式以计划自动运行PHP脚本。

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: 要安装或创建或编辑自己的cron作业,请执行以下操作:

  1. Type crontab -e then ENTER 键入crontab -e然后回车

  2. Press i , then start typing your expression i ,然后开始输入您的表情

  3. Type :wq! 输入:wq! then ENTER to save your cron job. 然后按ENTER保存您的cron作业。

  4. Type crontab -l to check for running cron jobs. 键入crontab -l来检查正在运行的cron作业。

That's it! 而已! Hope this would help others! 希望这对别人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM