Select a documentation version: <button class="button button-with-icon cloud-select-dropdown__button button-primary" aria-haspopup="true">16.9.0</button>

- [Current (18.4.0)](https://docs.cloud.google.com/alloydb/omni/containers/current/docs/ai/tune-indexes.md.txt)
- [18.4.0](https://docs.cloud.google.com/alloydb/omni/containers/18.4.0/docs/ai/tune-indexes.md.txt)
- [18.3.0](https://docs.cloud.google.com/alloydb/omni/containers/18.3.0/docs/ai/tune-indexes.md.txt)
- [18.1.0](https://docs.cloud.google.com/alloydb/omni/containers/18.1.0/docs/ai/tune-indexes.md.txt)
- [17.10.0](https://docs.cloud.google.com/alloydb/omni/containers/17.10.0/docs/ai/tune-indexes.md.txt)
- [17.9.0](https://docs.cloud.google.com/alloydb/omni/containers/17.9.0/docs/ai/tune-indexes.md.txt)
- [17.7.0](https://docs.cloud.google.com/alloydb/omni/containers/17.7.0/docs/ai/tune-indexes.md.txt)
- [17.5.0](https://docs.cloud.google.com/alloydb/omni/containers/17.5.0/docs/ai/tune-indexes.md.txt)
- [16.14.0](https://docs.cloud.google.com/alloydb/omni/containers/16.14.0/docs/ai/tune-indexes.md.txt)
- [16.13.0](https://docs.cloud.google.com/alloydb/omni/containers/16.13.0/docs/ai/tune-indexes.md.txt)
- [16.11.0](https://docs.cloud.google.com/alloydb/omni/containers/16.11.0/docs/ai/tune-indexes.md.txt)
- [16.9.0](https://docs.cloud.google.com/alloydb/omni/containers/16.9.0/docs/ai/tune-indexes.md.txt)
- [16.8.0](https://docs.cloud.google.com/alloydb/omni/containers/16.8.0/docs/ai/tune-indexes.md.txt)
- [16.3.0](https://docs.cloud.google.com/alloydb/omni/containers/16.3.0/docs/ai/tune-indexes.md.txt)
- [15.18.0](https://docs.cloud.google.com/alloydb/omni/containers/15.18.0/docs/ai/tune-indexes.md.txt)
- [15.17.0](https://docs.cloud.google.com/alloydb/omni/containers/15.17.0/docs/ai/tune-indexes.md.txt)
- [15.15.0](https://docs.cloud.google.com/alloydb/omni/containers/15.15.0/docs/ai/tune-indexes.md.txt)
- [15.13.0](https://docs.cloud.google.com/alloydb/omni/containers/15.13.0/docs/ai/tune-indexes.md.txt)
- [15.12.0](https://docs.cloud.google.com/alloydb/omni/containers/15.12.0/docs/ai/tune-indexes.md.txt)
- [15.7.1](https://docs.cloud.google.com/alloydb/omni/containers/15.7.1/docs/ai/tune-indexes.md.txt)
- [15.7.0](https://docs.cloud.google.com/alloydb/omni/containers/15.7.0/docs/ai/tune-indexes.md.txt)

<br />

This document describes how to tune your indexes to achieve faster query
performance and better recall in AlloyDB Omni.
<button value="scann" default="">ScaNN</button> <button value="ivf">IVF</button> <button value="ivfflat">IVFFlat</button> <button value="hnsw">HNSW</button>

> [!CAUTION]
> **Caution:** The `postgres_ann` extension is renamed to `alloydb_scann`. Before you upgrade to AlloyDB Omni, you must drop any indexes created using the older `postgres_ann` version. Next, upgrade AlloyDB Omni, and then create the indexes again using the `alloydb_scann` extension. This applies only to AlloyDB Omni.

## Before you begin

Before you build a `ScaNN` index, complete the following:

- Make sure that a table with your data is already created.
- To avoid issues while generating the index, make sure that the value you set for the `maintenance_work_mem` and the `shared_buffers` flag is less than total machine memory.

## Tune a `ScaNN` index

Use the following guidance to choose between a two-level and three-level ScaNN index:

- Choose a two-level index if the number of vector rows is less than 10 million rows.
- Choose a three-level index if the number of vector rows exceeds 100 million rows.
- Choose a three-level index to optimize for index build time or choose a two-level index to optimize for search recall if the number of vector rows lies between 10 million and 100 million rows.

Consider the following examples for two-level and three-level `ScaNN` indexes that show how tuning parameters are set for a table with 1000000 rows:

### Two-level index

```sql
-- Setting num_leaves to 1000 as power(1000000, 1/2) for two-level index
BEGIN;

SET LOCAL maintenance_work_mem = '300MB';

CREATE INDEX my_scann_index ON my_table
  USING scann (vector_column cosine)
  WITH (num_leaves = 1000);
END;
```

### Three-level index

```sql
-- Setting num_leaves to 10000 as power(1000000, 2/3) for three-level index
BEGIN;

SET LOCAL maintenance_work_mem = '500MB';

CREATE INDEX my_scann_index ON my_table
  USING scann (vector_column cosine)
  WITH (num_leaves = 10000, max_num_levels = 2);
END;
```

## Analyze your queries

Use the `EXPLAIN ANALYZE` command to analyze your query insights as shown in the following example SQL query.

```sql
  EXPLAIN ANALYZE SELECT result_column
  FROM my_table
  ORDER BY EMBEDDING_COLUMN <=> embedding('text-embedding-005', 'What is a database?')::vector
  LIMIT 1;
```

The example response `QUERY PLAN` includes information such as the time taken, the number of rows scanned or returned, and the resources used.

    Limit  (cost=0.42..15.27 rows=1 width=32) (actual time=0.106..0.132 rows=1 loops=1)
      ->  Index Scan using my_scann_index on my_table  (cost=0.42..858027.93 rows=100000 width=32) (actual time=0.105..0.129 rows=1 loops=1)
            Order By: (embedding_column <=> embedding('text-embedding-005', 'What is a database?')::vector(768))
            Limit value: 1
    Planning Time: 0.354 ms
    Execution Time: 0.141 ms

## View vector index metrics

You can use the vector index metrics to review performance of your vector index,
identify areas for improvement, and tune your index based on the metrics, if
needed.

To view all vector index metrics, run the following SQL query, which uses the
`pg_stat_ann_indexes` view:

```sql
SELECT * FROM pg_stat_ann_indexes;
```

For more information about the complete list of metrics, see [Vector index
metrics](https://docs.cloud.google.com/alloydb/omni/containers/16.9.0/docs/reference/vector-index-metrics).

## What's next

- [An example embedding workflow](https://docs.cloud.google.com/alloydb/omni/containers/16.9.0/docs/ai/example-embeddings.md.txt)