CC-5932: Update is_scheduled query is too slow

This commit is contained in:
drigato 2014-10-28 10:25:40 -04:00
parent d4e8eed51d
commit eae1b4e17c

View file

@ -1368,27 +1368,44 @@ SQL;
return $updateIsScheduled; return $updateIsScheduled;
} }
/**
*
* Updates the is_scheduled flag to false for tracks that are no longer
* scheduled in the future. We do this by checking the difference between
* all files scheduled in the future and all files with is_scheduled = true.
* The difference of the two result sets is what we need to update.
*/
public static function updatePastFilesIsScheduled() public static function updatePastFilesIsScheduled()
{ {
/* Set the is_scheduled flag to false where it was true in the $futureScheduledFilesSelectCriteria = new Criteria();
* past, and where tracks are not scheduled in the future and do $futureScheduledFilesSelectCriteria->addSelectColumn(CcSchedulePeer::FILE_ID);
* not belong to a show that has not ended yet. We need to check $futureScheduledFilesSelectCriteria->setDistinct();
* for show end times in case a track is overbooked, which would $futureScheduledFilesSelectCriteria->add(CcSchedulePeer::ENDS, gmdate("Y-m-d H:i:s"), Criteria::GREATER_THAN);
* indicate it is still scheduled in the future $stmt = CcSchedulePeer::doSelectStmt($futureScheduledFilesSelectCriteria);
*/ $filesScheduledInFuture = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
$sql = <<<SQL $count = 0;
UPDATE cc_files SET is_scheduled = false foreach ($filesScheduledInFuture as $f) {
WHERE is_scheduled = true $count += 1;
AND id NOT IN ( }
SELECT s.file_id FROM cc_schedule AS s
LEFT JOIN cc_show_instances AS i $filesCurrentlySetWithIsScheduledSelectCriteria = new Criteria();
ON s.instance_id = i.id $filesCurrentlySetWithIsScheduledSelectCriteria->addSelectColumn(CcFilesPeer::ID);
WHERE s.ends > now() at time zone 'UTC' $filesCurrentlySetWithIsScheduledSelectCriteria->add(CcFilesPeer::IS_SCHEDULED, true);
AND i.ends > now() at time zone 'UTC' $stmt = CcFilesPeer::doSelectStmt($filesCurrentlySetWithIsScheduledSelectCriteria);
) $filesCurrentlySetWithIsScheduled = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
SQL; $count = 0;
Application_Common_Database::prepareAndExecute($sql, array(), foreach ($filesCurrentlySetWithIsScheduled as $t) {
Application_Common_Database::EXECUTE); $count += 1;
}
$diff = array_diff($filesCurrentlySetWithIsScheduled, $filesScheduledInFuture);
$con = Propel::getConnection(CcFilesPeer::DATABASE_NAME);
$selectCriteria = new Criteria();
$selectCriteria->add(CcFilesPeer::ID, $diff, Criteria::IN);
$updateCriteria = new Criteria();
$updateCriteria->add(CcFilesPeer::IS_SCHEDULED, false);
BasePeer::doUpdate($selectCriteria, $updateCriteria, $con);
} }
} }