Laravel通过视图传递大数据,加载时间慢



我需要通过Laravel中的视图传递大量数据,似乎需要相当多的观察,有人可以建议改进此代码的正确方法/通过视图传递这么多数据吗?

从我的PC加载此页面非常慢,在实时系统中,一旦在服务器上似乎花费的时间更少,但我认为仍然不够快。

我看到有这种依赖项应该有助于实时系统composer install --optimize-autoloader --no-dev但我还没有安装它,这将有助于我的速度,因为我有很多类要初始化?

第一段代码它为每个城市获取连接,我们使用多模式 PostgreSQL DB,因此对于每个URL/city1URL/city2等,它需要找到从哪个数据库获取数据。

/**
* Create objects that we will need to query
*/
$this->Response = new Response;
$this->Response->initialize($connection);
$this->Responder = new Responder;
$this->Responder->initialize($connection);
$this->Interests = new lutInterest;
$this->Interests->initialize($connection);
$this->Issue = new lutIssue;
$this->Issue->initialize($connection);
$this->Time = new lutTime;
$this->Time->initialize($connection);

那么我需要检查schemeid参数是否存在及其值,然后从数据库运行相关查询

/**
* check the schemeid paramenter
* Do the queries
*/
if ($_GET['schemeid'] == 0)
{
$totalIssues = $this->Response->orderby('issueid')->groupby('issueid')->select('issueid', DB::raw('count(*) as total'))->get();
$totalTimes = $this->Response->orderby('timeid')->groupby('timeid')->select('timeid', DB::raw('count(*) as total'))->get();
$totalParticipants = DB::connection($connection)->table('responder_interests')->groupby('interestid')->orderby('interestid')->select('interestid', DB::raw('count(*) as total'))->get();
$totalResponses = $this->Response->select(DB::raw('count(responseid) as total'),DB::raw("EXTRACT('year' FROM created_at) as year,EXTRACT('month' FROM created_at) as month"))->groupby('year','month')->orderby('year','asc')->orderBy('month','asc')->get();
$totalRespArray = $this->Response->select(DB::raw('count(*) as total'))->get();
$totalresponders = $this->$responders->count();
}
else
{
$responders = $this->Response->where('scheme_id',$_GET['schemeid'])->groupby('responderid')->orderby('responderid')->select('responderid')->get();
$respondersIDs = $responders->map(function ($data) { return $data->responderid; });
$totalIssues = $this->Response->orderby('issueid')->groupby('issueid')->where('scheme_id', $_GET['schemeid'])->select('issueid', DB::raw('count(*) as total'))->get();
$totalTimes = $this->Response->orderby('timeid')->groupby('timeid')->where('scheme_id', $_GET['schemeid'])->select('timeid', DB::raw('count(*) as total'))->get();
$totalParticipants = DB::connection($connection)->table('responder_interests')->groupby('interestid')->orderby('interestid')->wherein('responderid',$respondersIDs)->select('interestid', DB::raw('count(*) as total'))->get();
$totalResponses = $this->Response->where('scheme_id',$_GET['schemeid'])->select(DB::raw('count(responseid) as total'),DB::raw("EXTRACT('year' FROM created_at) as year,EXTRACT('month' FROM created_at) as month"))->groupby('year','month')->orderby('year','asc')->orderBy('month','asc')->get();
$totalRespArray = $this->Response->where('scheme_id',$_GET['schemeid'])->select(DB::raw('count(*) as total'))->get();
$totalresponders = $responders->count();
}

最后运行几个函数来正确格式化数据并能够在图表中正确显示。

/**
* Convert issue data
*/
$categoryData = $totalIssues->map(function ($data) { return $data->total; });
$categoryLabels = $totalIssues->map(function ($data) { return $this->Issue->where('issueid',$data->issueid)->first()->shortdesc; });
$categoryBorder = $totalIssues->map(function ($data) { return $this->Issue->where('issueid',$data->issueid)->first()->color; });
$categoryColors = $categoryBorder->map(function ($data) { return implode('', [str_replace('rgb', 'rgba' ,explode(" ", $data)[0]),explode(" ", $data)[1], str_replace(')', ',0.2)' ,explode(" ", $data)[2])] );});
$categoryColorsHover = $categoryBorder->map(function ($data) { return implode('', [str_replace('rgb', 'rgba' ,explode(" ", $data)[0]),explode(" ", $data)[1], str_replace(')', ',0.5)' ,explode(" ", $data)[2])] );});
/**
* Convert time data
*/
$timeData = $totalTimes->map(function ($data) { return $data->total; });
$timeLabels = $totalTimes->map(function ($data) { return $this->Time->where('timeid',$data->timeid)->first()->shortdesc; });
$timeBorder = $totalTimes->map(function ($data) { return $this->Time->where('timeid',$data->timeid)->first()->color; });
$timeColors = $timeBorder->map(function ($data) { return implode('', [str_replace('rgb', 'rgba' ,explode(" ", $data)[0]),explode(" ", $data)[1], str_replace(')', ',0.2)' ,explode(" ", $data)[2])] );});
$timeColorsHOver = $timeBorder->map(function ($data) { return implode('', [str_replace('rgb', 'rgba' ,explode(" ", $data)[0]),explode(" ", $data)[1], str_replace(')', ',0.5)' ,explode(" ", $data)[2])] );});
/**
* Convert the interests data
*/
$participantData = $totalParticipants->map(function ($data) { return $data->total; });
$participantLabels = $totalParticipants->map(function ($data) { return $this->Interests->where('interestid',$data->interestid)->first()->shortdesc; });
$participantBorder = $totalParticipants->map(function ($data) { return $this->Interests->where('interestid',$data->interestid)->first()->color; });
$participantColors = $participantBorder->map(function ($data) { return implode('', [str_replace('rgb', 'rgba' ,explode(" ", $data)[0]),explode(" ", $data)[1], str_replace(')', ',0.2)' ,explode(" ", $data)[2])] );});
$participantColorsHover = $participantBorder->map(function ($data) { return implode('', [str_replace('rgb', 'rgba' ,explode(" ", $data)[0]),explode(" ", $data)[1], str_replace(')', ',0.5)' ,explode(" ", $data)[2])] );});
/**
* Convert the responses data
*/
$responseData = $totalResponses->map(function ($data) { return $data->total; });
$responseLabels = $totalResponses->map(function ($data) { return date("M", mktime(0, 0, 0, $data->month,1)).' | '.substr($data->year,2); });
/**
* Convert the total amount of responses data
*/
$totalResp = $totalRespArray->map(function ($data) { return $data->total; });
/**
* send the issues and times for the popup
*/
$lutissue = $this->Issue->select('issueid', 'description')->orderby('sort')->get();
$luttime = $this->Time->select('timeid', 'description')->orderby('sort')->get();

将所有内容传递到视图中

return view('dasboard')->with(compact('title', 'schemeDocuments', 'schemes',
'categoryData', 'categoryLabels', 'categoryBorder', 'categoryColors', 'categoryColorsHover','timeData', 'timeLabels',
'timeBorder', 'timeColors', 'timeColorsHOver', 'participantData', 'participantLabels', 'participantBorder', 'participantColors',
'participantColorsHover', 'responseData','responseLabels','totalResp', 'lutissue', 'luttime', 'totalresponders'));

50 KB 的数据(如注释中所述(很少,因此将数据从服务器传输到客户端并不是您情况下的瓶颈。您的数据并不像问题标题所暗示的那样"大"。

我将这段代码放在appProvidersAppServiceProvider.phpboot方法中。然后,每个数据库查询所花费的时间将记录在storagelogs中。

DB::listen(function ($query) {
Log::info($query->time . ' milliseconds for ' . $query->sql);
});

您还可以计算和记录任意代码段的执行时间:

$start = microtime(true);
// This may be anything between a single line of code and your whole script
Log::info(microtime(true) - $start); // This is in seconds

找出需要这么长时间的原因,然后尝试优化查询(或其他代码段(。

您还会询问是否有替代使用 AJAX 在加载数据之前呈现 HTML 的方法。我强烈建议您学习如何使用 AJAX 执行此操作,但我也认为在您的情况下,可能有办法加快查询速度,以便您可以简单地等待数据并在单个请求中加载所有内容。

你的代码非常复杂,我建议你考虑把它分成更小的函数并使用更多的注释,尽管这不会让你的代码运行得更快。

最新更新