我在Azure Data Explorer中有一个包含用户名、时间戳和状态的表。计算相同用户名的每个状态的持续时间,并将结果存储在物化视图中。
下面的示例查询显示了预期的结果,但是物化视图不支持数据序列化(例如sort语句)。
在物化视图中是否有一种聪明的方法来解决这个限制?
示例查询:
let data=datatable (Username:string, Timestamp:datetime, Status:int)
[
"Jimmy", datetime(2021-10-01 12:30:00), 1,
"Joan", datetime(2021-10-01 12:34:56), 1,
"Joan", datetime(2021-10-01 13:34:56), 2,
"Jimmy", datetime(2021-10-01 14:34:56), 2,
"Jimmy", datetime(2021-10-01 16:34:56), 1,
"Joan", datetime(2021-10-01 18:34:34), 1,
"Joan", datetime(2021-10-01 18:38:20), 2,
"Joan", datetime(2021-10-01 18:45:16), 1,
];
data
| sort by Username, Timestamp asc
| extend NextUsername = next(Username)
| extend NextTimestamp = iif(Username == NextUsername, next(Timestamp), datetime(null))
| extend Duration = (NextTimestamp - Timestamp)
| project Username, Start = Timestamp, End = NextTimestamp, Status, Duration
结果示例:
Username Start End Status Duration
Joan 2021-10-01 12:34:56.0000000 2021-10-01 13:34:56.0000000 1 01:00:00
Joan 2021-10-01 13:34:56.0000000 2021-10-01 18:34:34.0000000 2 04:59:38
Joan 2021-10-01 18:34:34.0000000 2021-10-01 18:38:20.0000000 1 00:03:46
Joan 2021-10-01 18:38:20.0000000 2021-10-01 18:45:16.0000000 2 00:06:56
Joan 2021-10-01 18:45:16.0000000 1
Jimmy 2021-10-01 12:30:00.0000000 2021-10-01 14:34:56.0000000 1 02:04:56
Jimmy 2021-10-01 14:34:56.0000000 2021-10-01 16:34:56.0000000 2 02:00:00
Jimmy 2021-10-01 16:34:56.0000000 1
不,物化视图只支持为您提供dedup、最新记录或定时分组聚合的汇总。对于这个计算,如果您想提高性能,您应该创建自己的调度进程来执行这个逻辑,并将结果存储在一个表中。