13. 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:
db4o允许通过对字段进行索引来提供最优的查询性能。为了创建一个索引,你需要在打开任何ObjectContainer/ObjectServer之前, 在db4o全局配置方法中执行下面的API方法调用:
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 application.
在如上面那般设置好配置后,在下次打开ObjectContainer/ObjectServer并在第一次使用Foo类的时,一个在Foo#bar字段上的索引将会被建立(如果尚未存在)。
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.
和所有的其它配置调用相比:索引一旦被创建, 即使在打开ObjectContainer/ObjectServer之前没有执行索引配置的调用,它也会一直留在数据库里。
To drop an index you would also issue a configuration call in your db4o configuration method:
如果要删除一个索引, 你可以在db4o配置函数中执行如下配置调用:
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:
如果你选择1级或更高的消息级别,db4o会告诉你它在何时创建或者删除了索引:
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.
要在大量的对象上创建和删除索引, 这里有两种策略:
(1) 在索引关闭的状态下导入所有的对象,配置索引,然后再打开ObjectContainer/ObjectServer。
(2) 在索引打开的状态下导入所有的对象,然后有规律性地按批次提交固定数目(约10,000个)的对象。
(1) 会更快一些。
(2) 能保持较低的内存使用率。