首页 > 解决方案 > PHP7 cron architecture for MySQL

问题描述

I have some php pages that have lots of queries on them. Everything works correctly, but it takes ages to load the pages due to very large data sets (tens of thousand of rows with compound queries etc.)

I realised I have to optimise this and take it from 30 secs load time to almost 3-4 seconds.

I did some reading on cron job scheduling, and understood how to do it, but I have no idea what the architecture should be in case of a lot of queries.

Example of query in html:

<span class="text-muted my-1">Total count of records time</span>
<hr>
<?php
    $select_query = "SELECT SUM(rectime / 3600) AS numberOfHours FROM inf_times";
    $result = mysqli_query ($conn, $select_query);
                                
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo ceil($row['numberOfHours']) . " hours";    
        }
    }
?>

How do I turn this into a cron job (say a cron job that runs every minute)?

My idea

I want to display the results of the last cron job that ran, so obviously it's a good idea to have variables for the things I'm displaying in a separate PHP file. These should get updated every minute, but how?

标签: phpmysqlcron

解决方案


推荐阅读