Performance when using window function use influxDB 2.7.6

I am using influxdb 2.7.6, and created a measurement called cgm_glucose_history. I added a tag called device_sn, a field called glucose, and a field called device_time to record the exact time when blood glucose was generated. In this measurement, each device_sn generates a glucose value every minute.

I use the java client to save data

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>List<Point> pointList = new ArrayList<>();
for (CgmGlucose cgmGlucose : list) {
// Time alignment to minutes, 00:00,00:01,00:2,00:3...
long time = cgmGlucose.getDeviceTime() - cgmGlucose.getDeviceTime() % 1000L;
Point point = Point.measurement(MEASUREMENT_CGM_GLUCOSE_HISTORY)
.time(time, WritePrecision.MS)
.addTag("patient_code", cgmGlucose.getPatientCode())
.addTag("device_sn", cgmGlucose.getDeviceSn())
.addField("glucose", cgmGlucose.getGlucose())
.addField("device_time", cgmGlucose.getDeviceTime());
pointList.add(point);
if (pointList.size() >= 1000) {
writeApi.writePoints(InfluxConfig.getBucket(), InfluxConfig.getOrg(), pointList);
pointList.clear();
}
}
if (!pointList.isEmpty()) {
writeApi.writePoints(InfluxConfig.getBucket(), InfluxConfig.getOrg(), pointList);
pointList.clear();
}
writeApi.close();
</code>
<code>List<Point> pointList = new ArrayList<>(); for (CgmGlucose cgmGlucose : list) { // Time alignment to minutes, 00:00,00:01,00:2,00:3... long time = cgmGlucose.getDeviceTime() - cgmGlucose.getDeviceTime() % 1000L; Point point = Point.measurement(MEASUREMENT_CGM_GLUCOSE_HISTORY) .time(time, WritePrecision.MS) .addTag("patient_code", cgmGlucose.getPatientCode()) .addTag("device_sn", cgmGlucose.getDeviceSn()) .addField("glucose", cgmGlucose.getGlucose()) .addField("device_time", cgmGlucose.getDeviceTime()); pointList.add(point); if (pointList.size() >= 1000) { writeApi.writePoints(InfluxConfig.getBucket(), InfluxConfig.getOrg(), pointList); pointList.clear(); } } if (!pointList.isEmpty()) { writeApi.writePoints(InfluxConfig.getBucket(), InfluxConfig.getOrg(), pointList); pointList.clear(); } writeApi.close(); </code>
List<Point> pointList = new ArrayList<>();
        for (CgmGlucose cgmGlucose : list) {
            // Time alignment to minutes, 00:00,00:01,00:2,00:3...
            long time = cgmGlucose.getDeviceTime() - cgmGlucose.getDeviceTime() % 1000L;
            Point point = Point.measurement(MEASUREMENT_CGM_GLUCOSE_HISTORY)
                    .time(time, WritePrecision.MS)
                    .addTag("patient_code", cgmGlucose.getPatientCode())
                    .addTag("device_sn", cgmGlucose.getDeviceSn())
                    .addField("glucose", cgmGlucose.getGlucose())
                    .addField("device_time", cgmGlucose.getDeviceTime());
            pointList.add(point);
            if (pointList.size() >= 1000) {
                writeApi.writePoints(InfluxConfig.getBucket(), InfluxConfig.getOrg(), pointList);
                pointList.clear();
            }
        }
        if (!pointList.isEmpty()) {
            writeApi.writePoints(InfluxConfig.getBucket(), InfluxConfig.getOrg(), pointList);
            pointList.clear();
        }
        writeApi.close();

I now have a requirement to query the time when a specific device_sn first had a sustained hyperglycemia event (if any). Sustained hyperglycemia is defined as a glucose value greater than 13.9 for more than two hours. I used the window and reduce methods, and the code is as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import "interpolate"
from(bucket: "cdm_dm")
|> range(start: 1718679994)
|> filter(fn: (r) => r["_measurement"] == "cgm_glucose_history")
|> filter(fn: (r) => r["device_sn"] == "TT22222AN2")
|> filter(fn: (r) => r["_field"] == "glucose" or r["_field"] == "device_time")
|> map(fn: (r) => ({r with _value: float(v: r._value)}))
|> interpolate.linear(every: 1m)
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> window(every: 1m, period: 122m)
// The core logic is to count the points where the glucose value is greater than 13.9 within two hours.
|> reduce(fn:(r, accumulator) => ({
count: if r.glucose > 13.9 then accumulator.count+1 else 0,
event_start_time:if r.glucose > 13.9 then r.device_time else 0.0,
glucose:if (r.glucose > 13.9 and accumulator.count==121) then r.glucose else if (r.glucose > 13.9 and accumulator.count < 121)
then accumulator.glucose else 0.0
}),identity:{count:0,event_start_time:0.0,glucose:0.0})
|> duplicate(column: "_start", as: "_time")
|> window(every: inf)
|> filter(fn: (r) => r["count"] == 122)
|> limit(n:1)
</code>
<code>import "interpolate" from(bucket: "cdm_dm") |> range(start: 1718679994) |> filter(fn: (r) => r["_measurement"] == "cgm_glucose_history") |> filter(fn: (r) => r["device_sn"] == "TT22222AN2") |> filter(fn: (r) => r["_field"] == "glucose" or r["_field"] == "device_time") |> map(fn: (r) => ({r with _value: float(v: r._value)})) |> interpolate.linear(every: 1m) |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value") |> window(every: 1m, period: 122m) // The core logic is to count the points where the glucose value is greater than 13.9 within two hours. |> reduce(fn:(r, accumulator) => ({ count: if r.glucose > 13.9 then accumulator.count+1 else 0, event_start_time:if r.glucose > 13.9 then r.device_time else 0.0, glucose:if (r.glucose > 13.9 and accumulator.count==121) then r.glucose else if (r.glucose > 13.9 and accumulator.count < 121) then accumulator.glucose else 0.0 }),identity:{count:0,event_start_time:0.0,glucose:0.0}) |> duplicate(column: "_start", as: "_time") |> window(every: inf) |> filter(fn: (r) => r["count"] == 122) |> limit(n:1) </code>
import "interpolate"
from(bucket: "cdm_dm")
  |> range(start: 1718679994)
  |> filter(fn: (r) => r["_measurement"] == "cgm_glucose_history")
  |> filter(fn: (r) => r["device_sn"] == "TT22222AN2")
  |> filter(fn: (r) => r["_field"] == "glucose" or r["_field"] == "device_time")
  |> map(fn: (r) => ({r with _value: float(v: r._value)}))
  |> interpolate.linear(every: 1m)
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> window(every: 1m, period: 122m)
  // The core logic is to count the points where the glucose value is greater than 13.9 within two hours.
  |> reduce(fn:(r, accumulator) => ({
  count: if r.glucose > 13.9 then accumulator.count+1 else 0,
  event_start_time:if r.glucose > 13.9  then r.device_time else 0.0,
  glucose:if (r.glucose > 13.9 and accumulator.count==121) then r.glucose else  if (r.glucose > 13.9 and accumulator.count < 121)
  then accumulator.glucose else 0.0 
  }),identity:{count:0,event_start_time:0.0,glucose:0.0})
  |> duplicate(column: "_start", as: "_time")
  |> window(every: inf)
  |> filter(fn: (r) => r["count"] == 122)
  |> limit(n:1)

However, in the actual process, I found that the execution time is particularly long. With very little test data, it takes more than 30 seconds to execute to get the result. Is there something wrong with my code logic? Is there a better way for influx to achieve this requirement?

I have no further ideas on how to optimize this code or data structure

New contributor

李旭光 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật