Create Index
Create database indexes to improve query performance on your Boltic Tables. Indexes provide fast access paths to data, improving SELECT query performance, sorting, and filtering operations.
Method Signature
addIndex(
tableName: string,
request: AddIndexRequest
): Promise<BolticSuccessResponse<AddIndexResponse> | BolticErrorResponse>
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
tableName | string | ✅ | Name of the table to create an index on |
request | AddIndexRequest | ✅ | Index creation configuration with field names and index type |
AddIndexRequest
interface AddIndexRequest {
field_names: string[]; // Columns to include in the index
method: "btree" | "hash" | "spgist" | "gin" | "brin"; // Index type
}
| Property | Type | Required | Description |
|---|---|---|---|
field_names | string[] | ✅ | Array of column names to include in the index |
method | string | ✅ | Index type (btree, hash, gin, brin, or spgist) |
tip
In composite indexes, column order matters. Filters and sorts are most effective when they follow the same left-to-right order as the index definition.
Index Methods
Choose the index method based on your query patterns:
btree: Best for equality and range queries (=,<,>,BETWEEN)hash: Best for exact equality matches only (=)gin: Best for multi-valued data (e.g., dropdown multi-select) and full-text searchbrin: Best for large tables with naturally ordered dataspgist: Best for specialized data types (geometric, network addresses)
Response
interface AddIndexResponse {
data: object;
message: str;
}
Examples
Basic Index Creation
import { createClient } from "@boltic/sdk";
const client = createClient("your-api-key");
// Create a B-tree index on email field
const { data: index, error } = await client.indexes.addIndex("users", {
field_names: ["email"],
method: "btree",
});
if (error) {
console.error("Failed to create index:", error.message);
} else {
console.log("Index created:", index.index_name);
}
Composite Indexes
// Multi-column index for complex queries
const compositeIndex = await client.indexes.addIndex("events", {
field_names: ["user_id", "event_type", "created_at"],
method: "btree",
});
Chained Access Method
// Using chained access pattern
const { data: index } = await client
.from("users")
.indexes()
.addIndex({
field_names: ["email"],
method: "btree",
});