You are here: Basics Operations & Concepts > Indexing

Indexing

db4o supports indexes like most databases do. Indexes are data structures which allow efficient lookup of data. When you enable an index, db4o will add an entry to index for each object. This makes the insert and update operation a little slower. However it makes queries a lot faster. A query which uses an index is a order of magnitude faster that a query which cannot use a index.

You can create a index by enabling it on a field. See "Adding a Field Index"

Note that you need to set the index on a field, not a property. This is also true for auto-properties.

IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.Common.ObjectClass(typeof (Person)).ObjectField("name").Indexed(true);
ObjectFieldConfigurations.cs: Index a certain field
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
configuration.Common.ObjectClass(GetType(Person)).ObjectField("name").Indexed(True)
ObjectFieldConfigurations.vb: Index a certain field

As an alternative you also can use the appropriate Attribute on the field which you want to index.

[Indexed]
private string zipCode;
ObjectFieldConfigurations.cs: Index a field
<Indexed()> _
Private m_zipCode As String
ObjectFieldConfigurations.vb: Index a field

When And Where Do I Need An Index

When do you need an index? As a rule of thumb: Add an index all fields which are used in queries. See "When And Where"

There are different factors which need to be fulfilled to profit from an index.