Tối ưu EF Core query xoay quanh giảm overhead tracking, giảm dữ liệu trả về và tránh N+1.
- Dùng
.AsNoTracking()cho read-only queries để tắt change tracking - Dùng
.Select()projection thay vì load toàn bộ entity - Batch updates với
ExecuteUpdate()vàExecuteDelete()thay vì load-modify-save - Dùng
.Include()đúng chỗ để tránh N+1 - Monitor SQL thực tế qua logging để phát hiện query xấu
- Tạo index trên các cột được filter/sort thường xuyên
EF Core optimization centers on reducing tracking overhead, returned data, and N+1 patterns.
- Use
.AsNoTracking()for read-only queries to disable change tracking - Use
.Select()projections instead of loading entire entities - Batch updates with
ExecuteUpdate()andExecuteDelete()instead of load-modify-save - Use
.Include()appropriately to avoid N+1 - Monitor generated SQL via logging to spot bad queries
- Add database indexes on frequently filtered or sorted columns