从 SQL 查询中排除子类型

发布于 2025-01-11 11:37:15 字数 1439 浏览 0 评论 0原文

我有下面的 Linq 查询:

var result = (from booking in _context.Booking.AsNoTracking()
  join vendor in _context.Vendor.AsNoTracking() on booking.VendorId equals vendor.Id
  where
    booking.ClientId == userId 
  orderby booking.Id descending
  select vendor.TypeId).FirstOrDefault();

供应商可以分为移动设备或车库。 但对于我的用例,我不关心子类型。

生成的 SQL 包含带有 UNION 的子类型表。

exec sp_executesql N'SELECT TOP (1) 
    [Project3].[TypeId] AS [TypeId]
    FROM ( SELECT 
        [Extent1].[Id] AS [Id], 
        [Join1].[TypeId] AS [TypeId]
        FROM  [dbo].[Booking] AS [Extent1]
        INNER JOIN  (SELECT [UnionAll1].[VendorId] AS [VendorId], [Extent4].[TypeId] AS [TypeId]
            FROM   (SELECT 
                [Extent2].[VendorId] AS [VendorId]
                FROM [dbo].[Garage] AS [Extent2]
            UNION ALL
                SELECT 
                [Extent3].[VendorId] AS [VendorId]
                FROM [dbo].[Mobile] AS [Extent3]) AS [UnionAll1]
            INNER JOIN [dbo].[Vendor] AS [Extent4] ON [UnionAll1].[VendorId] = [Extent4].[Id] ) AS [Join1] ON [Extent1].[VendorId] = [Join1].[VendorId]
        WHERE [Extent1].[ClientId] = @p__linq__0
    )  AS [Project3]
    ORDER BY [Project3].[Id] DESC',N'@p__linq__0 uniqueidentifier',@p__linq__0='6AE9C275-7944-47B0-9B03-7B10EC88C98C'

这是浪费。 如何排除子类型不包含在 SQL 命令中?

奖金问题。 我可以让 TOP 1 在嵌套 SQL 中运行吗? 再次。这是浪费。它首先获得完整结果,然后获得 TOP 1。

I have the Linq query below:

var result = (from booking in _context.Booking.AsNoTracking()
  join vendor in _context.Vendor.AsNoTracking() on booking.VendorId equals vendor.Id
  where
    booking.ClientId == userId 
  orderby booking.Id descending
  select vendor.TypeId).FirstOrDefault();

A Vendor can be subtyped into a Mobile or a Garage.
But for my use case, I don't care about the subtype.

The SQL that is generated includes the subtype tables with a UNION.

exec sp_executesql N'SELECT TOP (1) 
    [Project3].[TypeId] AS [TypeId]
    FROM ( SELECT 
        [Extent1].[Id] AS [Id], 
        [Join1].[TypeId] AS [TypeId]
        FROM  [dbo].[Booking] AS [Extent1]
        INNER JOIN  (SELECT [UnionAll1].[VendorId] AS [VendorId], [Extent4].[TypeId] AS [TypeId]
            FROM   (SELECT 
                [Extent2].[VendorId] AS [VendorId]
                FROM [dbo].[Garage] AS [Extent2]
            UNION ALL
                SELECT 
                [Extent3].[VendorId] AS [VendorId]
                FROM [dbo].[Mobile] AS [Extent3]) AS [UnionAll1]
            INNER JOIN [dbo].[Vendor] AS [Extent4] ON [UnionAll1].[VendorId] = [Extent4].[Id] ) AS [Join1] ON [Extent1].[VendorId] = [Join1].[VendorId]
        WHERE [Extent1].[ClientId] = @p__linq__0
    )  AS [Project3]
    ORDER BY [Project3].[Id] DESC',N'@p__linq__0 uniqueidentifier',@p__linq__0='6AE9C275-7944-47B0-9B03-7B10EC88C98C'

This is wasteful.
How can I exclude the SubTypes from being included in the SQL command?

Bonus question.
Can I make the TOP 1 run in the nested SQL?
Again. This is wasteful. It gets a full result first, and then TOP 1s it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文