NebulaDB supports indexing to improve query performance. This guide explains how to use indexes effectively.
Indexes in NebulaDB are data structures that improve the speed of data retrieval operations. They work by creating a reference to the data based on specific fields, allowing the database to find documents without scanning the entire collection.
import { createDb, IndexType } from '@nebula/core';
import { MemoryAdapter } from '@nebula/adapter-memory';
const db = createDb({ adapter: new MemoryAdapter() });
const users = db.collection('users', {
indexes: [
{ name: 'email_idx', fields: ['email'], type: IndexType.UNIQUE },
{ name: 'name_age_idx', fields: ['name', 'age'], type: IndexType.COMPOUND }
]
});
import { IndexType } from '@nebula/core';
users.createIndex({ name: 'age_idx', fields: ['age'], type: IndexType.SINGLE });
users.createIndex({ name: 'username_idx', fields: ['username'], type: IndexType.UNIQUE });
users.createIndex({ name: 'location_idx', fields: ['country', 'city'], type: IndexType.COMPOUND });
NebulaDB automatically uses indexes when processing queries. For an index to be used, the query must include the indexed fields with equality conditions:
// Will use the 'email_idx' index
const user = await users.findOne({ email: 'alice@example.com' });
// Will use the 'name_age_idx' compound index
const results = await users.find({ name: 'Alice', age: 30 });
// Will NOT use the 'name_age_idx' index (range query on age)
const results = await users.find({ name: 'Alice', age: { $gt: 25 } });
const indexes = users.getIndexes();
console.log(indexes);
// [{ name: 'email_idx', fields: ['email'], type: 'unique' }, ...]
users.dropIndex('age_idx');
$or, $not, etc. may not fully utilize indexesconst products = db.collection('products');
for (let i = 0; i < 10000; i++) {
await products.insert({
name: `Product ${i}`,
category: ['A', 'B', 'C'][i % 3],
price: Math.floor(Math.random() * 1000),
inStock: Math.random() > 0.5
});
}
console.time('Without index');
const resultsWithoutIndex = await products.find({ category: 'B', inStock: true });
console.timeEnd('Without index');
products.createIndex({
name: 'category_stock_idx',
fields: ['category', 'inStock'],
type: IndexType.COMPOUND
});
console.time('With index');
const resultsWithIndex = await products.find({ category: 'B', inStock: true });
console.timeEnd('With index');