开发者

linq2sql renders unnecessary ORDER BY

I have a bit complex linq2sql query, it doesn't contain any 'order by' statements, but somehow linq2sql thinks it is necessery and inserts it. Unfortunately this 'order by' statement hurts performance and I don't know how to remove it...

Here's the linq2sql expressions (I don't think that they might help, but anyway...)

var lockedBy = Guid.NewGuid();
var locks = LinqDataContext.Instance.LockBranches(3, lockedBy, DateTime.Now + TimeSpan.FromMinutes(3)); // Stored procedure...

var x = LinqDataContext.Instance.Branches
.Where(branch => branch.LockedBy == lockedBy)
.Select
(
    branch => new
    {
        Branch = branch,
        Leaves = branch.Leaves
        .Select
        (
            leaf => new
            {
                Leaf = leaf,
                Estimate = leaf.Representation.Estimates
                .GroupBy(estimate=>estimate.SegmentID)
                .Sum
                (
                    estimatesBySegment => estimatesBySegment.Average
                    (
                        estimate => estimate.EstimateRequests.Average
                        (
                            estimateRequest => estimateRequest.EstimateSubmit.Value
                        )
                    )
                )
            }
        )
    }
);

It render开发者_C百科s to the following sql

SELECT [t0].[ID], [t0].[IndexID], [t0].[IndexNo], [t0].[Sealed], [t0].[LockedBy], [t0].[UnlockOn], [t1].[ID] AS [ID2], [t1].[BranchID], [t1].[RepresentationID], [t1].[Xml], (
    SELECT SUM([t7].[value])
    FROM (
        SELECT AVG([t6].[value]) AS [value]
        FROM (
            SELECT (
                SELECT AVG([t5].[Value])
                FROM [dbo].[EstimateRequests] AS [t4]
                LEFT OUTER JOIN [dbo].[EstimateSubmits] AS [t5] ON [t5].[EstimateRequestID] = [t4].[ID]
                WHERE [t4].[EstimateID] = [t3].[ID]
                ) AS [value], [t2].[ID], [t3].[RepresentationID], [t3].[SegmentID]
            FROM [dbo].[Representations] AS [t2], [dbo].[Estimates] AS [t3]
            ) AS [t6]
        WHERE ([t6].[ID] = [t1].[RepresentationID]) AND ([t6].[RepresentationID] = [t6].[ID])
        GROUP BY [t6].[SegmentID]
        ) AS [t7]
    ) AS [Estimate], (
    SELECT COUNT(*)
    FROM [dbo].[Leaves] AS [t8]
    WHERE [t8].[BranchID] = [t0].[ID]
    ) AS [value]
FROM [dbo].[Branches] AS [t0]
LEFT OUTER JOIN [dbo].[Leaves] AS [t1] ON [t1].[BranchID] = [t0].[ID]
WHERE [t0].[LockedBy] = @p0
ORDER BY [t0].[ID], [t1].[ID]  <-- Here's the unnecessary ORDER BY


The order by is needed to be able to distinguish the child collection Leaves. (Every time you have a child collection there is an order by)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜