我有一个时间序列数据集需要分析,但困难在于
- 数据集非常大。
- 时序事件是由具有隶属关系的不同对象提交的。
- 在mysql中,没有像索引列这样的id来加入
例如
----------------
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
----------------
我想知道每次提交之间的延迟 来自每个对象的每个组件。 IE。 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
)
但这需要很长时间,所以我在想如果有什么方法可以对结果进行排序并使用变量进行行间计算,它会快得多。
但它在子查询和 View 方面都失败了,其中排序总是在计算后完成。从我在 order by
上读到的内容来看,这似乎是不可能的。那么我必须采取哪些选择才能相当快地实现这一目标?
最佳答案
我们将坚持您的结构。您缺少加入
条件:
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;
您会想要相同的索引。
如果您需要匹配行中除 dt
之外的其他列,则需要 join
方法。
关于mysql - 在行间计算之前对记录进行排序的选项是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39428942/