|
| 1 | +# Graph Index <badge type="tip" text="since v0.5.0" /> |
| 2 | + |
| 3 | +VectorChord's index type `vchordg` is a disk-based graph index. It provides slow build time and low memory consumption. |
| 4 | + |
| 5 | +To build a vector index, start by creating a table named `items` with an `embedding` column of type `vector(n)`, then populate it with sample data. |
| 6 | + |
| 7 | +To create a `vchordg` index, you can use the following SQL. |
| 8 | + |
| 9 | +```sql |
| 10 | +CREATE INDEX ON items USING vchordg (embedding vector_l2_ops); |
| 11 | +``` |
| 12 | + |
| 13 | +## Tuning |
| 14 | + |
| 15 | +When building an index, you usually need to tune two parameters: `m` and `ef_construction`. `m` is the maximum number of neighbors for each vertex, and `ef_construction` is the search range when building the graph for each vertex. `m` corresponds to $m_0$ in HNSW and $m$ in DiskANN. `ef_construction` corresponds to $\text{ef}_\text{construction}$ in HNSW and $\text{ef}_C$ in DiskANN. In search, you need to tune `ef_search`. `ef_search` corresponds to $\text{ef}$ in HNSW and DiskANN. |
| 16 | + |
| 17 | +```sql |
| 18 | +CREATE INDEX ON items USING vchordg (embedding vector_l2_ops) WITH (options = $$ |
| 19 | +m = 64 |
| 20 | +ef_construction = 128 |
| 21 | +$$); |
| 22 | + |
| 23 | +SET vchordg.ef_search TO '128'; |
| 24 | +SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 10; |
| 25 | +``` |
| 26 | + |
| 27 | +As a disk-based index, `vchordg` usually only requires the quantized vectors to be in the buffer pool to maintain performance. By default, `vchordg` quantizes a $D$-dimensional vector to $2D$ bits. Let the number of rows be $N$, then the total memory required for the index is $2DN$ bits. If you have very limited memory and are using ultra-high dimensional vectors, you can consider setting quantization to $1$ bit. |
| 28 | + |
| 29 | +```sql |
| 30 | +CREATE INDEX ON items USING vchordg (embedding vector_l2_ops) WITH (options = $$ |
| 31 | +bits = 1 |
| 32 | +m = 64 |
| 33 | +ef_construction = 128 |
| 34 | +$$); |
| 35 | +``` |
| 36 | + |
| 37 | +The index building can be sped up using multiple processes. Refer to [PostgreSQL Tuning](performance-tuning.md#indexing). |
| 38 | + |
| 39 | +## Reference |
| 40 | + |
| 41 | +### Operator Classes <badge type="info" text="vchordg" /> {#operator-classes} |
| 42 | + |
| 43 | +The following table lists all available operator classes supported by `vchordg`. |
| 44 | + |
| 45 | +| Operator Class | Description | Operator 1 | Operator 2 | |
| 46 | +| -------------------- | --------------------------------------------------------- | ---------------------- | ------------------------ | |
| 47 | +| `vector_l2_ops` | index works for `vector` type and Euclidean distance | `<->(vector,vector)` | `<<->>(vector,vector)` | |
| 48 | +| `vector_ip_ops` | index works for `vector` type and negative inner product | `<#>(vector,vector)` | `<<#>>(vector,vector)` | |
| 49 | +| `vector_cosine_ops` | index works for `vector` type and cosine distance | `<=>(vector,vector)` | `<<=>>(vector,vector)` | |
| 50 | +| `halfvec_l2_ops` | index works for `halfvec` type and Euclidean distance | `<->(halfvec,halfvec)` | `<<->>(halfvec,halfvec)` | |
| 51 | +| `halfvec_ip_ops` | index works for `halfvec` type and negative inner product | `<#>(halfvec,halfvec)` | `<<#>>(halfvec,halfvec)` | |
| 52 | +| `halfvec_cosine_ops` | index works for `halfvec` type and cosine distance | `<=>(halfvec,halfvec)` | `<<=>>(halfvec,halfvec)` | |
| 53 | + |
| 54 | +`<<->>`, `<<#>>`, `<<=>>` are operators defined by VectorChord. |
| 55 | + |
| 56 | +For more information about `<<->>`, `<<#>>`, `<<=>>`, refer to [Similarity Filter](range-query). |
| 57 | + |
| 58 | +All operator classes are available since version `0.3.0`. |
| 59 | + |
| 60 | +### Indexing Options <badge type="info" text="vchordg" /> |
| 61 | + |
| 62 | +#### `bits` <badge type="tip" text="since v0.5.0" /> |
| 63 | + |
| 64 | +- Description: . |
| 65 | +- Type: integer |
| 66 | +- Default: `2` |
| 67 | +- Example: |
| 68 | + - `bits = 2` means that a $D$-dimensional vector is quantized to $D$ bits. |
| 69 | + - `bits = 1` means that a $D$-dimensional vector is quantized to $2D$ bits . |
| 70 | + |
| 71 | +#### `m` <badge type="tip" text="since v0.5.0" /> |
| 72 | + |
| 73 | +- Description: . |
| 74 | +- Type: integer |
| 75 | +- Default: `32` |
| 76 | +- Example: |
| 77 | + - `m = 32` means that there are at most $32$ neighbors for each vertex. |
| 78 | + - `m = 64` means that there are at most $64$ neighbors for each vertex. |
| 79 | + |
| 80 | +#### `alpha` <badge type="tip" text="since v0.5.0" /> |
| 81 | + |
| 82 | +- Description: . |
| 83 | +- Type: list of floats |
| 84 | +- Default: `[1.0, 1.2]` |
| 85 | +- Example: |
| 86 | + - `alpha = [1.0, 1.2]` means that . |
| 87 | + - `alpha = [1.0]` means that . |
| 88 | + |
| 89 | +#### `ef_construction` <badge type="tip" text="since v0.5.0" /> |
| 90 | + |
| 91 | +- Description: . |
| 92 | +- Type: integer |
| 93 | +- Default: `64` |
| 94 | +- Example: |
| 95 | + - `ef_construction = 64` means that . |
| 96 | + - `ef_construction = 128` means that . |
| 97 | + |
| 98 | +#### `beam_construction` <badge type="tip" text="since v0.5.0" /> |
| 99 | + |
| 100 | +- Description: . |
| 101 | +- Type: integer |
| 102 | +- Default: `1` |
| 103 | +- Example: |
| 104 | + - `beam_construction = 8` means that . |
| 105 | + - `beam_construction = 1` means that . |
| 106 | + |
| 107 | +### Search Parameters <badge type="info" text="vchordg" /> |
| 108 | + |
| 109 | +#### `vchordg.ef_search` <badge type="tip" text="since v0.5.0" /> |
| 110 | + |
| 111 | +- Description: . |
| 112 | +- Type: integer |
| 113 | +- Default: `64` |
| 114 | +- Domain: `[1, 65535]` |
| 115 | +- Example: |
| 116 | + - `SET vchordg.ef_search = 64` indicates . |
| 117 | + - `SET vchordg.ef_search = 128` indicates . |
| 118 | + |
| 119 | +#### `vchordg.beam_search` <badge type="tip" text="since v0.5.0" /> |
| 120 | + |
| 121 | +- Description: . |
| 122 | +- Type: integer |
| 123 | +- Default: `64` |
| 124 | +- Domain: `[1, 65535]` |
| 125 | +- Example: |
| 126 | + - `SET vchordg.beam_search = 8` indicates . |
| 127 | + - `SET vchordg.beam_search = 1` indicates . |
0 commit comments