Reputation: 75
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.
This is the 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:
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:
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:
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):
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.
Edit: If I use .OrderBy(x => x.CreatedAt)
or .Where(x => x.DiplayName.Contains("mike"))
in the query, it goes a lot faster.
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
Upvotes: 4
Views: 291
Reputation: 205539
EF Core currently is a nightmare.
You can try the following workaround (but if you ask me, better switch back to 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
.
Upvotes: 1