所以我有以下方法,通过ID来检查帖子的存在:
public Task<bool> PostExistByIdAsync(string id)
{
return Task.FromResult(_dbContext.Posts.Any(a => a.Id.Equals(id)));
}
我看到的问题是它会编译为以下查询:
SELECT "p"."Id", "p"."AccountId", "p"."Post", "p"."Timestamp"
FROM "Posts" AS "p"
因此,与其使用WHERE
子句,不如遍历数百个结果以找到有效的实体。这是调用PostExistByIdAsync("109");
Context 'PostDataModel' started tracking '{Id: 1}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 2}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 3}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 4}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 5}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 6}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 7}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 8}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 9}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 10}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 11}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 12}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 13}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 14}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 15}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 16}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 17}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 18}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 19}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 20}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 21}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 22}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 23}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 24}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 25}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 26}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 27}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 28}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 29}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 30}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 31}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 32}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 33}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 34}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 35}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 36}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 37}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 38}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 39}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 40}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 41}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 42}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 43}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 44}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 45}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 46}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 47}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 48}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 49}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 50}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 51}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 52}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 53}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 54}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 55}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 56}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 57}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 58}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 59}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 60}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 61}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 62}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 63}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 64}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 65}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 66}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 67}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 68}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 69}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 70}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 71}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 72}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 73}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 74}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 75}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 76}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 77}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 78}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 79}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 80}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 81}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 82}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 83}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 84}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 85}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 86}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 87}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 88}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 89}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 90}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 91}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 92}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 93}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 94}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 95}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 96}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 97}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 98}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 99}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 100}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 101}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 102}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 103}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 104}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 105}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 106}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 107}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 108}' entity with key 'ClientDataStoreDbContext'.
Context 'PostDataModel' started tracking '{Id: 109}' entity with key 'ClientDataStoreDbContext'.
[......]
使用_dbContext.Posts.Where(...)
代替_dbContext.Posts.Any(...)
时也会发生同样的情况。
那是为什么?申请WHERE
绝对便宜
sql查询中的子句,而不是创建数百个
实体进行迭代。
为什么Entity Framework在此不关心性能 点?
Posts
中的 _dbContext
属性:
public DbSet<PostDataModel> Posts { get; set; }
PostDataModel:
public class PostDataModel
{
public string Id { get; set; }
public AccountDataModel Account { get; set; }
public string Post { get; set; }
public int Timestamp { get; set; }
}