SELECT行接下来n行满足CONDITIONS

时间:2012-07-11 00:48:01

标签: php mysql architecture

假设我们有这个资源可用性表:

+-----------+-----------------------------------------------------------+
| date      | obvious, the date                                         |
| timeslot  | we have 12 fixed 2-hour timeslots so this will be 1 to 12 |
| r1        | number of resource type 1 available during this timeslot  |
| r2        | same, for resource type 2                                 |
| r3        | same, for resource type 3                                 |
+-----------+-----------------------------------------------------------+

现在,我希望看到我可以使用的所有可用时间段job #43。对于这项工作,我需要2个 r1 单位,一个 r2 单位和三个 r3 单位。假设作业需要一个时间段,我可以使用此查询:

SELECT `date`, `timeslot` FROM `resource_availability` 
WHERE
  `r1` > '1' AND
  `r2` > '0' AND
  `r3` > '2'
ORDER BY 'date`, `timeslot`;

但是,如果我有另一份工作,job #86需要3次完成且无法停止重启,那么是否可以通过查询获得安全的开始时间?

我目前正在检查while循环的连续性,但认为可以让查询执行此操作。

如果可以的话,我想知道哪个更快更有效。对于功效评估,应该注意的是,这个位图是一种位图,它经常被更新 - 即每个作业被调度,资源可用性列都会更新。

此外,很明显,该系统的目的是允许检查 what-ifs 。如果我的方法不是最优的,那么有哪些更好的选择?

如果最后一个问题太多,请忽略它,或在评论中告诉我,我会将其删除。

1 个答案:

答案 0 :(得分:2)

哇...我总结了一个可以让你得到你想要的想法。请原谅我,如果需要一点点理解,但我希望你看到它实际上是一个相当简单的解决方案,适用于中等复杂的问题。

我将构建查询(在PHP中)以具有n个自联接,其中n是作业所需的时隙数。自联接加入 next 连续时隙,并根据所有插槽中可用的资源稀释结果。请注意,您可以将动态创建的WHERE子句移动到JOIN条件中......我拥有可见的MySQL版本,这将提高速度。

php代码:

// $r1, $r3, and $r3 are the required resources for this job.

$join_format = 'JOIN timeslots AS %s ON %date = %s.date AND %s.timeslot+1 = %s.timeslot';
$where_format = '(%s.r1 >= '.$r1.' AND %s.r2 >= '.$r2.' AND %s.r3 >= '.$r3.')';

$joins = array();
$wheres = array("block1.date > CURDATE()",
                sprintf($where_format, "block1", "block1", "block1")
                );
$select_list = 'block1.date, block1.timeslot as starting_time, block' . $slots_needed . '.timeslot as ending_time';

for($block = 2; $block <= $slots_needed; $block++) {
  $join_alias = "block" . $block;
  $previous_alias = "block" . ($block-1);
  $joins[] = sprintf($join_format, $join_alias, $previous_alias,$join_alias, $previous_alias, $join_alias);
  $wheres[] = sprintf($where_format, $join_alias, $join_alias, $join_alias);
}

$query_format = 'SELECT %s FROM timeslots as block1 %s WHERE %s GROUP BY block1.date, block1.timeslot ORDER BY block1.date ASC, block1.timeslot ASC';
$joins_string = implode(' ', $joins);
$wheres_string = implode(' AND ', $wheres);
$query = sprintf($query_format, $select_list, $joins_string, $wheres_string);

在我的意图中,这应该产生这样的查询(对于2个需要的块,每个需要1个资源:

结果SQL:

SELECT 
  block1.date,
  block1.timeslot as starting_time, 
  block2.timeslot as ending_time
FROM 
  timeslots AS block1
  JOIN timeslots AS block2
    ON block1.date = block2.date AND block1.timeslot+1 = block2.timeslot
WHERE
  block1.date > CURDATE()
  AND (block1.r1 >= 1 AND block1.r2 >= 1 AND block1.r3 >= 1)
  AND (block2.r1 >= 1 AND block2.r2 >= 1 AND block2.r3 >= 1)
GROUP BY
  block1.date, block1.timeslot
ORDER BY
  block1.date ASC, block1.timeslot ASC

它应该产生如下结果:

预期结果集:

+------------+---------------+-------------+
|    date    | starting_time | ending_time |
+------------+---------------+-------------+
| 2001-01-01 |       1       |      2      |
+------------+---------------+-------------+
| 2001-01-01 |       2       |      3      |
+------------+---------------+-------------+
| 2001-01-01 |       7       |      8      |
+------------+---------------+-------------+
| 2001-01-01 |       8       |      9      |
+------------+---------------+-------------+
| 2001-01-02 |       4       |      5      |
+------------+---------------+-------------+

请注意,如果需要2个块,但有3个可用(连续),则查询返回两个选项(第一个和第二个OR,第二个和第三个可用时间)。