Some problems in ef queries

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Az I see in lots of queries in service layer first you mentioned order by or sort and then filters
in this way in sql side you generate a big view and then select on this view and then filter which in large data make cost a big time
for an instance in CategoryService (it is not a big table but for an example) :
we have :
public virtual IList<Category> GetAllCategoriesDisplayedOnHomePage()
        {
            var query = from c in _categoryRepository.Table
                        orderby c.DisplayOrder
                        where c.Published &&
                        !c.Deleted &&
                        c.ShowOnHomePage
                        select c;

            var categories = query.ToList();
            return categories;
        }
in can be like this :
   var query = from c in _categoryRepository.Table
                        where c.Published &&
                        !c.Deleted &&
                        c.ShowOnHomePage
                       orderby c.DisplayOrder
                        select c;

another thing which can help in performance is using no lock in big data lists.

thank you so much for your grate architecture and lovely project.
yours,
farshid.
11 years ago
Can you give more details to the generated sql? Looking in the debugger both queries generate:

{SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[Description] AS [Description],
[Extent1].[CategoryTemplateId] AS [CategoryTemplateId],
[Extent1].[MetaKeywords] AS [MetaKeywords],
[Extent1].[MetaDescription] AS [MetaDescription],
[Extent1].[MetaTitle] AS [MetaTitle],
[Extent1].[SeName] AS [SeName],
[Extent1].[ParentCategoryId] AS [ParentCategoryId],
[Extent1].[PictureId] AS [PictureId],
[Extent1].[PageSize] AS [PageSize],
[Extent1].[AllowCustomersToSelectPageSize] AS [AllowCustomersToSelectPageSize],
[Extent1].[PageSizeOptions] AS [PageSizeOptions],
[Extent1].[PriceRanges] AS [PriceRanges],
[Extent1].[ShowOnHomePage] AS [ShowOnHomePage],
[Extent1].[HasDiscountsApplied] AS [HasDiscountsApplied],
[Extent1].[Published] AS [Published],
[Extent1].[Deleted] AS [Deleted],
[Extent1].[DisplayOrder] AS [DisplayOrder],
[Extent1].[CreatedOnUtc] AS [CreatedOnUtc],
[Extent1].[UpdatedOnUtc] AS [UpdatedOnUtc]
FROM [dbo].[Category] AS [Extent1]
WHERE ([Extent1].[Published] = 1) AND ([Extent1].[Deleted] <> cast(1 as bit)) AND ([Extent1].[ShowOnHomePage] = 1)
ORDER BY [Extent1].[DisplayOrder] ASC}
11 years ago
Thank you for fast answer.
I saw this query in profiler :
(@p__linq__0 datetime2(7),@p__linq__1 datetime2(7),@p__linq__2 datetime2(7),@p__linq__3 datetime2(7),@p__linq__4 int,@p__linq__5 int,@p__linq__6 int,@p__linq__7 int,@p__linq__8 int,@p__linq__9 int,@p__linq__10 bit,@p__linq__11 nvarchar(4000))SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
    SUM([Filter1].[PriceExclTax]) AS [A1]
    FROM   (SELECT [Extent1].[PriceExclTax] AS [PriceExclTax], [Extent2].[BillingAddressId] AS [BillingAddressId], [Extent2].[OrderStatusId] AS [OrderStatusId], [Extent2].[ShippingStatusId] AS [ShippingStatusId], [Extent2].[PaymentStatusId] AS [PaymentStatusId], [Extent2].[CreatedOnUtc] AS [CreatedOnUtc1]
        FROM    [dbo].[OrderProductVariant] AS [Extent1]
        INNER JOIN [dbo].[Order] AS [Extent2] ON [Extent1].[OrderId] = [Extent2].[Id]
        INNER JOIN [dbo].[ProductVariant] AS [Extent3] ON [Extent1].[ProductVariantId] = [Extent3].[Id]
        INNER JOIN [dbo].[Product] AS [Extent4] ON [Extent3].[ProductId] = [Extent4].[Id]
        WHERE ([Extent2].[Deleted] <> cast(1 as bit)) AND ([Extent3].[Deleted] <> cast(1 as bit)) AND ([Extent4].[Deleted] <> cast(1 as bit)) ) AS [Filter1]
    INNER JOIN [dbo].[Address] AS [Extent5] ON [Filter1].[BillingAddressId] = [Extent5].[Id]
    WHERE ((@p__linq__0 IS NULL) OR (@p__linq__1 <= [Filter1].[CreatedOnUtc1])) AND ((@p__linq__2 IS NULL) OR (@p__linq__3 >= [Filter1].[CreatedOnUtc1])) AND (@p__linq__4 IS NULL OR @p__linq__5 = [Filter1].[OrderStatusId]) AND (@p__linq__6 IS NULL OR @p__linq__7 = [Filter1].[PaymentStatusId]) AND (@p__linq__8 IS NULL OR @p__linq__9 = [Filter1].[ShippingStatusId]) AND ((@p__linq__10 = 1) OR (( NOT (([Extent5].[Email] IS NULL) OR (( CAST(LEN([Extent5].[Email]) AS int)) = 0))) AND ([Extent5].[Email] LIKE @p__linq__11 ESCAPE N'~')))
)  AS [GroupBy1]
11 years ago
this looks like a query for orders (in the Admin or the customer Account)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.