在 sqlite 数据库中进行除法计算的最快方法是什么

问题描述 投票:0回答:0

我有一个这样的sqlite数据库表

我需要为每个 TimeIndex 计算 ObjectIndex == 1 的值,除以 ObjectIndex == 2 的值,例如对于 TimeIndex == 1, 100 / 5 = 20

我的代码使用 Ruby Sequel,这是我目前所拥有的:

array_1 = db[:my_table].where(ObjectIndex: 1).order(:TimeIndex).select_map(:Value)
array_2 = db[:my_table].where(ObjectIndex: 2).order(:TimeIndex).select_map(:Value)
division = array_1.zip(array_2).map { |x, y| y == 0 ? 0 : (x / y).round(2)}

...然后在我的其他地方的 ruby 代码中使用除法

目前处理一个 ObjectIndex 大约需要 10 秒(如果我先打印出 array_1.first,则需要 10 秒)。 表中大约有 5813k 行,每个 ObjectIndex 大约有 9000 行。这个速度是正常的还是有办法减少处理时间?

ruby performance sqlite sequel
© www.soinside.com 2019 - 2024. All rights reserved.