# When is the @count index actually needed in Dgraph?

**URL:** <https://discuss.dgraph.io/t/when-is-the-count-index-actually-needed-in-dgraph/19889>\
**Category:** Dgraph\
**Tags:** kind:question\
**Created:** [May 9, 2025, 9:50am UTC](https://discuss.dgraph.io/t/when-is-the-count-index-actually-needed-in-dgraph/19889 "2025-05-09T09:50:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gooohgb](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/gooohgb/32/11771_2.png) [@gooohgb](https://discuss.dgraph.io/u/gooohgb)\
**Post date:** [May 9, 2025, 9:50am UTC](https://discuss.dgraph.io/t/when-is-the-count-index-actually-needed-in-dgraph/19889/1 "2025-05-09T09:50:18Z")

</div>

I’m trying to better understand the use case for the `@count` index in Dgraph.

From what I gather, it’s only necessary when we need to **filter or sort nodes based on the number of values they have for a specific predicate** — for example, how many `friends` a node has. In that case, we’d write something like:

```auto
{
  q(func: has(friends)) @filter(ge(count(friends), 3)) {
    uid
  }
}

```

But in a query like this:

```auto
{
  var(func: type(someType)) @filter(has(sth)) {
    someuid as uid  
  }

  count_num(func: uid(someuid)) @filter(type(someType)) {
    count_num: count(uid)
  }
}

```

We’re simply counting the number of nodes matching a condition, not counting predicate values. So I assume the `@count` index is **not required** here.

Is this understanding correct?

Thanks!

---

<div class="post-metadata">

**Author:** ![matthewmcneely](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/matthewmcneely/32/4774_2.png) [@matthewmcneely](https://discuss.dgraph.io/u/matthewmcneely)\
**Post date:** [May 9, 2025, 4:21pm UTC](https://discuss.dgraph.io/t/when-is-the-count-index-actually-needed-in-dgraph/19889/2 "2025-05-09T16:21:47Z")

</div>

That’s correct. Note that the @count directive would also be needed for “root functions” that use `count`, e.g.:

```auto
		user(func:eq(count(friend),4)) {
			name
		}

```

In your second example, the set of UIDs is already collected in the search so no @count predicate is required.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2b38fdece2abe5814f2e51bee69d1e67fc304b0a.png) [@system](https://discuss.dgraph.io/u/system)\
**Post date:** [May 12, 2025, 6:43am UTC](https://discuss.dgraph.io/t/when-is-the-count-index-actually-needed-in-dgraph/19889/3 "2025-05-12T06:43:29Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
