# 14. Indexes

db4o allows to index fields to provide maximum querying performance. To request an index to be created, you would issue the following API method call in your global db4o configuration method before you open an ObjectContainer/ObjectServer:

`// assuming class Foo{ String bar; } Db4o.configure().objectClass(Foo.class).objectField("bar").indexed(true);`

If the configuration is set in this way, an index on the Foo#bar field will be created (if not present already) the next time you open an
ObjectContainer/ObjectServer and you use the Foo class the first time
in your applcation.
Contrary to all other configuration calls indexes - once created - will remain in a database even if the index configuration call is not issued before opening an ObjectContainer/ObjectServer.
To drop an index you would also issue a configuration call in your db4o configuration method:

`Db4o.configure().objectClass(Foo.class).objectField("bar").indexed(false);`

Actually dropping the index will take place the next time the respective class is used.
db4o will tell you when it creates and drops indexes, if you choose a message level of 1 or higher:

`Db4o.configure().messageLevel(1);`

For creating and dropping indexes on large amounts of objects there are two possible strategies:
(1) Import all objects with indexing off, configure the index and reopen the ObjectContainer/ObjectServer.
(2) Import all objects with indexing turned on and commit regularly for a fixed amount of objects (~10,000).
(1) will be faster.
(2) will keep memory consumption lower.
Further reading
To improve performance even further, consider reading about some of the Tuning options discussed elsewhere in this tutorial.
--
generated by Doctor courtesy of db4objects Inc.