ΠΠ±Π·ΠΎΡ Π²ΠΎΠΏΡΠΎΡΠΎΠ² ΠΏΠΎ ΡΠ·ΡΠΊΠ° ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΈΠΈΡΠΎΠ²Π°Π½ΠΈΡ C# ΠΈ ΡΠ΅Ρ Π½ΠΎΠ»ΠΎΠ³ΠΈΠΈ NET
β¬ ΠΠ΅ΡΠ½ΡΡΡΡΡ ΠΊ Π³Π»Π°Π²Π½ΠΎΠΉ
ΠΠ°ΠΆΠ΅ ΠΈΠ΄Π΅Π°Π»ΡΠ½ΡΠΉ ΠΊΠΎΠ΄ Π½Π° C# Π½Π΅ ΡΠΏΠ°ΡΡΡ, Π΅ΡΠ»ΠΈ SQL ΡΠ°Π±ΠΎΡΠ°Π΅Ρ ΠΌΠ΅Π΄Π»Π΅Π½Π½ΠΎ:
var sql = context.Users.Where(u => u.IsActive).ToQueryString();
Console.WriteLine(sql);
SET STATISTICS PROFILE ON;
ΠΈΠ»ΠΈ ΠΏΡΠΎΡΠΌΠΎΡΡ Execution Plan
.Π§ΡΠΎ ΠΈΡΠΊΠ°ΡΡ:
INCLUDE
ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ Π΄Π»Ρ covering index.Stopwatch
Π΄Π»Ρ ΠΈΠ·ΠΌΠ΅ΡΠ΅Π½ΠΈΡ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ Π·Π°ΠΏΡΠΎΡΠ°.var sw = Stopwatch.StartNew();
var data = await context.Users.AsNoTracking().ToListAsync();
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
Count()
, Take(10)
Π΄Π»Ρ ΠΏΡΠΎΠ²Π΅ΡΠΊΠΈ, ΡΡΠΎΠ±Ρ Π½Π΅ ΡΡΠ½ΡΡΡ ΠΌΠΈΠ»Π»ΠΈΠΎΠ½Ρ ΡΡΡΠΎΠΊ Π² ΠΏΠ°ΠΌΡΡΡ.ToList()
Π±Π΅Π· ΡΠΈΠ»ΡΡΡΠ°ΡΠΈΠΈ β memory spike.ToList()
Π² ΠΏΠ°ΠΌΡΡΠΈ.// ΠΠ»ΠΎΡ
ΠΎ
var data = context.Users.ToList().Where(u => u.IsActive);
// Π₯ΠΎΡΠΎΡΠΎ
var data = context.Users.Where(u => u.IsActive).ToList();
var data = context.Users
.Where(u => u.IsActive)
.Select(u => new { u.Id, u.Name })
.ToList();
ToList
, First
, Count
).var users = await context.Users.Where(u => u.IsActive).ToListAsync();
var page = context.Users
.OrderBy(u => u.Id)
.Skip((page-1)*pageSize)
.Take(pageSize)
.ToList();
OrderBy
β Π½Π΅ΠΏΡΠ΅Π΄ΡΠΊΠ°Π·ΡΠ΅ΠΌΡΠΉ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ.β¬ ΠΠ΅ΡΠ½ΡΡΡΡΡ ΠΊ Π³Π»Π°Π²Π½ΠΎΠΉ
β¨Dvurechenskyβ¨