简体   繁体   中英

Can this query be optimized somehow?

Is this is a good query in performance or I can somehow optimize this query? I need to select date column from rental_schedules table and count how many available same dates are available (rental_schedule_status = 1):

$reservationTimes = RentalSchedule::distinct()
        ->select('date',
        DB::raw('(SELECT count(date) FROM rental_schedules AS r2 
        WHERE r2.date = rental_schedules.date and rental_schedule_status = 1) as free_time'))
        ->where('premises_id', $id)->pluck('free_time', 'date');

Thanks in advance!

You could try to phrase your query using left join to a subquery, eg

SELECT
    rs1.date,
    COALESCE(rs2.cnt, 0) AS free_time
FROM rental_schedules rs1
LEFT JOIN
(
    SELECT date, COUNT(*) AS cnt
    FROM rental_schedules
    WHERE rental_schedule_status = 1
    GROUP BY date
) rs2
    ON rs1.date = rs2.date
WHERE
    rs1.premises_id = ?;

Your updated Laravel code:

$reservationTimes = RentalSchedule:from("RentalSchedule AS rs1")
    ->select(DB::raw('rs1.date, COALESCE(rs2.cnt, 0) AS free_time'))
    ->join(DB::raw('(SELECT date, COUNT(*) AS cnt
                     FROM rental_schedules
                     WHERE rental_schedules_status = 1
                     GROUP BY date) rs2'),
    function($join) {
        $join->on('rs1.date', '=', 'rs2.date');
     })
->where('premises_id', $id)
->pluck('free_time', 'date');

You may also try adding an index on (rental_schedules_status, date) , to speed up the subquery:

CREATE INDEX idx ON rental_schedules (rental_schedules_status, date)

You can get result without join or subquery:

Try this:

$reservationTimes = RentalSchedule::select('date',\DB::Raw("sum(case when rental_schedule_status = 1 then 1 else 0 end) as free_time"))
                ->where('premises_id', $id)
                ->groupBy('date')
                ->get()
                ->pluck('free_time', 'date');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM