Hello @NickN,
Are you using 1.x or 2.0?
First I’d like to recommend against making a tag with unbounded values a tag, that should be a field instead. Things sensor ID, customer ID, etc are typically made fields to reduce cardinality and avoid runaway cardinality.
If you’re using 2.0, I recommend using the operation monitoring template to track your cardinality.
Hello @NickN,
SensorID can be a tag if it’s scoped. However, if you’re planning to grow your sensors indefinitely, then making sensorID a tag might result in runaway series cardinality.
No problem, that’s what we’re here for. Does that help?
What is the best query to use to determine the number of distinct tags in a database?
Is it possible to query the number of tags that have reported data within a given time range?
Is it possible to create a chart in Grafana that shows how many distinct tags are reporting data over time?
To show all the existing values for a tag you can use: SHOW TAG VALUES WITH KEY = "__YourTagName__"
To have a count of the values you can use SHOW TAG VALUES CARDINALITY WITH KEY = "tag" (estimated, exact is also available, see docs)
The result returned sometimes is weird and cannot be visualized, so try some combinations and see if it’s actually usable (in the influx-cli the result will be readable so you can check it there for reference)
The other 2 points are a bit tricky as they require subqueries
to SELECT points, at least a field must be in the select list. Therefore even if we don’t need any field, at least one must be returned.
if the field has no value no point will be returned, therefore choose a field that is always valued, or just use SELECT *
COUNT DISTINCT cannot be used directly on tags, therefore a subquery is needed.
This sample allows you to have the “tag” count over time
SELECT COUNT(DISTINCT "tag") as "TagCount"
FROM (
SELECT "time","tag","ValueA"
FROM "logins.count"
WHERE $timeFilter
)
GROUP BY time($__interval)
In order to have the total count for a defined time range as a KPI or similar, the query stays the same, ideally, you should remove the GROUP BY… but that can’t be done as that’s required by the COUNT function.
therefore I suggest you put a really big time interval, like “1d” maybe in order to group all the points in the same “range bucket”.
Note that KPIs (“Stats” chart) will force you to also select another aggregation function (Display → Calculation) on top of the returned result like Last, Max, etc.
which is not a problem as long as you just have a one-point result, but might be not accurate if you have more points so pay attention