我在高数据加载方案中遇到Servicestack和OrmLite的一些问题。
具体而言,
我有一份1000 000多个实体的清单
2.如果记录不存在,我想将它们插入Db(使用Sql Server)
因此,
public class Entity
{
[Autoincrement]
public int Id {get;set;}
public string Name {get;set;}
public string Address {get;set;}
}
现在是导入逻辑,
List<Entity> entities = oneMillionEntities.ToList();
foreach (var entity in entities)
{
if (!db.Exists<Entity>(ar => ar.Address == entity.Address))
{
db.Save(entity);
}
}
问题是db经常忙于保存操作,因此db.Exists并不总是产生正确的结果。处理这些场景的最佳方法是什么?
答案 0 :(得分:1)
尝试
// Prepare SqlExpression
var ev = Db.From<Entity>().Select(p => p.Address).GroupBy(p => p.Address);
// Execute SqlExpression and transform result to HashSet
var dbAddresses = Db.SqlList(ev).ToHashSet();
// Filter local entities and get only local entities with different addresses
var filteredEntities = oneMillionEntities.Where(p =>
!dbAddresses.Contains(p.Address));
// Bulk insert
db.InsertAll(filteredEntities.ToList());