aholmes
aholmes

Reputation: 458

Limit results from multiple individual tables in a single LINQ-to-Entities query. Resultant T-SQL is wrong

I need to query multiple tables with one query, and I need to limit the results from each table individually.

An example ...

I have a ContentItem, Retailer, and Product table.

ContentItem has a Type (int) field that corresponds to an enum of content types like "Retailer" and "Product." I am filtering ContentItem using this field for each sub-subquery.

ContentItem has an Id (pkey) field.

Retailer and Product have an Id (pkey) field. Id is also an FK to ContentItem.Id.

I can select from all three tables with a LEFT JOIN query. From there, I can then limit the total number of rows returned, let's say 6 rows total.

What I want to do is limit the number of rows returned from Retailer and Product individually. This way, I will have 12 rows (max) total: 6 from Retailer, and 6 from Product.

I can already accomplish this with SQL, but I am having a difficult time getting LINQ-to-Entities to "do the right thing."


Here's my SQL

SELECT * From
    (
            (SELECT * FROM (SELECT * FROM [dbo].[ContentItem] WHERE Type = 0 ORDER BY ContentItem.mtime OFFSET 0 ROWS FETCH NEXT 6 ROWS ONLY) Retailers)
        UNION ALL
            (SELECT * FROM (SELECT * FROM [dbo].[ContentItem] WHERE Type = 1 ORDER BY ContentItem.mtime OFFSET 0 ROWS FETCH NEXT 6 ROWS ONLY) Brands)
        UNION ALL
            (SELECT * FROM (SELECT * FROM [dbo].[ContentItem] WHERE Type = 2 ORDER BY ContentItem.mtime OFFSET 0 ROWS FETCH NEXT 6 ROWS ONLY) Products)
        UNION ALL
            (SELECT * FROM (SELECT * FROM [dbo].[ContentItem] WHERE Type = 3 ORDER BY ContentItem.mtime OFFSET 0 ROWS FETCH NEXT 6 ROWS ONLY) Certifications)
        UNION ALL
            (SELECT * FROM (SELECT * FROM [dbo].[ContentItem] WHERE Type = 4 ORDER BY ContentItem.mtime OFFSET 0 ROWS FETCH NEXT 6 ROWS ONLY) Claims)
    ) as ContentItem

    LEFT JOIN [dbo].[Retailer] ON (Retailer.Id = ContentItem.Id)
    LEFT JOIN [dbo].[Brand] ON (Brand.Id = ContentItem.Id)
    LEFT JOIN [dbo].[Product] ON (Product.Id = ContentItem.Id)
    LEFT JOIN [dbo].[Certification] ON (Certification.Id = ContentItem.Id)
    LEFT JOIN [dbo].[Claim] ON (Claim.Id = ContentItem.Id);

Here's one of my many iterations of LINQ queries (which is not returning the desired result).

var queryRetailers = contentItemModel
    .Where(contentItem => contentItem.Type == ContentTypeEnum.Retailer)
    .OrderByDescending(o => o.mtime).Skip(skip).Take(take).Select(o => new { Id = o.Id });
var queryBrands = contentItemModel
    .Where(contentItem => contentItem.Type == ContentTypeEnum.Brand)
    .OrderByDescending(o => o.mtime).Skip(skip).Take(take).Select(o => new { Id = o.Id });
var queryProducts = contentItemModel
    .Where(contentItem => contentItem.Type == ContentTypeEnum.Product)
    .OrderByDescending(o => o.mtime).Skip(skip).Take(take).Select(o => new { Id = o.Id });
var queryCertifications = contentItemModel
    .Where(contentItem => contentItem.Type == ContentTypeEnum.Certification)
    .OrderByDescending(o => o.mtime).Skip(skip).Take(take).Select(o => new { Id = o.Id });
var queryClaims = contentItemModel
    .Where(contentItem => contentItem.Type == ContentTypeEnum.Claim)
    .OrderByDescending(o => o.mtime).Skip(skip).Take(take).Select(o => new { Id = o.Id });

var query = from contentItem in
    queryRetailers
    .Concat(queryBrands)
    .Concat(queryProducts)
    .Concat(queryCertifications)
    .Concat(queryClaims)

join item in context.Retailer on contentItem.Id equals item.Id into retailerGroup
    from retailer in retailerGroup.DefaultIfEmpty(null)

join item in context.Brand on contentItem.Id equals item.Id into brandGroup
    from brand in brandGroup.DefaultIfEmpty(null)

join item in context.Product on contentItem.Id equals item.Id into productGroup
    from product in productGroup.DefaultIfEmpty(null)

join item in context.Certification on contentItem.Id equals item.Id into certificationGroup
    from certification in certificationGroup.DefaultIfEmpty(null)

join item in context.Claim on contentItem.Id equals item.Id into claimGroup
    from claim in claimGroup.DefaultIfEmpty(null)

select new
{
    contentItem,
    retailer,
    brand,
    product,
    certification,
    claim
};

var results = query.ToList();

This query returns SQL that essentially "nests" my UNION ALL statements, and the server returns all rows from the database.

SELECT 
    [Distinct4].[C1] AS [C1], 
    [Distinct4].[C2] AS [C2], 
    [Extent6].[Id] AS [Id], 
    [Extent6].[RowVersion] AS [RowVersion], 
    [Extent6].[ctime] AS [ctime], 
    [Extent6].[mtime] AS [mtime], 
    [Extent7].[Id] AS [Id1], 
    [Extent7].[Recommended] AS [Recommended], 
    [Extent7].[RowVersion] AS [RowVersion1], 
    [Extent7].[ctime] AS [ctime1], 
    [Extent7].[mtime] AS [mtime1], 
    [Extent8].[Id] AS [Id2], 
    [Extent8].[OverrideGrade] AS [OverrideGrade], 
    [Extent8].[PlantBased] AS [PlantBased], 
    [Extent8].[Recommended] AS [Recommended1], 
    [Extent8].[RowVersion] AS [RowVersion2], 
    [Extent8].[ctime] AS [ctime2], 
    [Extent8].[mtime] AS [mtime2], 
    [Extent8].[Brand_Id] AS [Brand_Id], 
    [Extent8].[Grade_Name] AS [Grade_Name], 
    [Extent8].[Grade_Value] AS [Grade_Value], 
    [Extent9].[Id] AS [Id3], 
    [Extent9].[RowVersion] AS [RowVersion3], 
    [Extent9].[ctime] AS [ctime3], 
    [Extent9].[mtime] AS [mtime3], 
    [Extent9].[Grade_Name] AS [Grade_Name1], 
    [Extent9].[Grade_Value] AS [Grade_Value1], 
    [Extent10].[Id] AS [Id4], 
    [Extent10].[RowVersion] AS [RowVersion4], 
    [Extent10].[ctime] AS [ctime4], 
    [Extent10].[mtime] AS [mtime4], 
    [Extent10].[Grade_Name] AS [Grade_Name2], 
    [Extent10].[Grade_Value] AS [Grade_Value2]
    FROM       (SELECT DISTINCT 
        [UnionAll4].[C1] AS [C1], 
        [UnionAll4].[C2] AS [C2]
        FROM  (SELECT 
            [Distinct3].[C1] AS [C1], 
            [Distinct3].[C2] AS [C2]
            FROM ( SELECT DISTINCT 
                [UnionAll3].[C1] AS [C1], 
                [UnionAll3].[C2] AS [C2]
                FROM  (SELECT 
                    [Distinct2].[C1] AS [C1], 
                    [Distinct2].[C2] AS [C2]
                    FROM ( SELECT DISTINCT 
                        [UnionAll2].[C1] AS [C1], 
                        [UnionAll2].[C2] AS [C2]
                        FROM  (SELECT 
                            [Distinct1].[C1] AS [C1], 
                            [Distinct1].[C2] AS [C2]
                            FROM ( SELECT DISTINCT 
                                [UnionAll1].[C1] AS [C1], 
                                [UnionAll1].[Id] AS [C2]
                                FROM  (SELECT TOP (1000) 
                                    [Project1].[C1] AS [C1], 
                                    [Project1].[Id] AS [Id]
                                    FROM ( SELECT [Project1].[Id] AS [Id], [Project1].[mtime] AS [mtime], [Project1].[C1] AS [C1], row_number() OVER (ORDER BY [Project1].[mtime] DESC) AS [row_number]
                                        FROM ( SELECT 
                                            [Extent1].[Id] AS [Id], 
                                            [Extent1].[mtime] AS [mtime], 
                                            1 AS [C1]
                                            FROM [dbo].[ContentItem] AS [Extent1]
                                            WHERE 0 =  CAST( [Extent1].[Type] AS int)
                                        )  AS [Project1]
                                    )  AS [Project1]
                                    WHERE [Project1].[row_number] > 0
                                    ORDER BY [Project1].[mtime] DESC
                                UNION ALL
                                    SELECT TOP (1000) 
                                    [Project3].[C1] AS [C1], 
                                    [Project3].[Id] AS [Id]
                                    FROM ( SELECT [Project3].[Id] AS [Id], [Project3].[mtime] AS [mtime], [Project3].[C1] AS [C1], row_number() OVER (ORDER BY [Project3].[mtime] DESC) AS [row_number]
                                        FROM ( SELECT 
                                            [Extent2].[Id] AS [Id], 
                                            [Extent2].[mtime] AS [mtime], 
                                            1 AS [C1]
                                            FROM [dbo].[ContentItem] AS [Extent2]
                                            WHERE 1 =  CAST( [Extent2].[Type] AS int)
                                        )  AS [Project3]
                                    )  AS [Project3]
                                    WHERE [Project3].[row_number] > 0
                                    ORDER BY [Project3].[mtime] DESC) AS [UnionAll1]
                            )  AS [Distinct1]
                        UNION ALL
                            SELECT TOP (1000) 
                            [Project7].[C1] AS [C1], 
                            [Project7].[Id] AS [Id]
                            FROM ( SELECT [Project7].[Id] AS [Id], [Project7].[mtime] AS [mtime], [Project7].[C1] AS [C1], row_number() OVER (ORDER BY [Project7].[mtime] DESC) AS [row_number]
                                FROM ( SELECT 
                                    [Extent3].[Id] AS [Id], 
                                    [Extent3].[mtime] AS [mtime], 
                                    1 AS [C1]
                                    FROM [dbo].[ContentItem] AS [Extent3]
                                    WHERE 2 =  CAST( [Extent3].[Type] AS int)
                                )  AS [Project7]
                            )  AS [Project7]
                            WHERE [Project7].[row_number] > 0
                            ORDER BY [Project7].[mtime] DESC) AS [UnionAll2]
                    )  AS [Distinct2]
                UNION ALL
                    SELECT TOP (1000) 
                    [Project11].[C1] AS [C1], 
                    [Project11].[Id] AS [Id]
                    FROM ( SELECT [Project11].[Id] AS [Id], [Project11].[mtime] AS [mtime], [Project11].[C1] AS [C1], row_number() OVER (ORDER BY [Project11].[mtime] DESC) AS [row_number]
                        FROM ( SELECT 
                            [Extent4].[Id] AS [Id], 
                            [Extent4].[mtime] AS [mtime], 
                            1 AS [C1]
                            FROM [dbo].[ContentItem] AS [Extent4]
                            WHERE 3 =  CAST( [Extent4].[Type] AS int)
                        )  AS [Project11]
                    )  AS [Project11]
                    WHERE [Project11].[row_number] > 0
                    ORDER BY [Project11].[mtime] DESC) AS [UnionAll3]
            )  AS [Distinct3]
        UNION ALL
            SELECT TOP (1000) 
            [Project15].[C1] AS [C1], 
            [Project15].[Id] AS [Id]
            FROM ( SELECT [Project15].[Id] AS [Id], [Project15].[mtime] AS [mtime], [Project15].[C1] AS [C1], row_number() OVER (ORDER BY [Project15].[mtime] DESC) AS [row_number]
                FROM ( SELECT 
                    [Extent5].[Id] AS [Id], 
                    [Extent5].[mtime] AS [mtime], 
                    1 AS [C1]
                    FROM [dbo].[ContentItem] AS [Extent5]
                    WHERE 4 =  CAST( [Extent5].[Type] AS int)
                )  AS [Project15]
            )  AS [Project15]
            WHERE [Project15].[row_number] > 0
            ORDER BY [Project15].[mtime] DESC) AS [UnionAll4] ) AS [Distinct4]
    LEFT OUTER JOIN [dbo].[Retailer] AS [Extent6] ON [Distinct4].[C2] = [Extent6].[Id]
    LEFT OUTER JOIN [dbo].[Brand] AS [Extent7] ON [Distinct4].[C2] = [Extent7].[Id]
    LEFT OUTER JOIN [dbo].[Product] AS [Extent8] ON [Distinct4].[C2] = [Extent8].[Id]
    LEFT OUTER JOIN [dbo].[Certification] AS [Extent9] ON [Distinct4].[C2] = [Extent9].[Id]
    LEFT OUTER JOIN [dbo].[Claim] AS [Extent10] ON [Distinct4].[C2] = [Extent10].[Id]

So my overall questions are:

1) Is there a simpler SQL query I can execute to get the same results? I know that T-SQL doesn't support offsets per table in a subquery, hence the subquery wrapping.

2) If there isn't, what am I doing wrong in my LINQ query? Is this even possible with LINQ?


I wanted to add the SQL from @radar here all nice and formatted. It at least appears to be an elegant solution to avoid the sub-subqueries, and still accomplishes the offset/fetch.

SELECT *
    FROM (SELECT
        [ContentItem].*,
        row_number() OVER ( PARTITION BY Type ORDER BY ContentItem.mtime ) as rn
        FROM [dbo].[ContentItem]
        LEFT JOIN [dbo].[Retailer] ON (Retailer.Id = ContentItem.Id)
        LEFT JOIN [dbo].[Brand] ON (Brand.Id = ContentItem.Id)
        LEFT JOIN [dbo].[Product] ON (Product.Id = ContentItem.Id)
        LEFT JOIN [dbo].[Certification] ON (Certification.Id = ContentItem.Id)
        LEFT JOIN [dbo].[Claim] ON (Claim.Id = ContentItem.Id)
    ) as x
WHERE x.rn >= a AND x.rn <= b;

a is the lower threshold (offset) and b is the upper threshold (fetch-ish). The only catch is that b now equals fetch + a instead of just fetch. The first set of results would be WHERE x.rn >= 0 AND x.rn <= 6, the second set WHERE x.rn >= 6 AND x.rn <= 12, third WHERE x.rn >= 12 AND x.rn <= 18, and so on.

Upvotes: 0

Views: 814

Answers (2)

aholmes
aholmes

Reputation: 458

Well, it appears that I'm an idiot. That TOP(1000) call should have tipped me off. I assumed that my take variable was set to 6 but it was, in fact, set to 1000. Turns out my giant LINQ query works as expected, but the nested UNION ALL statements threw me off.

Still, I'm going to investigate @radar's answer further. It's hard to argue with better performance.

enter image description here

Upvotes: 0

radar
radar

Reputation: 13425

As you are looking simpler SQL, you can use row_number analytic function, which would be faster

You need to try and see as there are many left joins and also proper index need to exists in these tables.

select * 
from (
select *, row_number() over ( partition by Type order by ContentItem.mtime ) as rn
from [dbo].[ContentItem]
LEFT JOIN [dbo].[Retailer] ON (Retailer.Id = ContentItem.Id)
LEFT JOIN [dbo].[Brand] ON (Brand.Id = ContentItem.Id)
LEFT JOIN [dbo].[Product] ON (Product.Id = ContentItem.Id)
LEFT JOIN [dbo].[Certification] ON (Certification.Id = ContentItem.Id)
LEFT JOIN [dbo].[Claim] ON (Claim.Id = ContentItem.Id);
)
where rn <= 6

Upvotes: 1

Related Questions