# REBUILD

The REBUILD statement is used to rebuild indexes.

The `REBUILD` statement is used to rebuild indexes in SurrealDB. It is usually used in relation to a specified [Index](/docs/reference/query-language/statements/define/indexes.md) to optimise performance. It is useful to rebuild indexes because sometimes [HNSW](/docs/reference/query-language/statements/define/indexes.md#hnsw-hierarchical-navigable-small-world) index performance can degrade due to frequent updates.

Rebuilding the index will ensure the index is fully optimised.

> [!NOTE]
> By default, `REBUILD INDEX` waits until the rebuild finishes before the statement returns (the same behaviour as `DEFINE INDEX` without `CONCURRENTLY`). Adding `CONCURRENTLY` on the rebuild statement will cause it to return immediately, after which progress can be monitored via [`INFO FOR INDEX`](/docs/reference/query-language/statements/info.md#index-information). Whether the index was originally created with `CONCURRENTLY` does not affect rebuilds. See the [`CONCURRENTLY` clause](/docs/reference/query-language/statements/define/indexes.md#using-concurrently-clause) on `DEFINE INDEX` for the same blocking vs non-blocking distinction when creating an index.

### Statement syntax

**SurrealQL Syntax**

```syntax title="SurrealQL Syntax"
REBUILD [
	INDEX [ IF EXISTS ] @name ON [ TABLE ] @table [ CONCURRENTLY ]
]
```

> [!NOTE]
> The `IF EXISTS` and TABLE clauses are optional.

## Example usage

For example, if you have a table called `book` and you have an index called `uniq_isbn` on the `isbn` field, you can rebuild the index using the following query:

```surql
REBUILD INDEX uniq_isbn ON book;
```

```surql
CREATE book:1 SET title = 'Rust Web Programming', isbn = '978-1803234694', author = 'Jon Doe';

// Define a unique index on the isbn field
DEFINE INDEX uniq_isbn ON book FIELDS isbn UNIQUE;

// Rebuild this index incase of more updates
REBUILD INDEX IF EXISTS uniq_isbn ON book;

// Check that the index has been created
INFO FOR TABLE book;

REBUILD INDEX IF EXISTS idx_author ON book;

REBUILD INDEX IF EXISTS ft_title ON book;

// Define index on the author field
DEFINE INDEX idx_author ON book FIELDS author;

// Define an analyzer which has blank and class Tokenizers and converts the tokens to lowercase
DEFINE ANALYZER simple TOKENIZERS blank,class FILTERS lowercase;

DEFINE INDEX ft_title ON book FIELDS title SEARCH ANALYZER simple BM25 HIGHLIGHTS;

REBUILD INDEX uniq_isbn ON book;

REBUILD INDEX idx_author ON book;

REBUILD INDEX ft_title ON book;

// Check that the index has been created
INFO FOR TABLE book;

//Checks whether the term RUST IS found in a full-text indexed field.
SELECT * FROM book WHERE title @@ 'Rust';
```

[Run this example in SurrealDB Studio](https://app.surrealdb.com/mini?query=CREATE+book%3A1+SET+title+%3D+%27Rust+Web+Programming%27%2C+isbn+%3D+%27978-1803234694%27%2C+author+%3D+%27Jon+Doe%27%3B%0A%0A%2F%2F+Define+a+unique+index+on+the+isbn+field%0ADEFINE+INDEX+uniq_isbn+ON+book+FIELDS+isbn+UNIQUE%3B%0A%0A%2F%2F+Rebuild+this+index+incase+of+more+updates%0AREBUILD+INDEX+IF+EXISTS+uniq_isbn+ON+book%3B%0A%0A%2F%2F+Check+that+the+index+has+been+created%0AINFO+FOR+TABLE+book%3B%0A%0AREBUILD+INDEX+IF+EXISTS+idx_author+ON+book%3B%0A%0AREBUILD+INDEX+IF+EXISTS+ft_title+ON+book%3B%0A%0A%2F%2F+Define+index+on+the+author+field+%0ADEFINE+INDEX+idx_author+ON+book+FIELDS+author%3B%0A%0A%2F%2F+Define+an+analyzer+which+has+blank+and+class+Tokenizers+and+converts+the+tokens+to+lowercase+%0ADEFINE+ANALYZER+simple+TOKENIZERS+blank%2Cclass+FILTERS+lowercase%3B%0A%0ADEFINE+INDEX+ft_title+ON+book+FIELDS+title+SEARCH+ANALYZER+simple+BM25+HIGHLIGHTS%3B%0A%0AREBUILD+INDEX+uniq_isbn+ON+book%3B%0A%0AREBUILD+INDEX+idx_author+ON+book%3B%0A%0AREBUILD+INDEX+ft_title+ON+book%3B%0A%0A%2F%2F+Check+that+the+index+has+been+created%0AINFO+FOR+TABLE+book%3B%0A%0A%2F%2FChecks+whether+the+term+RUST+IS+found+in+a+full-text+indexed+field.%0ASELECT+*+FROM+book+WHERE+title+%40%40+%27Rust%27%3B&orientation=horizontal)

### Using if exists clause

The following queries show an example of how to rebuild resources using the `IF EXISTS` clause, which will only rebuild the resource if it exists.

```surql
REBUILD INDEX IF EXISTS uniq_isbn ON book;
```
