我有一个包含大量传感器日志(+10000)的数据库。如果我将查询约束到特定的日期 - 时间范围,它会变得非常慢。如何加快速度? 在我的开发机器上大约需要150毫秒,但在嵌入式设备上需要+ 20秒
Db4objects.Db4o 8.0.160.14822(.NET)
Query1(...)是缓慢的事情。 : - (
public IObjectSet Query1(DateTime begin, DateTime end, Guid[] owners) {
DateTime startTime = DateTime.Now;
var query = _db.Query();
query.Constrain(typeof(LogEntry));
IConstraint cBegin = query.Descend("Date").Constrain(begin).Greater();
query.Descend("Date").Constrain(end).Smaller().And(cBegin);
if (owners != null && owners.Length > 0) {
IConstraint temp = query.Descend("OwnerGuid").Constrain(owners[0]);
for (int i = 1; i < owners.Length; i++) {
temp.Or(query.Descend("OwnerGuid").Constrain(owners[i]));
}
cBegin.And(temp);
}
System.Diagnostics.Debug.WriteLine(string.Format("DB Query1 QueryBuild: {0}ms", (DateTime.Now - startTime).TotalMilliseconds));
DateTime startTime2 = DateTime.Now;
IObjectSet result = query.Execute();
System.Diagnostics.Debug.WriteLine(string.Format("DB Query1 Execute: {0}ms", (DateTime.Now - startTime2).TotalMilliseconds));
return result;
}
public IEmbeddedConfiguration ConfigDb() {
IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.Common.ObjectClass(typeof(Owner)).ObjectField("ID").Indexed(true);
configuration.Common.ObjectClass(typeof(LogEntry)).ObjectField("OwnerGuid").Indexed(true);
configuration.Common.ObjectClass(typeof(LogEntry)).ObjectField("Date").Indexed(true);
configuration.File.LockDatabaseFile = false;
configuration.Common.OptimizeNativeQueries = true;
return configuration;
}
public class LogEntry {
public readonly Owner Owner;
public readonly object Value;
public readonly DateTime Date;
public Guid OwnerGuid;
public LogEntry(DateTime date, Owner owner, object value) {
Owner = owner;
OwnerGuid = owner.ID;
Value = value;
Date = date;
}
}
public class Owner {
public readonly Guid ID;
public readonly string Name;
public readonly DataType DataTyp;
public Owner() { }
public Owner(ILogable owner) {
ID = owner.Guid;
Name = owner.Description;
DataTyp = owner.DataType;
}
}
编辑:
下面的用法Db4objects.Db4o.IObjectSet queryResult = Manager.Instance.SensorLogger.Query1(begin, end, new Guid[] { iLogable });
int count = (int)Math.Floor((double)(queryResult.Count / resolution));
if (count == 0) count = 1;
int i = 0;
while (i < queryResult.Count) {
LogEntry e = queryResult[i] as LogEntry;
if (e != null) {
results.Add(e);
}
i += count;
}
或者有没有办法只选择几个对象?假设我们在数据库中每隔5分钟创建一个快照,但我需要每1小时快照一次。