Yii2优化长时间运行的查询以降低超时时间

时间:2019-07-20 05:01:59

标签: php yii2

我正在尝试通过查询获取10000条以上的记录,但一直在超时

   $from ="2018-06-20 23:59";
    $to ="2018-06-28 00:00";
    //generate nairobi plant excell data
    $query = NaiTblTrucks::find()
        ->leftJoin('tbl_truck_history', 'tbl_truck_history.truck_id = tbl_trucks.id')
        //->where(['tbl_trucks.town' => 1])
        ->leftJoin('tbl_security_trucks', 'tbl_security_trucks.truck_id = tbl_trucks.id')
        ->andWhere(['IN', 'tbl_truck_history.status', [1]])
        ->andWhere(['NOT IN', 'tbl_trucks.truck_status', [13,6]])
        ->orderBy(['tbl_security_trucks.time_out' => SORT_DESC]);

    $query->andFilterWhere([
        'OR',
        ['IN',  'tbl_trucks.material',3]
    ]);

    $query->andWhere(['between', 'tbl_security_trucks.time_out', strtotime($from), strtotime($to)]);

现在正在使用dataprovider返回结果,例如

 $data = new ActiveDataProvider([
        'query' => $query,
        'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]],
        'pagination' =>false
    ]);

从上述日期到预期的数据可能非常大,也就是说一天中可以有1000多个记录。 我还尝试通过以下方式对请求进行分页:

    $totalrecords = $data->getTotals()//dataprovider get totals
    $gotcount = 0;
    $currentpage = 0;
    $fetched = [];

    for ($val= 0; $val<=$totalrecords;){
        $fetchedata = $this->getTrucks($query,10,$currentpage);
        array_push($fetched,$fetchedata);
        $gotcount += count($fetchedata);
        $val +=10;
        $currentpage += 1;
    }

 public function getTrucks($query, $pagesize, $currentPage){
    $data = new ActiveDataProvider([
        'query' => $query,
        'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]],
        'pagination' => [
            'pageSize' => 100,
            'page' => 0
        ],
    ]);

    return $data->getModels();

     }

但是以上两种尝试仍然给我超时。通过将日期缩短为一天就可以获取数据。如何进一步优化此设置以确保不超时。有更好的方法做到这一点,尤其是在处理大型数据集时。

稍后,在获取此数据后,将其与php电子表格一起导出到excell。

0 个答案:

没有答案