如何降低数据表速度

时间:2018-06-30 06:08:17

标签: database codeigniter datatables

这是我的代码;我正在使用jQuery数据表,我仅从一个表中获取记录,但是当我试图在那时搜索记录时,加载记录几乎不需要5秒,而如何减少数据获取时间却需要14秒使用searchDelay: 10来降低速度,但是我仍然面临着同样的问题。

控制器:

function customer_is_com_wise_datatable() {
        $post_data = $this->input->post();
        $id = $this->session->userdata('teen_is_logged_in')['customer_id'];
        $query = $this->app_model->get_categories_customer_list($id);                    
        $parent_data = $this->array_flatten($query,null);
        $customer_id_arr = array();

        if(!empty($parent_data)) {
            foreach ($parent_data as $parent) {
                $customer_id_arr[] = $parent['customer_id'];
            }
        }

        $config['table'] = 'customers c';
        $config['select'] = 'c.*,c1.name as parent_name';
        $config['joins'][] = array('join_table' => 'customers c1', 'join_by' => 'c1.customer_id = c.parent_id', 'join_type' => 'left');
        $config['column_order'] = array(null, 'c.name','c.phone1','c.phone2','c.city','c.current_balance','c1.name');
        $config['column_search'] = array('c.name','c.phone1','c.phone2','c.city','c.current_balance','c1.name','c.login_type'); 
        $config['order'] = array('c.customer_id' => 'desc');

        if(!empty($customer_id_arr)) {
            $customer_ids = implode(',', $customer_id_arr);
            $config['custom_where'] = 'c.customer_id IN('.$customer_ids.')';
        }

        if($post_data['is_computer'] == 2) { } 
        else {
            $config['wheres'][] = array('column_name' => 'c.is_computer', 'column_value' => $post_data['is_computer']);
        }

        $login_types = array();

        if($post_data['main_dealer'] == 'true') {
            $login_types[] = LOGIN_TYPE_MAIN_DEALER;
        }

        if($post_data['dealer'] == 'true') {
            $login_types[] = LOGIN_TYPE_DEALER;
        }

        if($post_data['customer'] == 'true') {
            $login_types[] = LOGIN_TYPE_CUSTOMER;
        }

        if(!empty($login_types)) {
            $login_type = implode(',', $login_types);
            $config['custom_where'] = 'c.login_type IN('.$login_type.')';
        }

        $this->load->library('datatables', $config, 'datatable');
        $list = $this->datatable->get_datatables();
        $data = array();
//        echo '<pre>'; print_r($list); exit;

        foreach ($list as $customer) {
            $row = array();

            $action = '';
            $action .= '<a href="' . base_url("customer/add/" . $customer->customer_id) . '"><span class="edit_button glyphicon glyphicon-edit data-href="#"" style="color : #419bf4" >&nbsp;</span></a>';
            //$action .= '<a href="" customer_id="' . $customer->customer_id . '" title="Click to Edit"><span class="glyphicon glyphicon-edit" style="color : #419bf4">&nbsp;</span></a>';
            $action .= '<a href="javascript:void(0);" class="delete_button" data-href="' . base_url('customer/delete_customer/'.$customer->customer_id) . '" title="Click to Delete"><span class="glyphicon glyphicon-trash" style="color : red">&nbsp;</span></a>';
            if($customer->active_deactive == 1){
                $action .= '<a href="javascript:void(0);" class="deactive_btn" data-href="' . base_url('customer/active_deactive_customer/'.$customer->customer_id) . '" title="Click to Deactive" style="margin-right: 5px;"><span class="fa fa-check text-success">&nbsp;</span></a> ';
            } else {
                $action .= '<a href="javascript:void(0);" class="active_btn" data-href="' . base_url('customer/active_deactive_customer/'.$customer->customer_id) . '" title="Click to Active" style="margin-right: 8px;"><span class="fa fa-close text-danger">&nbsp;</span></a> ';
            }
            if($customer->private_room_allow == 1){
                $action .= '<a href="javascript:void(0);" class="disallow_private_room_btn" data-href="' . base_url('customer/private_room_allow_customer/'.$customer->customer_id) . '" title="Click to Disallow Private Room"><span class="fa fa-home text-success">&nbsp;</span></a> ';
            } else {
                $action .= '<a href="javascript:void(0);" class="allow_private_room_btn" data-href="' . base_url('customer/private_room_allow_customer/'.$customer->customer_id) . '" title="Click to Allow Private Room"><span class="fa fa-home text-danger">&nbsp;</span></a> ';
            }
            $row[] = $action;
            $row[] = $customer->name;
            if($customer->login_type == '1'){
                $customer->login_type = 'Admin';
            } elseif ($customer->login_type == '2') {
                $customer->login_type = 'Main Dealer';
            } elseif ($customer->login_type == '3') {
                $customer->login_type = 'Dealer';
            } else {
                if($customer->is_computer == '0'){
                    $customer->login_type = 'Customer';
                } else {
                    $customer->login_type = 'Customer (Computer)';
                }
            }
            $row[] = $customer->login_type;
            $row[] = $customer->parent_name;
            $row[] = $customer->cust_comm . "%";
            $row[] = $customer->commission_amount;
            $row[] = $customer->current_balance;
            $row[] = $customer->phone1;
            $row[] = $customer->phone2;
            $row[] = $customer->city;
            $data[] = $row;

        }

        $output = array(
            "draw" => $_POST['draw'],
            "recordsTotal" => $this->datatable->count_all(),
            "recordsFiltered" => $this->datatable->count_filtered(),
            "data" => $data,
        );
        echo json_encode($output);

视图:使用数据表

$("#is_computer").select2();
        var table = $('#customer_list').DataTable({
            "processing": true,
            "ordering": true,
            "serverSide": true,
            searchDelay: 10,
            "length" : 10,
            "order": [[1, 'desc']],
//            "searching": true,
            retrieve: true,
            //"aaSorting": [[1, 'desc']],
            "ajax": {
                "url": "<?php echo site_url('customer/customer_is_com_wise_datatable') ?>",
                "type": "POST",
                "data": function(d){
                    d.is_computer = $("#is_computer").val();
                    d.main_dealer = $("#main_dealer").prop('checked');
                    d.dealer = $("#dealer").prop('checked');
                    d.customer = $("#customer").prop('checked');
                } 
            },

            "scrollY": 450,
            "scrollX": true,
            "scroller": {
                "loadingIndicator": true
            },
            //            "sScrollX": "100%",
            "sScrollXInner": "110%",
            stateSave: true,
            "columnDefs": [{
                "className": "dt-right",
                "targets": [4,5,6],
                "orderable": false
            }]  

        });

0 个答案:

没有答案