Skip to content

EntityFramework优化技巧 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
WeihanLi opened this issue Jun 25, 2018 · 1 comment
Open

EntityFramework优化技巧 #15

WeihanLi opened this issue Jun 25, 2018 · 1 comment

Comments

@WeihanLi
Copy link
Owner

说EF性能不好的人,你真的会用EF吗,来看EF优化技巧

Reference

@WeihanLi
Copy link
Owner Author

WeihanLi commented Jul 19, 2018

EntityFramework优化技巧

本质上都是要优化生成的 sql,应注意监控生成的 sql 语句

  1. 只读查询时加 AsNoTracking(),不追踪查询出的数据状态
  2. 查询时只查自己需要的字段,不要全部字段都查,尤其是在字段比较多的时候
  3. 查询的时候 数据库里的字段是 varchar时,可以在查询的时候使用 AsNonUnicode
  4. 注意 IQueryable 的延迟查询,防止造成同一条件的查询执行多次以及假分页查询
  5. 批量处理只 SaveChanges 一次,减少数据库操作的次数
  6. EF预热
  7. 利用外键的导航属性Include减少sql查询次数(不推荐使用外键)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant