以下查询需要一段时间才能返回:
db.Query<Person>(x => x.StartsWith("Chr", StringComparison.CurrentCultureIgnoreCase))
有没有办法让它正常工作?即更快?
也许您遇到了db4o 查询优化的限制。通常 Native Queries 和 LINQ-Queries 被转换为低级SODA-query。当此优化失败时,db4o 实例化数据库中的对象以执行查询。正如您可以想象的那样,这可能会很慢。
当前最好的解决方案是在这种情况下直接使用 SODA。例如具有一个属性的类:
public class SimpleObject
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
像这样的本机查询:
var result = db.Query<SimpleObject>(x => x.Name.StartsWith ("Chr",StringComparison.CurrentCultureIgnoreCase));
可以用这个 SODA-Query 表示:
IQuery query = db.Query();
query.Constrain(typeof (SimpleObject)); // restrict to a certain class
query.Descend("name").Constrain("Chr").StartsWith(false); // the field 'name' starts with 'chr', case-insensitive
foreach (var s in query.Execute())
{
//
}
我希望查询优化器的未来版本直接支持这种情况。
adding and index on the column you're comparing would probably help.