# How to efficiently query data from FDB?

**URL:** <https://forums.foundationdb.org/t/how-to-efficiently-query-data-from-fdb/3895>\
**Category:** Using FoundationDB\
**Created:** [April 10, 2023, 11:25am UTC](https://forums.foundationdb.org/t/how-to-efficiently-query-data-from-fdb/3895 "2023-04-10T11:25:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Apache](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@Apache](https://forums.foundationdb.org/u/Apache)\
**Post date:** [April 10, 2023, 11:25am UTC](https://forums.foundationdb.org/t/how-to-efficiently-query-data-from-fdb/3895/1 "2023-04-10T11:25:58Z")

</div>

Few days back I posted on this forum asking about the way to structure data in FDB. I received very patient and informative replies from @markus.pilman

In those replies it was mentioned that a better way to structure data in FDB is to associate the primary key with the complete table data/record as the value, stored in an efficient format such as protobuf. For example, a student table `{uuid, name, address}` could be represented in FDB as `(uuid, {name, address})` where `name` and `address` are stored as protobuf encoded value for the `uuid` key.

So, my question is, how do I efficiently query data that is encoded in the value? Say, I want to find all students whose name is ‘Bandu’; do I need to query the FDB server for each `student` record, decode the protobuf value, and then check for the name? What will be the performance implications of this approach? Am I thinking in the wrong direction again? What is a better way to query data in FDB?

---

<div class="post-metadata">

**Author:** ![osamarin](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/osamarin/32/905_2.png) [@osamarin](https://forums.foundationdb.org/u/osamarin)\
**Post date:** [April 10, 2023, 2:09pm UTC](https://forums.foundationdb.org/t/how-to-efficiently-query-data-from-fdb/3895/2 "2023-04-10T14:09:53Z")

</div>

FDB allows to query by a key or a key range, `uuid` in your case. So if you need to query by a part of value efficiently, you have create secondary key-value pairs: {name+uuid, }. Then you will be able to query by name using the range with the prefix `name` and to get a list of `uuid`s of the students with given name, then, to query their data by uuid.

You can use the `Record Layer` on top of fdb, that does a part of this work automatically. There is a special part [Using Layers](https://forums.foundationdb.org/c/using-layers) in this community forum.

---

<div class="post-metadata">

**Author:** ![Apache](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@Apache](https://forums.foundationdb.org/u/Apache)\
**Post date:** [April 17, 2023, 9:48am UTC](https://forums.foundationdb.org/t/how-to-efficiently-query-data-from-fdb/3895/3 "2023-04-17T09:48:05Z")

</div>

> [@osamarin](#):
>
> you have create secondary key-value pairs: {name+uuid, }. Then you will be able to query by name using the range with the prefix `name` and to get a list of `uuid`s of the students with given name, then, to query their data by uuid.

What are the performance implications of querying the DB server twice?

---

<div class="post-metadata">

**Author:** ![osamarin](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/osamarin/32/905_2.png) [@osamarin](https://forums.foundationdb.org/u/osamarin)\
**Post date:** [April 19, 2023, 2:23pm UTC](https://forums.foundationdb.org/t/how-to-efficiently-query-data-from-fdb/3895/4 "2023-04-19T14:23:07Z")

</div>

Of cause, the performance will be lower.

If it is not acceptable, you may duplicate data and insert two pairs:

- {uuid, {name, address}}
- {name+uuid, address}
