# Indexes

> agdb example: creating indexes and looking up elements by an indexed key

## What it shows

The example creates indexes on the `username` and `token` keys, inserts 100 users as typed `User` structs linked to a `users` root and then finds one user by looking up its `username` in the index instead of searching the graph. The result is converted straight back into a `User`.

## Run it

```bash
cargo run -p agdb --example indexes
```

## Creating indexes

An index is created for a key. It can be created before or after the data is inserted - an index created later indexes all existing values of that key as well:

```rs
// Create two indexes, one for username and one for token. They can just as well
// be created later and will index all existing data as well.
db.exec_mut(QueryBuilder::insert().index("username").query())?;
db.exec_mut(QueryBuilder::insert().index("token").query())?;
```

## Looking up by index

The index search finds the element with the given value directly. This is useful when the data is too uniform to be split up on the graph - for example users looked up by their username or logged-in users looked up by a token:

```rs
let user: User = db
    .exec(
        QueryBuilder::select()
            .values(User::db_keys())
            .search()
            .index("username")
            .value("user50")
            .query(),
    )?
    .try_into()?;

println!("{user:?}");
```

## Learn more

- [Insert index](/docs/references/queries#insert-index) and [remove index](/docs/references/queries#remove-index) in the queries reference.
- [Search](/docs/references/queries#search) - index search versus graph search.
- [Efficient agdb](/docs/references/efficient-agdb) - when to model data on the graph and when to use an index.

<style>

html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}

</style>

---

- [View source](https://github.com/agnesoft/agdb/blob/main/agdb/examples/indexes.rs)
