由于我的数据库的特殊设计结构,我在 EF 中的性能非常差。以下是相关关系:
我有以下数据模型:
public class Sensor
{
[Key]
public int Id { get; set; }
[Required, MaxLength(64)]
public string Name { get; set; }
[Required, ForeignKey("Type")]
public int SensorTypeId { get; set; }
public virtual SensorType Type { get; set; }
public virtual ICollection<SensorSample> SensorSamples { get; set; }
}
public class SensorSample
{
[Key]
public int Id { get; set; }
[Required, ForeignKey("Sensor")]
public int SensorId { get; set; }
public virtual Sensor Sensor { get; set; }
[Required]
public DateTime SampleTime { get; set; }
[Required]
public virtual ICollection<SampleData> SampleData { get; set; }
}
public class SampleData
{
[Key]
public int Id { get; set; }
[Required, ForeignKey("DataType")]
public int SampleDataTypeId { get; set; }
public virtual SampleDataType DataType { get; set; }
[Required, ForeignKey("Unit")]
public int SampleUnitId { get; set; }
public virtual SampleUnit Unit { get; set; }
[Required, ForeignKey("Sample")]
public int SensorSampleId { get; set; }
public virtual SensorSample Sample { get; set; }
[MaxLength(128)]
public string Value { get; set; }
}
因为 aSensorSample
可以有多种数据样本类型(即温度、压力等),所以INSERT
必须查询现有样本以与正确的SampleTime
. 这是使用以下代码完成的:
SensorSample sample = null;
foreach (var d in input)
{
SampleData data = new SampleData();
data.SampleDataTypeId = dataTypeId;
data.SampleUnitId = unitId;
data.Value = d.Value;
// check for existing sample for this sensor and timestamp
sample = SensorSamples.FirstOrDefault(s => s.SensorId == sensor.Id && s.SampleTime == d.Timestamp);
if (sample == null)
{
// sample doesn't exist, create a new one
sample = new SensorSample();
sample.SampleTime = d.Timestamp;
sample.SensorId = sensor.Id;
sensor.SensorSamples.Add(sample);
}
// add the data to the sample
sample.SampleData.Add(data);
}
我曾尝试通过分批(即一次 1000 条记录)来优化样本数据的插入。这确实有帮助,但即使SampleTime
字段上有索引,随着更多记录的添加,查找查询似乎需要更长的时间。
所以,我的问题是,如何改进将样本数据添加到数据库的设计和/或性能?是否有更好的数据库结构来处理一对多关系?如果我可以在性能上获得适当的偏移,我愿意在数据库设计上做出一些妥协,但我仍然需要能够处理与给定的SampleTime
.