繁体   English   中英

Grafana:SQL 查询:如何仅在选择过滤器时应用过滤器

[英]Grafana: SQL Query: How to apply a filter only if it is selected

我正在使用 PostgreSQL 与 Grafana 的连接。

我有以下 SQL 查询:

select video_type as video, 
  sum(cv.duration)/3600 as "Duration (Hours)"
  from fact.content_view cv 
where  $__timeFilter(cv.datetime at time zone 'UTC+5')
  and cv.channel_id in ($channel)
group by 1
order by 2 desc

当 Grafana 执行此查询时,它会转换为:

select video_type as video, 
  sum(cv.duration)/3600 as "Duration (Hours)"
  from fact.content_view cv 
where  cv.datetime at time zone 'UTC+5' BETWEEN '2021-12-30T17:14:09.53Z' AND '2022-01-06T17:14:09.53Z'
  and cv.channel_id in ('48','13002','1071','1076','310','309','19','13004','1073','815','806','824','823','624','52','670','186','827','14','191','687','13001','58','54','63','305','306','2','6','13005','896','10016','37','304','103','105','104','148','149','946','808','11011','31','192','268','224','269','283','294','183','11001','218','5','188','189','767','759','300','11020','11021','116','814','86','96','99','1049','690','231','204','213','13003','292','1051','46','156','1048','297','164','33','820','32','165','166','206','55','56','119','917','271','100','232','285','220','247','248','61','110','228','187','36','111','193','34','251','11027','244','246','245','117','1087','151','242','11037','163','197','1031','829','828','39','258','178','147','252','11028','10009','756','256','11024','9120','270','273','121','291','1050','290','948','198','721','159','185','51','24','10010','68','1074','124','11022','862','296','219','10011','870','16','114','298','1026','308','90','93','95','853','97','833','142','98','138','137','140','861','272','118','699','113','145','10012','696','10015','771','210','287','106','249','4','62','109','10005','154','11025','102','243','240','229','211','128','254','1033','157','10013','10003','1058','1078','1080','1077','1081','1079','1082','9147','1015','49','42','59','10002','259','683','208','295','71','859','11036','205','10018','265','241','335','908','10014','864','1072','1000','28','107','30','226','621','27','301','233','127','6666','-1','112','29','311','320','312','313','314','315','316','317','318','319','239','126','200','10008','238','184','253','108','160','282','11035','122','10006','278','279','280','281','20','209','65','1070','66','321','53','236','195','222','115','207','199','293','214','162','10007','123','11032','230','11030','181','10001','731','1034','286','216','302','793','212','217','38','132','158','790','791','792','263','950','284','826','227','1057','203','1092','1124','1131','7','1141','1157','1142','1143','1144','1145','1146','1147','1155','1156','1111','1132','1133','1134','1135','1136','1137','1138','1139','1140','1093','1110','1129','1112','1113','1114','1098','1099','1126','1101','1102','1088','1090','1117','1118','1130','1158','1105','1120','1121','1122','307','44','674','134','261','73','88','1107','1108','255','1020','1038','129','12','825','267','8','266','13','143','146','150','18','1028','72','75','69','171','84','25','170','169','43','35','40','21','94','11','10','74','80','67','257','41','76','173','85','87','262','182','179','174','177','152','77','153','172','260','168','180','176','83','47','45','89','26','81','9','70','91','78','92','175','82','23','10017','288','925','196','734','120','1022','201','235','1','131','11023','17','237','11031','130','1075','3','11033','1053','144','655','264','907','22','139','817','935','194','11002','722','215','12001','125','764','816','57','812','813','101','64','161','819','190','1027','11029','274','275','276','277','167','133','155','299','860','223','136','663','60','289','640','50','847','830','839','225','11034','768','625','202','1039','742','221','15','1055','303','79','818','1056','10004')
group by 1
order by 2 desc

通道变量定义如下:

select  channel_id __value, name __text  from dimension.channel where name not like '\_%' order by 2

在此处输入图像描述

我的问题是,当在“频道”过滤器中选择“全部”时,Grafana 会将所有“频道 ID”传递给 SQL 查询。 如果我有超过 5000-6000 个 id,那些会被 Grafana 剪掉。 因此我得到了不正确的结果。

所以我正在尝试以某种方式,如果没有从过滤器中选择任何值,那么整个过滤器将被跳过。 只有在选择某些值的情况下,才会应用此过滤。 理想情况下,我应该能够通过过滤器中的 10-20 个值来限制选择。 不幸的是,我无法完成这项工作。 也欢迎其他解决问题的建议。

谢谢

How to apply a filter only if it is selected的想法是错误的。 您需要将 t 更改为:如何编写查询条件,它将 select all 记录与Custom all value

一种可能的解决方案是将channel变量的Custom all value设置配置为%

然后查询条件将是( ~是区分大小写的匹配):

AND cv.channel_id ~ '${channel:pipe}'

When All channel variable is selected then this SQL will be generated/executed:

AND cv.channel_id ~ '%'

应该匹配所有记录(未经测试,因此最终条件可能仍需要一些调整)。

我找到了以下问题的解决方案:

  1. 在过滤器中,对于“全部”值,我配置了“-99”
  2. 我通过以下方式更改了“where”子句:
      and (-99 in ($channel) OR cv.channel_id in ($channel))
  1. “全部”被选中,它被评估为:

      and (-99 in (-99) OR va.channel_id in (-99))

这里'OR'条件的第一部分是'true'

  1. When some something else is selected it it evaluated to eg
     and (-99 in ('48','13002') OR va.channel_id in ('48','13002'))

这里“或”条件的第一部分为假,因此计算第二部分。

这种方法的缺点是,如果您想要 select 多个值,则随着值加倍,查询会变得更大。 但是,如果选择“全部”,则从性能角度来看,它的效果也很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM