需要帮助打破超出PHP脚本/ max_execution_time

时间:2015-06-18 16:48:22

标签: php mysql linux apache

我有一个PHP脚本,可以更新我网站上最受欢迎和最热门的图像。这个脚本应该由cron作业每天运行一次,但我相信由于流量数据库(它主要依赖于它),每个图像的每日统计数据都存储得太大,脚本会超时。我在hostgator共享主机上,没有办法增加max_execution_time,不幸的是VPS或专用服务器此时不可行。如果有人对我可以用以下脚本或数据库(目前超过100mb)做什么有任何建议让我顺利运行,我将不胜感激。你认为截断数据库是一个好主意,还是只是搞乱了图像数据的重要方面?

当前脚本如下:

<?php 
    $minute = date("i");

//  if($minute=='00') {
        include("../config/config.inc.php"); 
        include("../includes/meme-function.php");

        $date = date("Y-m-d");

        $query = mysql_query("SELECT `meme_id` FROM `$database1`.`memes`");
        while($row=mysql_fetch_assoc($query)) {
            $query_b = mysql_query("SELECT `views` FROM `$database2`.`daily_meme_stats` WHERE `meme_id` = '$row[meme_id]' ORDER BY `date` DESC");
            $c=0; $week=0; $month=0;
            while($row_b=mysql_fetch_assoc($query_b)) { 
                if($c<=5) {
                    $week=$week+$row_b['views'];
                }
                $month=$month+$row_b['views'];
            }
            mysql_query("UPDATE `$database1`.`memes` SET `views_day` = '0', `views_week` = '$week', `views_month` = '$month' WHERE `meme_id` = '$row[meme_id]' LIMIT 1");
            mysql_query("INSERT INTO `$database2`.`daily_meme_stats` SET `meme_id` = '$row[meme_id]', `date` = '$date'");
        }

        $query = mysql_query("SELECT `character_id` FROM `$database1`.`characters`");
        while($row=mysql_fetch_assoc($query)) {
            $query_b = mysql_query("SELECT `images` FROM `$database2`.`daily_char_stats` WHERE `char_id` = '$row[character_id]' ORDER BY `date` DESC");
            $c=0; $week=0; $month=0;
            while($row_b=mysql_fetch_assoc($query_b)) { 
                if($c<=5) {
                    $week=$week+$row_b['images'];
                }
                $month=$month+$row_b['images'];
            }
            mysql_query("UPDATE `$database1`.`characters` SET `images_today` = '0', `images_week` = '$week', `images_month` = '$month' WHERE `character_id` = '$row[character_id]' LIMIT 1");
            mysql_query("INSERT INTO `$database2`.`daily_char_stats` SET `char_id` = '$row[character_id]', `date` = '$date'");
        }

//  }

我非常感谢您提出的任何建议或想法。

1 个答案:

答案 0 :(得分:1)

我的想法:

  • 我想到的第一件事是:尝试将两个主要查询拆分成自己的文件。
    • update-memes.phpupdate-chars.php
  • 可能会提高max_execution_time或禁用它set_time_limit(0);
  • 可能将功能移到MySQL存储功能
  • 将所有统计(重新)计算移动到任务/作业队列,如Gearman
    • 可以在后台持续工作并进行更新
  

您是否有推荐的文件结构,包括每个文件的开始和结束标记。

分割文件时遇到问题?我们走了:

update-memes.php

<?php 
    // set_time_limit(0);

    include '../config/config.inc.php'; 
    include '../includes/meme-function.php';

    $date = date("Y-m-d");

    $query = mysql_query("SELECT `meme_id` FROM `$database1`.`memes`");

    while($row=mysql_fetch_assoc($query)) {

        $query_b = mysql_query("SELECT `views` FROM `$database2`.`daily_meme_stats` WHERE `meme_id` = '$row[meme_id]' ORDER BY `date` DESC");

        $c=0; $week=0; $month=0;

        while($row_b=mysql_fetch_assoc($query_b)) { 
            if($c<=5) {
                $week=$week+$row_b['views'];
            }
            $month=$month+$row_b['views'];
        }

        mysql_query("UPDATE `$database1`.`memes` SET `views_day` = '0', `views_week` = '$week', `views_month` = '$month' WHERE `meme_id` = '$row[meme_id]' LIMIT 1");

        mysql_query("INSERT INTO `$database2`.`daily_meme_stats` SET `meme_id` = '$row[meme_id]', `date` = '$date'");
    }
?>

update-chars.php

<?php
    // set_time_limit(0);

    include '../config/config.inc.php'; 
    include '../includes/meme-function.php';

    $date = date("Y-m-d");

    $query = mysql_query("SELECT `character_id` FROM `$database1`.`characters`");

    while($row=mysql_fetch_assoc($query)) {

        $query_b = mysql_query("SELECT `images` FROM `$database2`.`daily_char_stats` WHERE `char_id` = '$row[character_id]' ORDER BY `date` DESC");

        $c=0; $week=0; $month=0;   

        while($row_b=mysql_fetch_assoc($query_b)) { 
            if($c<=5) {
                $week=$week+$row_b['images'];
            }
            $month=$month+$row_b['images'];
        }

        mysql_query("UPDATE `$database1`.`characters` SET `images_today` = '0', `images_week` = '$week', `images_month` = '$month' WHERE `character_id` = '$row[character_id]' LIMIT 1");

        mysql_query("INSERT INTO `$database2`.`daily_char_stats` SET `char_id` = '$row[character_id]', `date` = '$date'");
    }
?>

顺便说一下:我放弃了$minute = date("i"); // if($minute=='00') {。如果你确实需要的话,毫无头绪。

<强>的Cron

将这些行添加到您的cronjobs(调整路径):

# run stats scripts every hour
00 * * * * /usr/local/bin/php /home/somewhere/update-memes.php
00 * * * * /usr/local/bin/php /home/somewhere/update-chars.php