EF.Core 3.x 生成 window 函数而不是 JOIN,导致 MySQL 语法错误

EF.Core 3.x generates window function instead of JOIN, leading to MySQL syntax error

运行 以下 EF Core 查询:

var groupData = await _dbContext.Groups.AsNoTracking()
    .Where(g => g.Id == groupId)
    .Select(g => new
    {
        /* ...some other fields are queried here... */
        ActiveLab = g.ActiveLabs.FirstOrDefault(al => al.LabId == labId)
    })
    .FirstAsync(cancellationToken);

导致此错误:

MySqlException: You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near
'(PARTITION BY `l`.`GroupId` ORDER BY `l`.`GroupId`, `l`.`LabId`) AS `row`'
at line 6

检查生成的 SQL 会发现 EF 出于某种原因插入了 PARTITION 指令:

SELECT `g`.`Name`, `t0`.`GroupId`, `t0`.`LabId`, `t0`.`StartTime`
FROM `Groups` AS `g`
LEFT JOIN (
    SELECT `t`.`GroupId`, `t`.`LabId`, `t`.`StartTime`
    FROM (
        SELECT `l`.`GroupId`, `l`.`LabId`, `l`.`StartTime`, ROW_NUMBER() OVER(PARTITION BY `l`.`GroupId` ORDER BY `l`.`GroupId`, `l`.`LabId`) AS `row`
        FROM `ActiveLabs` AS `l`
        WHERE `l`.`LabId` = @__labId_1
    ) AS `t`
    WHERE `t`.`row` <= 1
) AS `t0` ON `g`.`Id` = `t0`.`GroupId`
WHERE `g`.`Id` = @__groupId_0
LIMIT 1

我宁愿期待这样的查询:

SELECT `g`.`Name`, `l`.`GroupId`, `l`.`LabId`, `l`.`StartTime`
FROM `Groups` AS `g`
LEFT JOIN `ActiveLabs` AS `l`
ON `l`.`GroupId` = `g`.`Id`
WHERE `l`.`LabId` = @__labId_1 AND `g`.`Id` = @__groupId_0
LIMIT 1

为什么 EF 生成如此复杂的查询,而 ActiveLabs 上的简单 JOIN 就足够了?

我正在使用 EF Core 3.1.2、Pomelo MySQL 3.1.1 和 MySQL 5.7.14 进行测试。


我的数据库如下所示:我有两个表 Groups

+----+--------+
| Id |  Name  |
+----+--------+
|  1 | Group1 |
|  2 | Group2 |
+----+--------+

ActiveLabs

+---------+-------+----------------------------+
| GroupId | LabId |         StartTime          |
+---------+-------+----------------------------+
|       1 |     1 | 2020-03-01 00:00:00.000000 |
|       2 |     1 | 2020-03-01 00:00:00.000000 |
|       1 |     2 | 2020-03-08 00:00:00.000000 |
+---------+-------+----------------------------+

后者代表多对多关系,它跟踪哪个实验室对哪个组处于活动状态。因此,Group 对象具有导航 属性 ActiveLabs,它指向该组的活动实验室。 class/table 结构和外键是正确的并且适用于所有用例。


编辑: 看起来 MySQL 5.7.14 根本不支持 PARTITION(Pomelo 的 GitHub 存储库上的 related issue)。升级到 MySQL 8.0 摆脱了错误消息,查询现在有效;但是,我仍然不明白为什么 EF 生成 PARTITION(window 函数)语句

实际上 EF Core 根据您的 LINQ 查询生成了正确的 SQL,而且它做得很好。

为了更接近预期的SQL,您可以按以下方式重写此查询:

var query = 
    from g in _dbContext.Groups
    from al in g.ActiveLabs
    where g.Id == groupId && al.LabId == labId
    select new 
    {
        /* ...some other fields are queried here... */
        ActiveLab = al
    };

var groupDate = await query.FirstAsync(cancellationToken);

稍微解释一下它是如何工作的

它可以不是直接的 EF Core 翻译技术,但它可以非常相似

首先,Translator 会生成您定义的所有需要​​的联接 - 将其称为主查询。然后,在投影生成(Select)期间,Translator 发现您向一些相关实体请求了FirstOrDefault。但是主查询已经定义了,我们可以先对 select 做些什么 child 并且不损害主查询结果 - 对有限的记录集进行 OUTER APPLY JOIN。但是OUTER APPLY JOIN 不是那么有效,所以我们可以尝试将OUTER APPLY JOIN 转换为LEFT JOIN。对于这种特定情况,可以使用 Window 函数轻松完成 - 瞧。