You are here: Tuning > Main Operations Performance > Query Performance > Indexing

Indexing

Using indexes is always a good idea to improve query performance. The following test illustrates index performance impact:

QueryPerformanceBenchmark.cs: RunIndexTest
private void RunIndexTest()
         {

            Init();
            System.Console.WriteLine("Storing " + _count + 
" objects with " + _depth
                    + " levels of embedded objects:");

            Clean();
            System.Console.WriteLine(" - no index");
            Open(Configure());
            Store();
            Close();
            Open(Configure());
            StartTimer();
            IQuery query = objectContainer.Query();
            query.Constrain(typeof(Item));
            query.Descend("_name").Constrain("level1/2");
            IList result = query.Execute();
            Item item = (Item)result[0];
            StopTimer("Querying object with string field: " + item._name);
            Close();


            System.Console.WriteLine(" - index on string field");
            // Open to create index
            Open(ConfigureIndex());
            Close();
            Open(Configure());
            StartTimer();
            query = objectContainer.Query();
            query.Constrain(typeof(Item));
            query.Descend("_name").Constrain("level1/2");
            result = query.Execute();
            item = (Item)result[0];
            StopTimer("Querying object with string field: " + item._name);
            Close();
        }
QueryPerformanceBenchmark.cs: InitForHardDriveTest
private void InitForHardDriveTest()
         {
            _count = 10000;
            _depth = 3;
            _filePath = "performance.db4o";
            _isClientServer = false;

        }
QueryPerformanceBenchmark.cs: ConfigureRamDrive
private IConfiguration ConfigureRamDrive()
         {
            IConfiguration config = Db4oFactory.NewConfiguration();
            config.FlushFileBuffers(true);
            return config;
        }

Results from the test machine:

Storing 10000 objects with 3 levels of embedded objects:

- no index

Store 30000 objects: 2228ms

Querying object with String field: level1/2: 461ms

- index on String field

Querying object with String field: level1/2: 460ms

Download example code:

c#