With the Debugbar debugging tool installed for the Laravel project, a toolbar opens at the bottom of the page in debug mode, where you can view performance statistics and, most importantly, print the SQL statements and debug content that the page executes. The following uses the Debugbar tool to find the optimization point for a SQL query.
Optimizing Page SQL queries
Here are two models, destination models and travel models, they are one-to-many relationships defined in the model destination.php of the destination models as follows:
Php
/**
* Define destination-a one-to-many relationship of travels
* @return \illuminate\database\eloquent\relations\hasmany
*/
Public Function Travel ()
{
return $this->hasmany (travel::class, ' destination_id ');
}
In order to count the number of travels in each destination, the Laravel model uses a very useful feature appends, the definition appends array is the custom table field:
Php
/**
* Custom Fields
* @var Array
*/
protected $appends = [
' URL ',
' First_travel_url ',
' Total ',
];
Here total refers to the destination of the number of travel notes, the code before the optimization is such:
/**
* Destination Travel number
* @return Mixed
*/
Public Function Gettotalattribute ()
{
return $this->travel ()->count ();
}
This results in traversing the total number of travel destinations and increasing the frequency of SQL statements. This would have been a good feature, but additional query overhead might be added if you were to query the database. The Debugbar tool prints all the SQL statements that the page executes on the page, as shown in the figure:
Obviously it needs to be optimized here, and it needs to be counted by a group by destination, just a single SQL statement.
Used in Laravel
Using GROUP by for statistics
Add a method to the model of the travel travel.php:
/**
* Get the total number of travels per destination
* @return Array
*/
Public Function gettravelnumsgroupbydestination ()
{
$travelNums = [];
$result = $this->groupby (' destination_id ')->get ([' destination_id ', Db::raw (' count (*) as Total ')];
foreach ($result as $item) {
$travelNums [$item->destination_id] = $item->total;
}
return $travelNums;
}
Here $result returns a Collection object with a collection of Travel classes in which the SQL statement actually executes: select DESTINATION_ID, COUNT (*) as total from Travel Group by destination_id
Group-by-select-count
Call this method in the index method of the page controller travelcontroller.php, and then assign the value to the template.
Php
/**
* Travel Home
* @return \illuminate\contracts\view\factory|\illuminate\view\view
*/
Public Function Index ()
{
$data = Cache::remember (' Travel.index ', self::cache_time, function () {
$destinationList = $this->destination->getlist (12);
$travelList = $this->travel->latest (' begin_date ')->take ()->get ();
$travelNums = $this->travel->gettravelnumsgroupbydestination ();
return [
' Destinationlist ' => $destinationList,
' Travellist ' => $travelList,
' Travelnums ' => $travelNums
];
});
Return view (' Travel.index ', $data);
}
Here the database query results are cached, only in the cache expiration will query the database.
After making the appropriate modifications in the template, this SQL is optimized for performance, which reduces the number of SQL queries if more queries are available.
Optimized after page execution sql:
Through the Debugbar tool can very good grasp of the performance, not only that, he is a good debugging tool, we can print log way to debug the code, can also use Debugbar to provide debugging methods to print content, very convenient, using laravel development of the necessary debugging tools.