将 influxdb 数据建模为标签和字段
Modelling influxdb data into tags and fields
TL;DR
如果您想同时执行分组依据和计数(distinct())
,如何将数据建模到字段与标签中
所以目前这是我的 influxdb 数据模型:
api_requests (database)
- requests_stats (measurement)
- api_path (tag)
- app_version (tag)
- host (tag)
- platform (tag)
- account_id (field)
- user_id (field)
- function_name (field)
- network (field)
- network_type (field)
- time_to_execute (field)
所以现在我想找出不同帐户(活跃帐户)的数量。
所以我可以运行下面的查询:
SELECT count(distinct("account_id")) AS "active_accounts"
FROM "api_requests"."autogen"."requests_stats"
这很好用,因为帐户 ID 是一个字段。
现在假设我想对 account_id 执行分组操作,例如查找每个帐户收到的请求数:
SELECT count("function_name") AS "request_count"
FROM "api_requests"."autogen"."requests_stats"
GROUP BY "account_id"
我不能这样做,因为标签上推荐了分组依据。
遇到这样的场景怎么办?
解决方案之一是将值存储在字段和值中,但这将是数据冗余。
另一种最佳方式是让 count(distinct()) 处理标签。这可能吗?这实际上是他们 github 存储库中的功能请求。
或者可以对数据模型做些什么来实现同样的目标?
对 account_id
使用 tag
。而不是计数查询:
SELECT count(distinct("account_id")) AS "active_accounts"
FROM "api_requests"."autogen"."requests_stats"
使用查询,它将精确计算 tag value cardinality:
SHOW TAG VALUES EXACT CARDINALITY WITH KEY = "account_id"
这仅适用于您的用例,因为您不想在非重复计数查询中使用任何额外的(时间、标签)过滤器。
TL;DR
如果您想同时执行分组依据和计数(distinct())
所以目前这是我的 influxdb 数据模型:
api_requests (database)
- requests_stats (measurement)
- api_path (tag)
- app_version (tag)
- host (tag)
- platform (tag)
- account_id (field)
- user_id (field)
- function_name (field)
- network (field)
- network_type (field)
- time_to_execute (field)
所以现在我想找出不同帐户(活跃帐户)的数量。 所以我可以运行下面的查询:
SELECT count(distinct("account_id")) AS "active_accounts"
FROM "api_requests"."autogen"."requests_stats"
这很好用,因为帐户 ID 是一个字段。
现在假设我想对 account_id 执行分组操作,例如查找每个帐户收到的请求数:
SELECT count("function_name") AS "request_count"
FROM "api_requests"."autogen"."requests_stats"
GROUP BY "account_id"
我不能这样做,因为标签上推荐了分组依据。
遇到这样的场景怎么办?
解决方案之一是将值存储在字段和值中,但这将是数据冗余。
另一种最佳方式是让 count(distinct()) 处理标签。这可能吗?这实际上是他们 github 存储库中的功能请求。
或者可以对数据模型做些什么来实现同样的目标?
对 account_id
使用 tag
。而不是计数查询:
SELECT count(distinct("account_id")) AS "active_accounts"
FROM "api_requests"."autogen"."requests_stats"
使用查询,它将精确计算 tag value cardinality:
SHOW TAG VALUES EXACT CARDINALITY WITH KEY = "account_id"
这仅适用于您的用例,因为您不想在非重复计数查询中使用任何额外的(时间、标签)过滤器。