我必须从指纹机中获取数据。我的代码中的所有内容都可以运行和工作,但如果我显示所有员工数据,它的加载速度会非常慢。我已经通过codeigniter和ajax调用创建了数据表来查看。我的代码如下
// Datatables Variables
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$year = $this->input->post("year");
$month = $this->input->post("month");
$dept = $this->input->post("dept");
$where = $year.'-'.$month.'-';
$shifts = $this->m_general->get_data('tb_shift')->result();
$rows = $this->m_employee->attendance($where)->result_array();
if($dept=='5'){
$pin_emp = $this->m_general->get_data('tb_employee')->result();
}else{
$pin_emp = $this->m_general->check_data('tb_employee',array('emp_dept'=>$dept))->result();
}
$loc_machine= $this->m_general->get_data('tb_pkm')->result();
$roaster = $this->m_employee->list_schedule_emp($where)->result_array();
$holiday = $this->m_general->get_data('tb_holiday')->result();
$total_days = tglakhir($year,$month);
$dates = array();
$people = array();
$data = array();
foreach ($rows as $row) {
if (empty($row['date']) || empty($row['pin'])) {
continue; // no date or sn, nothing to print for this row...
}
// in array assures unique values
if (!in_array($row['date'], $dates)) {
$dates[] = $row['date'];
}
// this may seem convoluted, take your time to understand
$people[$row['date']][$row['pin']] = $row;
$array_emp[$row['date']][$row['pin']][] = $row;
}
foreach ($roaster as $rs) {
if (empty($rs['date']) || empty($rs['id_nip'])) {
continue; // no date or sn, nothing to print for this row...
}
// this may seem convoluted, take your time to understand
$get_rs[$rs['date']][$rs['id_nip']] = $rs;
$dt_rs[$rs['date']][$rs['id_nip']][] = $rs;
}
foreach($pin_emp AS $q){
$array_pin[]=$q->emp_pin;
$details[$q->emp_pin] = $q;
}
foreach($holiday AS $hday){
$get_hday[$hday->date_holiday] = $hday;
}
foreach($loc_machine AS $list_machine){
$lm[$list_machine->id_machine] = $list_machine;
}
for($m=1; $m <= $total_days; $m++){
if($m<10){
$m = '0'.$m;
}else{
$m = $m;
}
$date[]=$m;
}
foreach($shifts AS $shift_e){
$array_shift[$shift_e->id_shift] = $shift_e;
$ar_shift[]=$shift_e->id_shift;
}
foreach ($array_pin AS $id){
$name = $details[$id]->emp_name;
$shift = $details[$id]->emp_shift;
$id_machine = $details[$id]->emp_reg_sn;
if(isset($lm[$id_machine])){
$loc_machine= $lm[$id_machine]->pkm_name;
}
$date_range = array();
foreach($date AS $date_col){
$day = date('D', strtotime($year.'-'.$month.'-'.$date_col));
$today = date('Y-m-d');
$full_day = $year.'-'.$month.'-'.$date_col;
$s_in = '';
$s_out = '';
$time_in = '';
$time_out = '';
$t_in = '';
$t_out = '';
$finger_tap = '';
$diff_in = '';
$diff_out = '';
$diff = null;
$diff2 = null;
$late = '00:00:00';
$r = $date_col;
$title = '';
$date_holiday= '';
if (ISSET($get_hday[$full_day])==TRUE) {
$date_holiday = $get_hday[$full_day]->date_holiday;
$title = $get_hday[$full_day]->note_holiday;
}
foreach ($dates as $key) {
$shift_detail= $array_shift[$shift]->code_shift;
if(isset($get_rs[$full_day][$id]['id_shift_emp']) && $get_rs[$full_day][$id]['id_shift_emp']!=''){
$shift = $get_rs[$full_day][$id]['id_shift_emp'];
$shift_detail = $array_shift[$shift]->code_shift;
}
if (isset($people[$key][$id]['date']) && $people[$key][$id]['date'] == $key) {
foreach ($array_emp[$key][$id] AS $dtl){
if(isset($get_rs[$key][$id]['id_shift_emp']) && $get_rs[$key][$id]['id_shift_emp']!=''){
$shift = $get_rs[$key][$id]['id_shift_emp'];
}
$shift = $dtl['emp_shift'];
$date_time = $dtl['date_time'];
if(($shift==5)OR($shift==7)OR($shift==9)OR($shift==11)OR($shift==12)OR($shift==14)){
$r = $date_col+1;
}
if(substr($key,8,2)==$date_col){
$status_code= $dtl['status_code'];
$status_note= $dtl['att_status'];
$first_in = strtotime($year.'-'.$month.'-'.$date_col.' '.$array_shift[$shift]->first_check);
$last_out = strtotime($year.'-'.$month.'-'.$r.' '.$array_shift[$shift]->last_check);
$shift_in = $array_shift[$shift]->in_shift;
$shift_out = $array_shift[$shift]->out_shift;
$s_in = strtotime($year.'-'.$month.'-'.$day.' '.$shift_in);
$s_out = strtotime($year.'-'.$month.'-'.$day.' '.$shift_out);
$finger_tap = strtotime($date_time);
$shift_detail= $array_shift[$shift]->code_shift;
foreach($ar_shift AS $id_shift){
if ($shift==$id_shift) {
$currdiff = abs($finger_tap - $first_in);
if (is_null($diff) || $currdiff < $diff) {
$diff = $currdiff;
$time_in = date('H:i:s',strtotime($date_time));
$t_in = strtotime($date_time);
if(($t_in-$s_in)>0){
$diff_in = gmdate('H:i:s',$t_in-$s_in);
}else{
$diff_in = $late;
}
}
$currdiff2 = abs($finger_tap - $last_out);
if (is_null($diff2) || $currdiff2 < $diff2) {
$diff2 = $currdiff2;
$time_out = date('H:i:s',strtotime($date_time));
$t_out = strtotime($date_time);
if(($t_out-$s_out)>0){
$diff_out = $late;
}else{
$diff_out= gmdate('H:i:s',$s_out-$t_out);
}
}
}
}
}
if(($shift==5)OR($shift==7)OR($shift==9)OR($shift==11)OR($shift==12)OR($shift==14)){
if ($day==$r) {
$shift_out = $array_shift[$shift]->out_shift;
$s_out = strtotime($year.'-'.$month.'-'.$r.' '.$shift_out);
$finger_tap = strtotime($date_time);
foreach($ar_shift AS $id_shift){
if ($shift==$id_shift) {
$currdiff2 = abs($finger_tap - $last_out);
if (is_null($diff2) || $currdiff2 < $diff2) {
$diff2 = $currdiff2;
$time_out = date('H:i:s',strtotime($date_time));
$t_out = strtotime($date_time);
if(($t_out-$s_out)>0){
$diff_out = $late;
}else{
$diff_out= gmdate('H:i:s',$s_out-$t_out);
}
}
}
}
}
}
$status = '<a href="javascript:change_status_att(''.$id.'',''.$name.'',''.$full_day.'')" class="btn btn-xs btn-dino" data-toggle="tooltip" data-placement="top" title="Belum hadir"><i class="material-icons col-green">check_box_outline_blank</i>'.$shift_detail.'</a>';
if($year.'-'.$month.'-'.$date_col < $today){
$status = '<a href="javascript:change_status_att(''.$id.'',''.$name.'',''.$full_day.'')" class="btn btn-xs btn-dino" data-toggle="tooltip" data-placement="top" title="Alpha"><i class="material-icons col-red">fiber_manual_record</i>'.$shift_detail.'</a>';
}
if(($day == 'Sun') OR ($day == 'Sat')){
$status='<a href="javascript:change_status_att(''.$id.'',''.$name.'',''.$full_day.'')" class="btn btn-md bg-red waves-effect" data-toggle="tooltip" data-placement="top" title="Libur">off</button>';
}
if($date_holiday == $full_day){
$status = '<a href="javascript:change_status_att(''.$id.'',''.$name.'',''.$full_day.'')" class="btn btn-xs btn-dino" data-toggle="tooltip" data-placement="top" title="'.$title.'"><i class="material-icons col-red">today</i>OFF</a>';
}
if($time_in!=''){
if($time_in==$time_out){
$status = '<a href="javascript:change_status_att(''.$id.'',''.$name.'',''.$full_day.'')" class="btn btn-xs btn-dino" data-toggle="tooltip" data-placement="top" title="Absen"><i class="material-icons col-red">brightness_6</i>'.$shift_detail.'</a>';
}
if ($status_code!='') {
$status = '<a href="javascript:change_status_att(''.$id.'',''.$name.'',''.$full_day.'')" class="btn btn-xs btn-dino" data-toggle="tooltip" data-placement="top" title="'.$status_note.'"><i class="material-icons col-orange">sim_card_alert</i>'.$shift_detail.'</a>';
}
}
if($time_in!=$time_out){
$status = '<a href="javascript:change_status_att(''.$id.'',''.$name.'',''.$full_day.'')" class="btn btn-xs btn-dino" data-toggle="tooltip" data-placement="top" title="Complete"><i class="material-icons col-green">check_box</i>'.$shift_detail.'</a>';
}
if($time_in=='00:00:00'){
$status = '<a href="javascript:change_status_att(''.$id.'',''.$name.'',''.$full_day.'')" class="btn btn-xs btn-dino" data-toggle="tooltip" data-placement="top" title="'.$status_note.'"><i class="material-icons col-orange">sim_card_alert</i>'.$shift_detail.'</a>';
}
}
}
}
$date_range[] = $status;
}
$first = array(
'<a href="javascript:view_att_emp('details',''.$id.'',''.$year.'',''.$month.'')">'.$id.'</a>',
$name,
$loc_machine
);
$data[] = array_merge($first,$date_range);
}
$output = array(
"draw" => $draw,
"recordsTotal" => '',
"recordsFiltered" =>'' ,
"data" => $data
);
$this->output->set_content_type('application/json')->set_output(json_encode($output));
}
此代码可以运行并正常工作,但需要大约10秒的加载时间。有人能帮我做些什么吗?
更新
在我分析了我的代码后,加载数据的速度很慢,因为数据表上写着"数据被截断了"。但当我加载小数据(过滤(时,它很好。
当我从数据库中获取所有数据时,问题仍然相同。
要使它更快,您首先需要知道什么是慢的。要做到这一点,您需要使用调试工具,比如这个工具和时间戳。
然后,一旦您获得了包含进程持续时间的日志,就可以对其进行分析,以了解代码的哪一部分速度较慢。
然后,试着优化它。如果你做不到,编辑你的问题,把重点放在慢的部分,或者发布一个新的问题。