PHP 脚本超时

发布于 2024-12-01 05:50:07 字数 986 浏览 0 评论 0原文

我有一个用于公告板系统的自定义脚本,它可以计算用户创建的线程数,并相应地更新列。这工作正常,但是对于超过 100,000 个用户,第一次运行时会超时。

我尝试在查询之前添加以下内容,但它仍然超时(500 错误)。

set_time_limit(0);
ignore_user_abort(true);

附加:我在我的 vps 上使用此脚本。

查询:

set_time_limit(0); 
ignore_user_abort(true);

$db->write_query("ALTER TABLE `".TABLE_PREFIX."users` ADD `numthreads` int(10) unsigned NOT NULL default '0'");

// load users into an array to count number of threads
$query = $db->simple_select("users", "uid");
while($user = $db->fetch_array($query))
{
    $users[$user['uid']] = $user;
}

foreach($users as $user)
{   
    $query = $db->simple_select("threads", "COUNT(tid) AS threads", "uid = '{$user['uid']}'");

    // get total number of threads
    $numthreads = intval($db->fetch_field($query, "threads"));

    $db->update_query("users", array("numthreads" => $numthreads), "uid = '{$user['uid']}'");
}

I have a custom script for a bulletin board system that counts the number of threads a user has made, and updates a column accordingly. This works fine, however with 100,000+ users, it times out when running it for the first time.

I've tried adding the following before the query, but it still times out (500 error).

set_time_limit(0);
ignore_user_abort(true);

Additional: I'm using this script on my vps.

Query:

set_time_limit(0); 
ignore_user_abort(true);

$db->write_query("ALTER TABLE `".TABLE_PREFIX."users` ADD `numthreads` int(10) unsigned NOT NULL default '0'");

// load users into an array to count number of threads
$query = $db->simple_select("users", "uid");
while($user = $db->fetch_array($query))
{
    $users[$user['uid']] = $user;
}

foreach($users as $user)
{   
    $query = $db->simple_select("threads", "COUNT(tid) AS threads", "uid = '{$user['uid']}'");

    // get total number of threads
    $numthreads = intval($db->fetch_field($query, "threads"));

    $db->update_query("users", array("numthreads" => $numthreads), "uid = '{$user['uid']}'");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

dawn曙光 2024-12-08 05:50:07

使用这个:

ini_set('max_execution_time', 0);

代替:

set_time_limit(0); 
ignore_user_abort(true);

您还可以编辑 php.ini:

max_execution_time = 60; //Maximum execution time of each script, in seconds
max_input_time = 60; //Maximum amount of time each script may spend parsing request data

希望这会有所帮助。

Use this:

ini_set('max_execution_time', 0);

Inplace of:

set_time_limit(0); 
ignore_user_abort(true);

You can also edit php.ini:

max_execution_time = 60; //Maximum execution time of each script, in seconds
max_input_time = 60; //Maximum amount of time each script may spend parsing request data

Hope this helps.

柳若烟 2024-12-08 05:50:07

首先,您应该分开您的 ALTER 语句。先执行ALTER,然后再执行其余的操作。如果你有一张大桌子,那么改变桌子的时间可能会很昂贵。您可以使用 phpmyadmin 或通过 shell 手动运行它(更好,因为没有 php 超时)。这将使您能够不超时。

然后从脚本中删除 ALTER 并运行它。

然后使用:

$query = $db->simple_select("users", "uid");
while($user = $db->fetch_array($query))
{
    $query = $db->simple_select("threads", "COUNT(tid) AS threads", "uid = '{$user['uid']}'");

    $numthreads = intval($db->fetch_field($query, "threads"));
    $db->update_query("users", array("numthreads" => $numthreads), "uid = '{$user['uid']}'");
}

First you should separate your ALTER statement. Execute the ALTER first and then do the rest. Alter table can be expensive in time if you have a big table. You can run it manually, using phpmyadmin or via shell (even better since there's no php timeout). which will give you the ability to not timeout.

Then remove the ALTER from the script and run it.

and then use:

$query = $db->simple_select("users", "uid");
while($user = $db->fetch_array($query))
{
    $query = $db->simple_select("threads", "COUNT(tid) AS threads", "uid = '{$user['uid']}'");

    $numthreads = intval($db->fetch_field($query, "threads"));
    $db->update_query("users", array("numthreads" => $numthreads), "uid = '{$user['uid']}'");
}
情域 2024-12-08 05:50:07

试试这个

ini_alter ("max_execution_time", 600000000);
$tmp = ini_get ( "max_execution_time" );
set_time_limit(600000000);

try this

ini_alter ("max_execution_time", 600000000);
$tmp = ini_get ( "max_execution_time" );
set_time_limit(600000000);
云之铃。 2024-12-08 05:50:07

人们无法更改最大执行时间的一个常见原因是他们的主机不允许他们这样做。确保他们不会阻止此操作。

A common reason that people are not able to change the max execution time is that their host does not allow them to. Make sure to find out that they do not block this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文