fix slow query

This commit is contained in:
msquare 2016-12-27 16:02:13 +01:00
parent 9975e42d3c
commit b05b235cf8
2 changed files with 13 additions and 16 deletions

View File

@ -23,3 +23,4 @@ INSERT INTO `Privileges` (`id`, `name`, `desc`) VALUES (NULL, 'shiftentry_edit_a
-- DB Performance -- DB Performance
ALTER TABLE `Shifts` ADD INDEX(`start`); ALTER TABLE `Shifts` ADD INDEX(`start`);
ALTER TABLE `NeededAngelTypes` ADD INDEX(`count`);

View File

@ -11,7 +11,15 @@ function Shifts_by_room($room) {
} }
function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter, $user) { function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter, $user) {
$SQL = "SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` as `room_name`, nat2.`special_needs` > 0 AS 'has_special_needs' $SQL="SELECT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` as `room_name`
FROM `Shifts`
JOIN `Room` USING (`RID`)
JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ")
AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime() . "
ORDER BY `Shifts`.`start`";
/**
$SQL = "SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` as `room_name`
FROM `Shifts` FROM `Shifts`
INNER JOIN `Room` USING (`RID`) INNER JOIN `Room` USING (`RID`)
INNER JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) INNER JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
@ -42,30 +50,18 @@ function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter, $user) {
if ($shiftsFilter->getFilled()[0] == ShiftsFilter::FILLED_FREE) { if ($shiftsFilter->getFilled()[0] == ShiftsFilter::FILLED_FREE) {
$SQL .= " $SQL .= "
AND ( AND (
nat.`count` > entries.`count` OR entries.`count` IS NULL nat.`count` > entries.`count` OR entries.`count` IS NULL
OR EXISTS (
SELECT `SID`
FROM `ShiftEntry`
WHERE `UID` = '" . sql_escape($user['UID']) . "'
AND `ShiftEntry`.`SID` = `Shifts`.`SID`
)
)"; )";
} elseif ($_SESSION['user_shifts']['filled'][0] == ShiftsFilter::FILLED_FILLED) { } elseif ($_SESSION['user_shifts']['filled'][0] == ShiftsFilter::FILLED_FILLED) {
$SQL .= " $SQL .= "
AND ( AND (
nat.`count` <= entries.`count` nat.`count` <= entries.`count`
OR EXISTS (
SELECT `SID`
FROM `ShiftEntry`
WHERE `UID` = '" . sql_escape($user['UID']) . "'
AND `ShiftEntry`.`SID` = `Shifts`.`SID`
)
)"; )";
} }
} }
$SQL .= " $SQL .= "
ORDER BY `start`"; ORDER BY `start`";
*/
$result = sql_select($SQL); $result = sql_select($SQL);
if ($result === false) { if ($result === false) {
engelsystem_error("Unable to load shifts by filter."); engelsystem_error("Unable to load shifts by filter.");