You are here: Tuning > Main Operations Performance > Insert Performance > Indexes

Indexes

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:

InsertPerformanceBenchmark.cs: RunIndexTest
private void RunIndexTest()
         {
            Init();
            System.Console.WriteLine("Storing " + _count + " objects with " 
+ _depth + " levels of embedded objects:");

            Clean();
            Configure();
            System.Console.WriteLine(" - no index");
            Open();
            Store();
            Close();

            ConfigureIndex();
            System.Console.WriteLine(" - index on String field");
            Open();
            Store();
            Close();
        }
InsertPerformanceBenchmark.cs: Configure
private void Configure()
         {
            IConfiguration config = Db4oFactory.Configure();
            config.LockDatabaseFile(false);
            config.WeakReferences(false);
            config.Io(new MemoryIoAdapter());
            config.FlushFileBuffers(false);
        }
InsertPerformanceBenchmark.cs: ConfigureIndex
private void ConfigureIndex()
         {
            IConfiguration config = Db4oFactory.Configure();
            config.LockDatabaseFile(false);
            config.WeakReferences(false);
            config.Io(new MemoryIoAdapter());
            config.FlushFileBuffers(false);
            config.ObjectClass(typeof(Item)).ObjectField("_name").Indexed(true);
        }
InsertPerformanceBenchmark.cs: Init
private void Init()
         {
            _count = 10000;
            _depth = 3;
            _isClientServer = false;
        }
InsertPerformanceBenchmark.cs: Store
private void Store()
         {
            StartTimer();
            for (int i = 0; i < _count; i++)
             {
                Item item = new Item("load", null);
                for (int j = 1; j < _depth; j++)
                 {
                    item = new Item("load", item);
                }
                objectContainer.Store(item);
            }
            objectContainer.Commit();
            StopTimer("Store " + TotalObjects() + " objects");
        }

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

Download example code:

c#