feat: Optimize query.
This commit is contained in:
@ -11,6 +11,7 @@ use Auth;
|
|||||||
use App\Responses;
|
use App\Responses;
|
||||||
use App\Helper;
|
use App\Helper;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Carbon\CarbonPeriod;
|
||||||
use App\Models\UserLogs;
|
use App\Models\UserLogs;
|
||||||
use Illuminate\Support\Facades\Crypt;
|
use Illuminate\Support\Facades\Crypt;
|
||||||
use Illuminate\Contracts\Encryption\DecryptException;
|
use Illuminate\Contracts\Encryption\DecryptException;
|
||||||
@ -330,25 +331,45 @@ class ReportsController extends Controller
|
|||||||
$to_date = $req->input('to_date') - Helper::TIMEFIX;
|
$to_date = $req->input('to_date') - Helper::TIMEFIX;
|
||||||
$vid = $req->input('vid');
|
$vid = $req->input('vid');
|
||||||
|
|
||||||
|
// Create a period between the two timestamps
|
||||||
|
$period = CarbonPeriod::create(
|
||||||
|
Carbon::createFromTimestamp($from_date)->startOfMonth(),
|
||||||
|
'1 month',
|
||||||
|
Carbon::createFromTimestamp($to_date)
|
||||||
|
);
|
||||||
|
$subQueryParts = [];
|
||||||
|
foreach ($period as $date) {
|
||||||
|
$tableName = "tracks_" . $date->format('ym');
|
||||||
|
|
||||||
|
// We filter inside each table so the DB uses indexes immediately
|
||||||
|
$subQueryParts[] = "SELECT id, vhc_id, crt_d, speed
|
||||||
|
FROM $tableName
|
||||||
|
WHERE crt_d BETWEEN $from_date AND $to_date
|
||||||
|
AND action = 'location' and speed>60
|
||||||
|
";
|
||||||
|
}
|
||||||
|
$qString = "(" . implode(' UNION ALL ', $subQueryParts) . ")";
|
||||||
|
// dd("qString", $qString);
|
||||||
try {
|
try {
|
||||||
$list = DB::select("SELECT
|
$list = DB::select("SELECT
|
||||||
tv.name, tv.nopol1,
|
tv.name,
|
||||||
t.crt_d, t.speed, tgta.fulladdress,
|
tv.nopol1,
|
||||||
tvd.speed_limit, tvd.pool_code, tvd.dc_code
|
t.crt_d,
|
||||||
from
|
t.speed,
|
||||||
t_gps_tracks t
|
tgta.fulladdress,
|
||||||
left join t_vehicles tv on tv.id = t.vhc_id
|
tvd.speed_limit,
|
||||||
left join t_vehicles_detail tvd on tvd.vid = tv.id
|
tvd.pool_code,
|
||||||
left join t_gps_tracks_address tgta on tgta.master_id = t.id
|
tvd.dc_code
|
||||||
|
FROM
|
||||||
|
$qString t
|
||||||
|
INNER JOIN t_vehicles tv ON tv.id = t.vhc_id
|
||||||
|
INNER JOIN t_vehicles_detail tvd ON tvd.vid = tv.id
|
||||||
|
LEFT JOIN t_gps_tracks_address tgta ON tgta.master_id = t.id
|
||||||
WHERE
|
WHERE
|
||||||
t.action = 'location'
|
if(? , tv.id = ? , 1=1)
|
||||||
and t.speed != 0
|
AND t.speed > tvd.speed_limit
|
||||||
AND t.crt_d BETWEEN ? AND ?
|
ORDER BY t.crt_d;
|
||||||
and if(? , tv.id = ? , 1=1)
|
", [$vid, $vid]);
|
||||||
-- and t.speed > tvd.speed_limit
|
|
||||||
having t.speed >= tvd.speed_limit
|
|
||||||
ORDER BY t.crt_d
|
|
||||||
", [$from_date, $to_date, $vid, $vid]);
|
|
||||||
|
|
||||||
// // RETURN 1 - LIST
|
// // RETURN 1 - LIST
|
||||||
// if($req->type != 'report'){
|
// if($req->type != 'report'){
|
||||||
|
|||||||
Reference in New Issue
Block a user