В моей БД SQL Server у меня есть таблица данных IOT (около 1 000 000 записей). Когда приложение получает входящие показания, я хочу убедиться, что в БД еще нет показаний для этого устройства с такой же отметкой времени. Какой самый быстрый способ проверить записи с подходящими свойствами?
Модель
public class Reading
{
public int Id { get; set; }
public double Measurement { get; set; }
public int DeviceId { get; set; }
public Device Device { get; set; }
public DateTime Timestamp { get; set; }
}
Метод AddReading
public class ReadingRepository
{
private readonly DataContext _context;
public ReadingRepository(DataContext context)
{
_context = context;
}
public void AddReading(Reading reading)
{
// my proposed method... is there a better way?
if (!_context.Readings.Any(r =>
r.DeviceId == reading.DeviceId,
r.Timestamp == reading.Timestamp))
_context.Readings.Add(reading);
}
}