2009年4月4日 星期六

用 index 改善 db4o query 的效能

在 code 中記得要對會需要進行 query 的 field 進行 index,可以大幅改善效能。下面是一個例子:
Db4oFactory.Configure().ObjectClass(typeof(GeneOntologyInfo)).ObjectField("id").Indexed(true);  // 重點是這段;因為我們會常常搜尋 id==X 的物件,故對該 field 進行 index
gene2GO = Db4oFactory.OpenFile(GENE2GO_DB);
using (StreamReader r = new StreamReader(GENE2GO_SOURCE))
{
 while ((line = r.ReadLine()) != null)
 {                            
   string[] info = Regex.Split(line, @"\t");
   GeneOntologyInfo ginfo = new GeneOntologyInfo(info[2], info[3], info[4], info[0]);
   gene2GO.Store(ginfo);
 }
}


// query 的程式碼
IQuery query = gene2GO.Query();
query.Constrain(typeof(GeneOntologyInfo));
query.Descend("id").Constrain(id);
IObjectSet result = query.Execute();
// 略