我有一个时间序列数据集进行分析,但困难在于
e.g。
----------------
dt obj comp
----------------
t1, object_a, component_1
t2, object_b, component_1
t3, object_c, component_3
t4, object_a, component_1
t5, object_c, component_2
t6, object_b, component_3
----------------
我想知道每次提交之间的延迟 来自每个对象的每个组件。 即 obj_a.comp_1.delay1 = obj_a.compoent_1.t4 - obj_a.component_1.t1
等
我试图加入
select timediff( t1.dt, t.dt ) as delay
from table as t
join table as t1
on t1.dt = (
select min(t2.dt)
from table as t2
where t2.obj = t.obj
and t2.comp = t.comp
and t2.dt > t.dt
)
但这是永远的,所以我在想是否有任何方法可以对结果进行排序并使用变量进行行间计算,它会更快。
但它在每次使用子查询和视图时失败,其中排序总是在计算后完成。从我在order by
上看到的内容来看,这似乎是不可能的。那么我有什么选择才能以相当快的速度实现这一目标呢?
答案 0 :(得分:0)
我们会坚持你的结构。您错过了join
条件:
select timediff( t1.dt, t.dt ) as delay
from table t join
table t1
on t1.obj = t.obj and t1.comp = t.comp and
t1.dt = (select min(t2.dt)
from table t2
where t2.obj = t.obj and
t2.comp = t.comp and
t2.dt > t.dt
);
然后,使用此结构,您需要table(obj, comp, dt)
上的索引。
现在,这与您的结构有关。一个更简单的方法就是使用子查询:
select timediff((select min(t2.dt)
from table t2
where t2.obj = t.obj and
t2.comp = t.comp and
t2.dt > t.dt
),
t.dt
) as delay
from table t;
您需要相同的索引。
如果您需要匹配行中的其他列(join
除外),则需要dt
方法。