One more feature that inevitably decreases the insert performance: indexes. When a new object with indexed field is inserted an index should be created and written to the database, which consumes additional resources. Luckily indexes do not only reduce the performance, actually they will improve the performance to a much more valuable degree during querying.
An example below provides a simple comparison of storing objects with and without indexes:
01private void RunIndexTest() 02
{ 03
Init(); 04
System.Console.WriteLine("Storing " + _count + " objects with " + _depth + " levels of embedded objects:"); 05
06
Clean(); 07
Configure(); 08
System.Console.WriteLine(" - no index"); 09
Open(); 10
Store(); 11
Close(); 12
13
ConfigureIndex(); 14
System.Console.WriteLine(" - index on String field"); 15
Open(); 16
Store(); 17
Close(); 18
}
1private void Configure() 2
{ 3
IConfiguration config = Db4oFactory.Configure(); 4
config.LockDatabaseFile(false); 5
config.WeakReferences(false); 6
config.Io(new MemoryIoAdapter()); 7
config.FlushFileBuffers(false); 8
}
1private void ConfigureIndex() 2
{ 3
IConfiguration config = Db4oFactory.Configure(); 4
config.LockDatabaseFile(false); 5
config.WeakReferences(false); 6
config.Io(new MemoryIoAdapter()); 7
config.FlushFileBuffers(false); 8
config.ObjectClass(typeof(Item)).ObjectField("_name").Indexed(true); 9
}
1private void Init() 2
{ 3
_count = 10000; 4
_depth = 3; 5
_isClientServer = false; 6
}
01private void Store() 02
{ 03
StartTimer(); 04
for (int i = 0; i < _count; i++) 05
{ 06
Item item = new Item("load", null); 07
for (int j = 1; j < _depth; j++) 08
{ 09
item = new Item("load", item); 10
} 11
objectContainer.Set(item); 12
} 13
objectContainer.Commit(); 14
StopTimer("Store " + TotalObjects() + " objects"); 15
}
The following results were achieved for the testing configuration:
.NET:
Storing 10000 objects with 3 levels of embedded objects:
- no index
Store 30000 objects: 1235ms
- index on String field
Store 30000 objects: 1748ms