简体   繁体   English

在 EF Core 6 中使用原始 SQL

[英]Usage of raw SQL in EF Core 6

I am currently trying to find different areas where Linq is not sufficient and FromSqlRaw or ExecuteSqlRaw have to be used.我目前正在尝试查找 Linq 不够用且必须使用FromSqlRawExecuteSqlRaw的不同区域。

Some examples I have found are我发现的一些例子是

However I am looking for more areas where Linq does not perform good enough and even queries that cannot be generated from Linq in EF Core when it comes to database access.但是,我正在寻找 Linq 性能不够好的更多领域,甚至在涉及数据库访问时无法从 EF Core 中的 Linq 生成查询。

My goal is to find poor performing Linq translations and examine the cause.我的目标是找到表现不佳的 Linq 翻译并检查原因。

This is a bit of a solution looking for a problem.这是一个寻找问题的解决方案。 Given an application and looking for inefficiencies that might benefit from a different approach is something I would start off using a profiler and observing the database access in as close to a production capacity as I am allowed to get.给定一个应用程序并寻找可能从不同方法中受益的低效率是我将开始使用分析器并观察数据库访问的情况,尽可能接近我允许的生产能力。

EF is like any tool, it can be leveraged to create works of art, and it can be abused and misused to create shanties. EF 就像任何工具一样,可以用来创造艺术作品,也可以被滥用和误用来创造棚屋。 Even when done correctly, optimizations like indexes are something that are tuned based on looking at real-world options.即使正确完成,像索引这样的优化也是根据现实世界的选项进行调整的。 There are many options that I would look at to address performance issues before considering direct to SQL. Typical culprits that can be easily identified via profiling:在考虑直接拨打 SQL 之前,我会考虑许多选项来解决性能问题。可以通过分析轻松识别的典型罪魁祸首:

  1. Lazy loading.延迟加载。 (Dozens to hundreds of queries following up a "main" query.) (在“主要”查询之后有数十到数百个查询。)
  2. Over-use of eager loading.过度使用预加载。 (Queries involving a heck of a lot of joins) (涉及大量连接的查询)
  3. Sloppy use of client-side evaluation.草率地使用客户端评估。 (Either enabling that feature in EF Core, or slapping a ToList somewhere when a query complains to "fix" it, AsSplitQuery can help here, Projection is a better solution in most cases) (无论是在 EF Core 中启用该功能,还是在查询抱怨“修复”它时在某处拍打ToList ,AsSplitQuery 都可以在这里提供帮助,Projection 在大多数情况下是更好的解决方案)
  4. Lack of pagination where more data is returned than necessary.缺少分页,返回的数据比必要的多。 (Similar to #3, having methods like "GetAll" and then applying filtering, pagination, etc.) (与 #3 类似,具有“GetAll”之类的方法,然后应用过滤、分页等。)
  5. Giving users too much flexibility in querying that they don't need 99% of the time, but in that 1% someone does try it, grinds the system to a halt.给用户太多的查询灵活性,他们在 99% 的时间里不需要,但在这 1% 的时间里有人确实尝试过,这会使系统陷入停顿。 (Giving users filters/sorts on ALL columns and performing things like string.Contains by default for text searches) (为用户提供对所有列的过滤器/排序并执行string.Contains类的操作。默认情况下包含文本搜索)
  6. Giving users access to expensive, but necessary queries in real-time.让用户实时访问昂贵但必要的查询。 (Big, justified queries, but being run against the production dataset and not "throttled" by something like a Queue to ensure too many of these monsters don't get run at once.) (大的、合理的查询,但是针对生产数据集运行,而不是被诸如队列之类的东西“限制”,以确保不会同时运行太多这些怪物。)

Those are some of the top culprits that come to mind around performance, and none of them resort to going to SQL. Batch processing in your list is certainly one case that I believe does deserve looking outside of Linq, and potentially outside of EF all-together.这些是围绕性能想到的一些头号罪魁祸首,并且没有一个诉诸于 SQL。我认为您列表中的批处理肯定是一个值得在 Linq 之外寻找的案例,并且可能在 EF all-一起。 Stored Procs I am mixed on.存储过程我混在上面。 If there is business logic that is shared between an EF-supported application and another existing system and I want to share that business logic as-is.如果在 EF 支持的应用程序和另一个现有系统之间共享业务逻辑,并且我想按原样共享该业务逻辑。 The trouble is that if I'm relying on the Sproc for business rules then there's little point to EF, and if I'm splitting business rules between C#/EF and Sprocs, then that's having to manage logic in two locations.问题在于,如果我依赖于业务规则的 Sproc,那么 EF 就没有什么意义,如果我在 C#/EF 和 Sprocs 之间拆分业务规则,那么就必须在两个位置管理逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM