laravel渴望加载以最小化查询

时间:2014-09-16 22:22:45

标签: php mysql laravel laravel-4 domain-driven-design

我正在使用带有DDD模式的laravel,这是我为网站制作一些分析报告的代码......

我的代码来自存储库...

//Abstract Entity
  public function getFirstBy($key, $value, array $with = array())
  {
    return $this->repository->getFirstBy($key, $value, $with);
  }


//Link Entity
$link = parent::getFirstBy('hash', $data['hash'], ['visits']);


//prepare report
          $report = [];

          //get dates difference
          $date1 = new \DateTime($data['from']);
          $date2 = new \DateTime($data['to']);
          $date_diff = $date2->diff($date1)->format("%a");

          $time_visits = $link->visits()->whereRaw('DATE(created_at) BETWEEN ? AND ?', array($date1, $date2))->groupBy('created_at')->orderBy('created_at', 'ASC')->get();

          //Total Visits
          $report['total_visits'] = $time_visits->count();

    //country visits
          $country_visits = $link->visits()->whereRaw('DATE(created_at) BETWEEN ? AND ?', array($date1, $date2))->orderBy('country_iso', 'ASC')->groupBy('country_iso')->get();

    //referrers
          $referer_visits = $link->visits()->whereRaw('DATE(created_at) BETWEEN ? AND ?', array($date1, $date2))->where('source', '!=', '')->orderBy('source', 'ASC')->groupBy('source')->get();

    //browsers
          $browser_visits = $link->visits()->whereRaw('DATE(created_at) BETWEEN ? AND ?', array($date1, $date2))->where('browser', '!=', '')->orderBy('browser', 'ASC')->groupBy('browser')->get();

    //os
          $os_visits = $link->visits()->whereRaw('DATE(created_at) BETWEEN ? AND ?', array($date1, $date2))->where('os', '!=', '')->orderBy('os', 'ASC')->groupBy('os')->get();


The code is working perfectly fine but executing 6 mysql queries which could be a big problem for larger usage... 

**sql queries dump...**

    select * from `link_visits` where `link_visits`.`link_id` = '4' and DATE(created_at) BETWEEN '2014-09-15 00:00:00' AND '2014-09-17 00:00:00' group by `created_at` order by `created_at` asc

    select * from `link_visits` where `link_visits`.`link_id` = '4' and DATE(created_at) BETWEEN '2014-09-15 00:00:00' AND '2014-09-17 00:00:00' group by `country_iso` order by `country_iso` asc

    select count(*) as total_visits, medium from `link_visits` where `link_visits`.`link_id` = '4' and DATE(created_at) BETWEEN '2014-09-15 00:00:00' AND '2014-09-17 00:00:00' group by `medium` order by `medium` asc

    select * from `link_visits` where `link_visits`.`link_id` = '4' and DATE(created_at) BETWEEN '2014-09-15 00:00:00' AND '2014-09-17 00:00:00' and `source` != '' group by `source` order by `source` asc

    select * from `link_visits` where `link_visits`.`link_id` = '4' and DATE(created_at) BETWEEN '2014-09-15 00:00:00' AND '2014-09-17 00:00:00' and `browser` != '' group by `browser` order by `browser` asc

    select * from `link_visits` where `link_visits`.`link_id` = '4' and DATE(created_at) BETWEEN '2014-09-15 00:00:00' AND '2014-09-17 00:00:00' and `os` != '' group by `os` order by `os` asc

Please suggest some good ways to optimize it better. thanks, i really need advanced help with this.

1 个答案:

答案 0 :(得分:0)

你可以在ex。

的关系中使用子查询

假设$ link是你的模型对象

$link->with(['Visits'=>function($q){
       $q->whereRaw('DATE(created_at) BETWEEN ? AND ?', array($date1,$date2))->where('browser', '!=', '')->orderBy('browser', 'ASC')->groupBy('browser');
}])->get();

内部查询将相应地过滤所有记录,内部查询将仅应用于访问模型

我的建议是首先获取所有记录而不进行任何过滤,然后使用filter()方法在集合对象中过滤它们!因此,它将仅使用1个查询来获取所有记录,然后您只使用filter()方法重新排列它们