0

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

从我的电脑上加载这个页面非常慢,在实时系统中一旦在服务器上似乎需要更少的时间,但我认为还不够快。

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

它获取每个城市的连接的第一段代码,我们使用的是多模式 PostgreSQL 数据库,因此对于每个URL/city1 URL/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'));
4

1 回答 1

0

50 KB 的数据(如评论中所述)非常少,因此将数据从服务器传输到客户端并不是您的情况的瓶颈。您的数据并不像您的问题标题所暗示的那样“大”。

我会把这段代码boot放在app\Providers\AppServiceProvider.php. 然后每个数据库查询所花费的时间将被登录storage\logs

\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 来做到这一点,但我也认为在您的情况下,可能有一些方法可以加快您的查询速度,以便您可以简单地等待数据并在单个请求中加载所有内容。

您的代码非常复杂,我建议您考虑将其拆分为更小的函数并使用更多注释,但这不会使您的代码运行得更快。

于 2019-09-27T10:04:42.543 回答