> ## Documentation Index
> Fetch the complete documentation index at: https://docs.regatta.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Creating a Vector Index

The following command creates a vector index.

## Synopsis

```sql theme={null}
CREATE VECTOR INDEX [ index_name ] ON table_name  
  USING { HNSW | IVFFLAT } (column_name distance_metric) 
  [ WITH ( option [, ... ] ) ] 

where distance_metric is one of {COSINE, L2, IP} 

and option is in the form of <param> = <value> 

The following options apply to any vector index: 
  parallelism = {FULL, HIGH, MEDIUM, LOW} 
  modules = ((module_idname [, parallelism]), ...) 

Other options are specific to the index type and are listed below. 
```

## Description

A vector index can be created on a vector column to provide appropriate searches for vectors that are “close” to a given vector according to one of the distance metrics described in the previous section. Regatta supports two types of vector indexes: `HNSW `and `IVFFLAT`. 

Each index type supports all the distance metrics, but each index uses a single distance metric and supports approximate searches only for that metric. Multiple vector indexes can be created for the same vector column, as long as they differ in the combination of index type and distance metric. 

An index vector is created for all the rows of a table, or only for rows residing in specific modules. Modules can be added to and removed from the index at any time using the `ALTER VECTOR INDEX` command (see [Modify Vector Indexes](/sql/vector-index/ddl/modifying-vector-index)). However, once the index was built for a specific module, it does not change – it is not updated when rows are inserted or updated. In order to update the index for the rows residing in a module, the module should be removed from the index and added back. This will result in rebuilding the index for the module’s rows. 

The `CREATE VECTOR INDEX` command creates the index and initiates the build process. It returns once the index was created and the build process was initiated. Only after the build process is completed, the index becomes usable for approximate vector search. The `SHOW VECTOR INDEXES` command (see [Show Vector Indexes](/sql/vector-index/show-commands#show-vector-indexes)) can be used to query the status of the index. 

## Parameters

***index\_name*** 

The name of the index. If not specified, RegattaDB generates a name. 

`ON table_name` 

Specifies the table containing the vector column to be indexed. 

`USING { HNSW | IVFFLAT } `

This parameter indicates that the index is a vector index that supports approximate search. 

***column\_name*** 

Specifies the column to be indexed. The column must be of type vector. 

***distance\_metric*** 

This parameter can be one of the following: 

`COSINE `– Cosine similarity between vectors. 

`L2 `– Euclidean distance. 

`IP` – Inner product (also known as dot product) 

`WITH ( option [, ... ] )` 

Specifies various options for the index creation. The options are listed below. Some of them apply to any vector index type, and some are specific to a certain type.

## General Options

`parallelism = {FULL, HIGH, MEDIUM, LOW} `

This is a hint for determining the number of CPU cores that participate in the construction of the index in each module. The default value is FULL, which is the highest level of parallelism. The actual number of cores participating in the index construction is determined by an internal RegattaDB algorithm and depends on the actual number of cores. 

The higher the parallelism is set, the faster the index will be built. On the other hand, highly-parallel build can potentially impact transaction performance, especially when that processing is CPU-intensive. If you create a vector index while the system is running a high load of mission-critical transaction processing, consider using this parameter to lower the parallelism of the index build to avoid impact to the transactions performance. 

`Modules = ((module_name [, parallelism]), ...)` 

Specifies a list of modules for which the vector index should be built, out of the modules in which the table resides. The index will be built only for rows residing in those modules. If this option is not specified, the index is built for all the table rows. 

When modules are specified, an optional parallelism parameter can be used for each specified module. This allows setting parallelism on a per-module basis, overriding the index-wide parameter. 

<Note>
  Modules can be added and removed using the `ALTER VECTOR INDEX` command (see [Modify Vector Indexes](/sql/vector-index/ddl/modifying-vector-index))
</Note>

### Options for the HNSW Index Type 

| Name(s)                 | Value Range    | Default Value                 | Description                                                                        |
| ----------------------- | -------------- | ----------------------------- | ---------------------------------------------------------------------------------- |
| `m connectivity`        | `[2, 64]`      | 16                            | The maximum number of connections per layer for all the layers other than layer 0. |
| `m_0 connectivity_base` | `[m, 64]`      | The minimum between 2m and 64 | The maximum number of connections in layer 0.                                      |
| `ef_construction`       | `[m_0, 65535]` | max(m\_0, 40)                 | The size of the dynamic candidate list for constructing the graph.                 |

### Options for the IVFFLAT Index Type

There is currently one option that applies only to `IVFFLAT `Index type. 

`lists | num_clusters `

This option is applied in each module according to the number of rows residing in the module. The following values can be specified: 

`FUNCTION SQRT [ (c) ] `

The number of clusters is the square root of the number of the rows, multiplied by a constant c. The argument c is optional, the default for c is 1. 

`FUNCTION DIV(n) `

The number of clusters is the number of rows divided by n. 

`FUNCTION MIN_DIV_SQRT(n [, c]) `

The number of clusters is the minimum between `DIV(n)` and `SQRT(c) `

This parameter is optional. The default is `FUNCTION MIN_DIV_SQRT(1000) `

Example:

```sql theme={null}
-- Create an IVEFLAT vector index with default parameters and system- generated name on all the modules the table resides in
CREATE VECTOR INDEX ON mytable USING IVEFLAT (mycol IP);

-- Create an HNSW vector index with your own name and parameter values on a selected set of modules, parallelism control and other options
CREATE VECTOR INDEX mytable_mycol_hnsw_ip ON mytable USING HNSW (mycol L2)
	WITH (parallelisn = HIGH,
		  modules = ((module_a), (module_b, MEDIUM),
		  ef_construction = 32,
		  m = 10
	);
```
