gpt4 book ai didi

php - Laravel 急切加载以最大程度地减少查询

转载 作者:行者123 更新时间:2023-11-29 23:46:51 33 4
gpt4 key购买 nike

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

存储库中的我的代码...

//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.

最佳答案

您可以在 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() 方法重新排列它们

关于php - Laravel 急切加载以最大程度地减少查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25879347/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com