[英]Entity Framework One to many relationship
My EF query takes around 3 seconds to fetch 10 players because it fetches all 500k+ rows of the other table, instead of the few I need. 我的EF查询大约花了3秒钟来获取10个玩家,因为它获取了另一个表的所有500k +行,而不是我需要的几个。
This is the PlayerEntity: 这是PlayerEntity:
namespace RocketLeagueStats.Database.Entities
{
[Table("players", Schema = "public")]
public class PlayerEntity
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("unique_id")]
public string UniqueId { get; set; }
[Column("display_name")]
public string DiplayName { get; set; }
[Column("platform_id")]
[JsonIgnore]
public int PlatformId { get; set; }
[ForeignKey("PlatformId")]
public PlatformEntity Platform { get; set; }
[Column("avatar")]
public string Avatar { get; set; }
public PlayerStatsEntity Stats { get; set; }
public List<PlayerRankedEntity> Ranks { get; set; }
[Column("last_requested")]
public DateTime LastRequested { get; set; }
[Column("created_at")]
public DateTime CreatedAt { get; set; }
[Column("updated_at")]
public DateTime UpdatedAt { get; set; }
}
}
This is the PlayerRankedEntity: 这是PlayerRankedEntity:
namespace RocketLeagueStats.Database.Entities
{
[Table("player_ranked", Schema = "public")]
public class PlayerRankedEntity
{
[ForeignKey("Player")]
[Column("player_id")]
[JsonIgnore]
public int PlayerId { get; set; }
[Column("season_id")]
[JsonIgnore]
public int SeasonId { get; set; }
[Column("playlist_id")]
[JsonIgnore]
public int PlaylistId { get; set; }
[Column("matches_played")]
public int MatchesPlayed { get; set; }
[Column("rank_points")]
public int RankPoints { get; set; }
[Column("tier")]
public int Tier { get; set; }
[Column("division")]
public int Division { get; set; }
public PlayerEntity Player { get; set; }
}
}
This is the PlayerStatsEntity: 这是PlayerStatsEntity:
namespace RocketLeagueStats.Database.Entities
{
[Table("player_stats", Schema = "public")]
public class PlayerStatsEntity
{
[Key, ForeignKey("Player")]
[Column("player_id")]
[JsonIgnore]
public int PlayerId { get; set; }
[Column("wins")]
public int Wins { get; set; }
[Column("goals")]
public int Goals { get; set; }
[Column("mvps")]
public int Mvps { get; set; }
[Column("saves")]
public int Saves { get; set; }
[Column("shots")]
public int Shots { get; set; }
[Column("assists")]
public int Assists { get; set; }
public PlayerEntity Player { get; set; }
}
}
This is my DatabaseContext.OnModelCreating method: 这是我的DatabaseContext.OnModelCreating方法:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PlayerRankedEntity>()
.HasKey(k => new { k.PlayerId, k.SeasonId, k.PlaylistId });
}
This is the code that takes over 3 seconds to complete (If I remove '.Include(x => x.Ranks)' it just takes a few ms): 这是需要3秒才能完成的代码(如果我删除'.Include(x => x.Ranks)'只需要几毫秒):
using (var database = new DatabaseContext())
{
var serviceProvider = database.GetInfrastructure();
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
loggerFactory.AddNLog();
var stopwatch = Stopwatch.StartNew();
var players = database.Players
.Include(x => x.Ranks)
.Take(10)
.ToArray();
Console.WriteLine($"Took {stopwatch.ElapsedMilliseconds}ms to fetch {players.Length} players");
}
This is the output, notice that it generates two queries instead of one: 这是输出,请注意它生成两个查询而不是一个:
2016-10-31 22:50:18.6416 INFO Executed DbCommand (8ms) [Parameters=[@__p_0='?'], CommandType='Text', CommandTimeout='30']
SELECT "x"."id", "x"."avatar", "x"."created_at", "x"."display_name", "x"."last_requested", "x"."platform_id", "x"."unique_id", "x"."updated_at"
FROM "public"."players" AS "x"
ORDER BY "x"."id"
LIMIT @__p_0
2016-10-31 22:50:18.7128 INFO Executed DbCommand (0ms) [Parameters=[@__p_0='?'], CommandType='Text', CommandTimeout='30']
SELECT "p"."player_id", "p"."season_id", "p"."playlist_id", "p"."division", "p"."matches_played", "p"."rank_points", "p"."tier"
FROM "public"."player_ranked" AS "p"
WHERE EXISTS (
SELECT 1
FROM "public"."players" AS "x"
WHERE "p"."player_id" = "x"."id"
LIMIT @__p_0)
ORDER BY "p"."player_id"
Took 3991ms to fetch 10 players
I think that I messed up on the relations somewhere, causing it to select all rows. 我认为我搞砸了某处的关系,导致它选择所有行。 But I don't know what I messed up.
但我不知道我搞砸了什么。
How can I fix this, and are there any other problems with my attributes? 我该如何解决这个问题,我的属性还有其他问题吗?
I am using Microsoft.EntityFrameworkCore v1.0.1. 我正在使用Microsoft.EntityFrameworkCore v1.0.1。
Edit: If I use .OrderBy(x => x.CreatedAt)
or .Where(x => x.DiplayName.Contains("mike"))
in the query, it goes a lot faster. 编辑:如果我在查询中使用
.OrderBy(x => x.CreatedAt)
或.Where(x => x.DiplayName.Contains("mike"))
,它会更快。
Generated query: 生成的查询:
2016-11-01 00:14:15.9638 INFO Executed DbCommand (24ms) [Parameters=[@__p_0='?'], CommandType='Text', CommandTimeout='30']
SELECT "x"."id", "x"."avatar", "x"."created_at", "x"."display_name", "x"."last_requested", "x"."platform_id", "x"."unique_id", "x"."updated_at"
FROM "public"."players" AS "x"
ORDER BY "x"."created_at", "x"."id"
LIMIT @__p_0
2016-11-01 00:14:16.0972 INFO Executed DbCommand (44ms) [Parameters=[@__p_0='?'], CommandType='Text', CommandTimeout='30']
SELECT "p"."player_id", "p"."season_id", "p"."playlist_id", "p"."division", "p"."matches_played", "p"."rank_points", "p"."tier"
FROM "public"."player_ranked" AS "p"
INNER JOIN (
SELECT DISTINCT "x"."created_at", "x"."id"
FROM "public"."players" AS "x"
ORDER BY "x"."created_at", "x"."id"
LIMIT @__p_0
) AS "x0" ON "p"."player_id" = "x0"."id"
ORDER BY "x0"."created_at", "x0"."id"
Took 314ms to fetch 10 players
EF Core currently is a nightmare. EF Core目前是一场噩梦。
You can try the following workaround (but if you ask me, better switch back to EF6). 您可以尝试以下解决方法(但如果您问我,最好切换回EF6)。
Instead of: 代替:
var players = database.Players
.Include(x => x.Ranks)
.Take(10)
.ToArray();
use: 使用:
var players = database.Players
.Take(10)
.ToArray();
var playerIds = players.Select(p => p.Id);
database.PlayerRanks.Where(r => playerIds.Contains(r.PlayerId)).Load();
which should produce the same effect as Include
. 它应该产生与
Include
相同的效果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.