You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KQL查询资源占用过高及无法返回30天前数据问题求助

KQL问题解决方案

问题1:查询资源占用过高优化

原查询的核心问题是子查询对全量CommonSecurityLog做无过滤聚合,当监视列表IP数量增加时,全量数据的join操作会耗尽资源。另外原summarize by TimeGenerated逻辑错误,应该按SourceIP分组来获取每个IP的最新日志。

优化后的查询:

// 先提取监视列表中的IP,存为变量
let watchlist_ips = _GetWatchlist('Testing_Watchlist') | project IPAddress;
// 先过滤目标IP,再按IP分组取最新日志,大幅减少处理数据量
CommonSecurityLog
| where SourceIP in (watchlist_ips)
| summarize arg_max(TimeGenerated, *) by SourceIP
// 关联监视列表保留需要的字段
| join kind=inner (watchlist_ips) on $left.SourceIP == $right.IPAddress
| project-reorder TimeGenerated, IPAddress=SourceIP, DeviceAction, LastUpdatedTimeUTC

优化点说明:

  • 先提取监视列表IP,提前过滤CommonSecurityLog,避免全表扫描
  • 按SourceIP分组做arg_max,只保留每个IP的最新日志,减少聚合后的数据量
  • 使用kind=inner join,只保留两边都匹配的记录,进一步精简结果

问题2:TimeGenerated <= 30days无结果排查

KQL中没有30days这种相对时间的写法,正确的相对时间需要用ago()函数。你写的30days会被识别为非法表达式,导致查询返回空结果。

修正写法:

  • 若要查询30天前及更早的日志:
    | where TimeGenerated <= ago(30d)
    
  • 若要查询30天内的日志(你之前能返回结果的逻辑):
    | where TimeGenerated >= ago(30d)
    

注意:ago(30d)表示当前时间往前推30天,是KQL标准的相对时间语法,必须用该函数来指定时间范围。

内容的提问来源于stack exchange,提问作者Angela Garcia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 19:22:14