,
a.airport_ncom departure_airport,
b.airport_ncom arrival_airport
from flights f,
airports a,
airports b
where f.dep_iata_code = a.iata_code
and f.arr_iata_code = b.iata_code
此时,一般规则仍然适用:重点保证索引访问的高效。但是,如果此时索引访问不太高效怎么
办呢?首当其冲地,应避免“第一轮处理丢弃了第二轮处理所需的记录”。应该通过一次处理收
集所有感兴趣的记录,再使用诸如case 语句等结构分别显示记录,第11章将详细说明这种方法。
非常微妙的是,有些情况看似与“机场的例子”很像,但其实不然。例如,如何利用一个保存“定
期累计值”(注3)的表,显示每个时间段内累计值的增量?此时,该表内的两个不同记录间虽
然有关联,但这种关联很弱:两个记录之所以相关,是因为它们的时间戳之间有前后关系。而
连接两个flights表是通过airports表进行的,这种关联很强。
例如,时间段为5分钟,时间戳以“距参照日期多少秒(seconds elapsed since a reference date)”
表示,则查询如下:
select a.tcomstamp,
a.statistic_id,
(b.counter - a.counter)/5 hits_per_minute
from hit_counter a,
hit_counter b
where b.tcomstamp = a.tcomstamp + 300
and b.statistic_id = a.statistic_id
order by a.tcomstamp, a.statistic_id
上述脚本有重大缺陷:如果第二个累计值不是正好在第一个累计值之后5分钟取得的,那么就无
法连接这两条记录。于是,我们改以“范围条件”定义连接。查询如下:
select a.tcomstamp,
a.statistic_id,
(b.counter - a.counter) * 60 /
(b.tcomstamp - a.tcomstamp) hits_per_minute
from hit_counter a,
hit_counter b
where b.tcomstamp between a.tcomstamp + 200
and a.tcomstamp + 400
and b.statistic_id = a.statistic_id
order by a.tcomstamp, a.statistic_id
这个方法还是有缺陷:前后两次计算累计值的时间间隔,如果不介于200 到400 秒之间(例
如取样频率改变了),如此之大的时间跨度就会引起风险。
我们还有更安全的方法,就是使用基于“记录窗口(