[英]Bulk MySQL update instead of PHP While loop
I am using PHP code to update week ending date which is on a different day for each subdomain.我正在使用 PHP 代码更新每个子域的不同日期的周结束日期。 Each subdomain date range varies.
每个子域日期范围各不相同。
My current code uses a while loop to update the week end date.我当前的代码使用 while 循环来更新周末日期。 This is very slow and I am looking for ideas to optimise it.
这很慢,我正在寻找优化它的想法。
Here is my current code:这是我当前的代码:
// Loop through and assign week end date to each user activity.
$nextWeek = $startDate; // initialise while loop condition
while ($nextWeek <= $endDate) {
$lastWeek = $nextWeek;
$nextWeek = Carbon::parse($lastWeek)->addWeek(1)->format('Y-m-d');
// update activity date to week end date
$query = 'UPDATE r_active_user_activities
SET week_end_date = "' . $nextWeek . '"
WHERE (activity_date > "' . $lastWeek . '"
AND activity_date <= "' . $nextWeek . '"
AND subdomain="' . $subdomain->subdomain . '" );';
$db->statement($query);
}
Just append all those update queries in a php variable ($query) and execute that outside the while loop.只需将所有这些更新查询附加到 php 变量 ($query) 中,并在 while 循环之外执行。 something like this.
像这样的东西。
$nextWeek = $startDate; // initialise while loop condition
$query = '';
while ($nextWeek <= $endDate) {
$lastWeek = $nextWeek;
$nextWeek = Carbon::parse($lastWeek)->addWeek(1)->format('Y-m-d');
// update activity date to week end date
$query .= 'UPDATE r_active_user_activities
SET week_end_date = "' . $nextWeek . '"
WHERE (activity_date > "' . $lastWeek . '"
AND activity_date <= "' . $nextWeek . '"
AND subdomain="' . $subdomain->subdomain . '" );';
}
$db->statement($query);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.