简体   繁体   English

Entity Framework Core:如何将 2 个 COUNT 查询合并为一个

[英]Entity Framework Core: How to combine 2 COUNT queries into one

I'm working on improving back-end performance with Entity Framework Core.我正在努力使用 Entity Framework Core 提高后端性能。 This is a small and simple example to illustrate what I want to return:这是一个小而简单的示例,用于说明我想要返回的内容:

var filteredMovies = _dataService
    .Repository
    .Where(movie => movie.Genre == 'Drama')
    .Where(...many other conditions, joins, etc...);

var counts1 = filteredMovies.Count(movie => movie.director == 'Spielberg');
var counts2 = filteredMovies.Count(movie => movie.director == 'Nolan');

return new Summary 
{
    SpielbergCount = counts1,
    NolanCount = counts2,
};

This will generate 2 SQL queries, that each go through all the WHERE, JOIN steps.这将生成 2 个 SQL 查询,每个查询都经过所有 WHERE、JOIN 步骤。 What I've seen, in SQL, is that I could write it this way to only execute 1 query:我在 SQL 中看到的是,我可以通过这种方式编写它以仅执行 1 个查询:

SELECT
    SUM(CASE WHEN Director = 'Spielberg' THEN 1 ELSE 0 END) SpielbergCount,
    SUM(CASE WHEN Director = 'Nolan' THEN 1 ELSE 0 END) NolanCount
FROM Movies
JOIN ....
WHERE ....

2 questions: 2个问题:

  1. How can I translate this last query into EntityFramework, to prevent executing 2 different SQL queries?如何将最后一个查询转换为 EntityFramework,以防止执行 2 个不同的 SQL 查询?
  2. Is it going to improve performance?它会提高性能吗? (ie: This is a small example, but I have many queries to improve, some of them very big, with most of the query being the same, except for one condition, like the Director in this example) Or is it not actually improving anything? (即:这是一个小例子,但我有很多查询需要改进,其中一些非常大,大多数查询都相同,除了一个条件,如本例中的 Director)或者实际上并没有改进任何事物?

Thank you谢谢

In order to do multiple top level aggregates in a select you have to do a group by on a constant value.为了在一个选择中进行多个顶级聚合,您必须对一个常量值进行分组。

var counts = _dataService
    .Repository
    .Where(movie => movie.Genre == 'Drama')
    .Where(...many other conditions, joins, etc...)
    .GroupBy(x => 1)
    .Select(grp => new Summary 
    {
        SpielbergCount = grp.Count(movie => movie.director == 'Spielberg'),
        NolanCount = grp.Count(movie => movie.director == 'Nolan'),
    });
return _dataService
  .Repository
  .Where(movie => movie.Genre == 'Drama')
  .Where(movie => movie.director == 'Spielberg' || movie.director == 'Nolan')
  .Where(...many other conditions, joins, etc...)
  .GroupBy(movie => movie.director)
  .Select(g => new Summary 
  {
      SpielbergCount = g.Count(x => x.director == 'Spielberg'),
      NolanCount = g.Count(x => x.director == 'Nolan'),
  });

1. How can I translate this last query into EntityFramework, to prevent executing 2 different SQL queries? 1. 如何将最后一个查询转换为 EntityFramework,以防止执行 2 个不同的 SQL 查询?

Try using Group By and Count with Filter.尝试使用 Group By 和 Count with Filter。

2. Is it going to improve performance? 2.它会提高性能吗? Or is it not actually improving anything?或者它实际上并没有改善任何东西?

Few tips to improve performance提高性能的几个技巧

  • Avoid fetching all the fields if not required如果不需要,避免获取所有字段
  • Retrieve only required number of records仅检索所需数量的记录
  • If you want to moves processing from the SQL server to your application server then try using AsEnumerable.如果您想将处理从 SQL 服务器移动到您的应用程序服务器,请尝试使用 AsEnumerable。

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

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