简体   繁体   中英

Datatables very slow load on Codeigniter

I have to get data from fingerprint machine. Everything in my code run and works, but if I show all employee data, it loads very slow. I have create datatables by codeigniter and ajax call to view. My code like below

// 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));
}

This code runs and works, but need about 10 second for load. Can somebody help Me what I have to do to be fast this load?

UPDATE

after I analysis my code, the slow load data because datatables say "data was truncated". But when I load on small data(filtered), It was fine.

problem still same when I get all data from database.

To make it faster, you first need to know what is slow. To do that, you need to use debugging tools, like this one and timestamps .

Then, once you get logs with the duration of your processes, you can analyze it to know which part of your code is slow.

Then, try to optimize it. And if you can't, edit your question to focus on the slow part or post a new one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM