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

优化Azure Resource Graph KQL查询:过滤含指定前缀标签的VM

优化后的Azure KQL查询方案

原查询两次调用bag_keys(tags)会重复解析标签键集合,在虚拟机数量较多时会影响查询性能。可以通过let语句将标签键集合缓存到变量中,只解析一次,再基于该变量完成多前缀的筛选:

resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| where resourceGroup =~ 'test-resource-group'
| let tagKeys = bag_keys(tags);
| where tagKeys hasprefix "bingo-" and tagKeys hasprefix "tringo-"
| project name

优化说明

  • 使用let语句定义tagKeys变量,仅调用一次bag_keys(tags)解析标签键集合,避免重复计算
  • 合并两个where条件为一个,减少查询阶段的过滤次数
  • 逻辑上与原查询完全一致,仅优化了性能,最终只会返回同时包含bingo-和tringo-前缀标签键的虚拟机(如示例中的vm-1)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 19:12:36