我正在使用MySQL为我在HPC群集上运行的大量仿真保留数据。每个模拟在表格中都有自己的条目,并且还有第二个表格用于保存模拟时间步骤结果数据。时间步骤结果数据表非常大(数十到数亿行)。这些表是这样的:如何在mysql中获取相应的最大和最小ID?
表:模拟
id descriptor notes
1 SIM1 notes here...
2 SIM2 SIM2 Notes...
... ... ...
8643 SIM8643 SIM8643 Notes...
表:simulations_ts
id simulation_id step data_value
1 1 1 0.05
2 1 2 0.051
... ... ... ...
1983 1 1983 0.253
1984 2 1 0.043
... ... ... ...
59345435 8643 2832 0.067
我想高效能如下表返回:
simulation_id first_ts_id last_ts_id num_steps
1 1 1983 1983
2 1984 2938434 2052
... ... ... ...
8643 12835283 59345435 2832
我知道我可以像执行查询:
SELECT
simulation_id
MIN(step) AS first_step,
MAX(step) AS last_step,
COUNT(id) AS num_steps
FROM
simulations_ts
GROUP BY
simulation_id
ORDER BY
simulation_id ASC
而且有办法做的子查询拉相应的ID一项合计,但我发现没有例子拉相应的ID两种骨料功能。这是否有可能以一种有效的方式在单个查询中完成,还是我更好地逐步完成并分别进行最小查找和最大查找?
您能否介绍一下更多关于'拉对应两个聚合函数的id的例子? – sarwar026
@ sarwar026:请参阅上面的“我希望能够有效地返回下表”。我期待拉对应于最大simulation_ts.step的simulation_ts.id和最小simulation_ts.step的分组simulation_ts.simulation_id(我希望澄清了一点) – MarkD
不查询做到这一点?它不够有效吗?你有什么指标在桌子上? – Barmar