# Counting predicate cardinality should be fast

**URL:** <https://discuss.dgraph.io/t/counting-predicate-cardinality-should-be-fast/8580>\
**Category:** Dgraph\
**Tags:** dgraph, status:more-info-nee, area:querylang, area:performance\
**Created:** [July 3, 2020, 8:52am UTC](https://discuss.dgraph.io/t/counting-predicate-cardinality-should-be-fast/8580 "2020-07-03T08:52:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [July 3, 2020, 8:52am UTC](https://discuss.dgraph.io/t/counting-predicate-cardinality-should-be-fast/8580/1 "2020-07-03T08:52:55Z")

</div>

**Moved from GitHub [dgraph/5813](https://github.com/dgraph-io/dgraph/issues/5813)**

_Posted by_ [EnricoMi](https://github.com/EnricoMi):

## Experience Report

### What you wanted to do

I want to know how many uids have a specific predicate.

### What you actually did

The query

```auto
{
  result (func: has(pred)) {
    count(uid)
  }
}

```

gives me the number of uids that have predicate `pred`.

### Why that wasn’t great, with examples

This query is very slow for large predicates. There must be some index or cardinality information for a predicate available to answer this query in constant time. The “Schema” tab in Ratel provides for predicates the “Samples & Statistics” tab, which seems to fire the same query, which takes for ever. Improving this query would also improve UX for Ratel:

 ![grafik](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/d/dca2c3e63933ed9aec59cb3c5c908c17d00dc39b.png)

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [July 9, 2020, 4:37pm UTC](https://discuss.dgraph.io/t/counting-predicate-cardinality-should-be-fast/8580/2 "2020-07-09T16:37:02Z")

</div>

[danielmai](https://github.com/danielmai) _commented_ :

There’s an existing feature request in #3054 to add quick approximate counts. Would this work for you?

---

<div class="post-metadata">

**Author:** ![EnricoMi](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@EnricoMi](https://discuss.dgraph.io/u/EnricoMi)\
**Post date:** [July 28, 2020, 12:33pm UTC](https://discuss.dgraph.io/t/counting-predicate-cardinality-should-be-fast/8580/3 "2020-07-28T12:33:57Z")

</div>

Approximation as in [Implement approximate counts using HyperLogLog++ algorithm](http://discuss.hypermode.com/t/implement-approximate-counts-using-hyperloglog-algorithm/8763) would work well for me. I would need this not for a single but multiple predicates though:

```
  pred1 as var(func: has(<dgraph.graphql.schema>))
  pred2 as var(func: has(<dgraph.graphql.xid>))
  pred3 as var(func: has(<dgraph.type>))

  result (func: uid(pred1,pred2,pred3)) {
    count(uid)
  }

```

Would that work as well?
