Php 加快创建未来记录
我有一个Codeigniter系统,用户可以输入预约,并每周自动重复。如果原始约会设置为重复,系统将在当前日期前一个月自动创建约会 下面是模块函数。它能工作,但它很慢。非常感谢您的建议;有没有一种方法可以使用SQL在几个过程中实现这一点 问题在于嵌套循环,当系统有数百个重复约会要检查时,运行大约需要20秒 对于重复的约会,“repeats”字段设置为1,对于根据该字段创建的“child”约会,该字段设置为2Php 加快创建未来记录,php,mysql,codeigniter,Php,Mysql,Codeigniter,我有一个Codeigniter系统,用户可以输入预约,并每周自动重复。如果原始约会设置为重复,系统将在当前日期前一个月自动创建约会 下面是模块函数。它能工作,但它很慢。非常感谢您的建议;有没有一种方法可以使用SQL在几个过程中实现这一点 问题在于嵌套循环,当系统有数百个重复约会要检查时,运行大约需要20秒 对于重复的约会,“repeats”字段设置为1,对于根据该字段创建的“child”约会,该字段设置为2 function update_repeat_appointments($pup
function update_repeat_appointments($pupil_ID=NULL) // adds appointments 1 month ahead for all repeat appointments
{
if ($pupil_ID != NULL) {$this->db->where('timetable.pupil_ID', $pupil_ID);}
$this->db->where('repeats', 1);
$this->db->where('date >=', date('Y-m-d', strtotime("-1 month", strtotime("now"))));
$this->db->order_by("date", "asc");
$query = $this->db->get('timetable');
$repeatapps = $query->result();
$enddate = strtotime("+1 month", strtotime("now")); // Change this line to have repeat appointments go further into the future
//Loop over current repeat appointments
foreach ($repeatapps as $row) {
$startdate = strtotime($row->date);
$runningdate = $startdate;
$this->db->where('pupil_id', $row->pupil_id);
$this->db->where('repeats <>', 1);
$this->db->where('date >=', date('Y-m-d', strtotime("-1 month", strtotime("now"))));
$this->db->order_by("date", "asc");
$query = $this->db->get('timetable');
$subapps = $query->result();
while ($runningdate <= $enddate) {
// Check if there is an appointment in a future week for this pupil
$runningdate = strtotime("+1 week", $runningdate);
$found=false;
foreach ($subapps as $subrow) {
if (strtotime($subrow->date) == $runningdate) { //Matched appointment found, exit loop
$found=true;
break;
}
}
if ($found=false) {
//Add an appointment with relevant data, including future date
$data = array ( "staff_id" => $row->staff_id,
"pupil_id" => $row->pupil_id,
"venue_id" => $row->venue_id,
"group_id" => $row->group_id,
"notes" => $row->notes,
"date" => date('Y-m-d h:i:s', $runningdate),
"repeats" => 2, // is a sub_repeat
"root_ID" => $row->ID // Record ID of root appointment, allows bulk changes to it's repeats
);
$this->add_appointment($data);
}
}
}
function update\u repeat\u约会($pullow\u ID=NULL)//为所有重复约会提前1个月添加约会
{
if($poudior\u ID!=NULL){$this->db->where('timeline.poudior\u ID',$poudior\u ID);}
$this->db->where('repeats',1);
$this->db->where('date>=',date('Y-m-d',strottime('1个月',strottime('now')));
$this->db->order_by(“日期”、“asc”);
$query=$this->db->get('timeline');
$repeatapps=$query->result();
$enddate=strotime(“+1个月”,strotime(“现在”);//更改此行以使重复约会更深入到未来
//循环当前重复约会
foreach($repeatapps作为$row){
$startdate=strottime($row->date);
$runningdate=$startdate;
$this->db->where('pullow\u id',$row->pullow\u id);
$this->db->where('repeats',1);
$this->db->where('date>=',date('Y-m-d',strottime('1个月',strottime('now')));
$this->db->order_by(“日期”、“asc”);
$query=$this->db->get('timeline');
$subapps=$query->result();
而($runningdate)=$runningdate){//找到匹配的约会,退出循环
$found=true;
打破
}
}
如果($found=false){
//添加包含相关数据(包括未来日期)的约会
$data=数组(“staff\u id”=>$row->staff\u id,
“瞳孔id”=>$row->瞳孔id,
“场馆id”=>$row->场馆id,
“组id”=>$row->组id,
“注释”=>$row->notes,
“日期”=>日期($Y-m-d h:i:s',$runningdate),
“repeats”=>2,//是次重复
“root_ID”=>$row->ID//根约会的记录ID,允许对其重复进行批量更改
);
$this->add_约会($data);
}
}
}
这似乎不是一个“此特定脚本速度慢”的问题,而是一个“此特定脚本无法扩展,因为它一次执行了太多”的问题
我会假设从这看,这是一个cron脚本。它必须重新计算信息,不管是否有任何变化。重复的事件越多,此脚本花费的时间就越长
我会问一些这样的问题:
话虽如此,一个20秒的cron脚本并不像一个20秒的页面请求那么糟糕。当你分配负载时,总是倾向于用户体验。尝试纯MySQL路线。。。无法使其工作,因为它引用自身:在不存在的位置(从时间表中选择id,其中的时间表。学生id=时间表。学生id=时间表。学生id=时间表。学生id和时间选项卡)插入时间表(时间表。学生id,日期添加(时间表。日期,间隔7天))