I was hoping to get some help in converting the following query to a SQL Server stored procedure.
string[] Includes = new string[] { "IntakeData" };
var r = _repo.Filter<ResultData>
(a => (a.TrackingIdentifier == trackingNum ||
a.IntakeData.Requestor_CaseID == trackingNum)
&& a.ResponseType == "RESPONSE"
&& a.Status == "OK"
&& a.Destination == rs, Includes)
.OrderByDescending(d => d.CreatedDate)
.Select(c => new { c.ResultStatus, c.Destination, c.IntakeData })
.FirstOrDefault();
Below is the query I extracted from SQL profiler, but the SQL query generated is far too complex and many unnecessary null checks are generated as a result. So, we made the decision to simplify it by using a similar stored procedure.
exec sp_executesql N'SELECT TOP (1)
[Project1].[C1] AS [C1],
[Project1].[ResultStatus] AS [ResultStatus],
[Project1].[Destination] AS [Destination],
[Project1].[ID] AS [ID],
[Project1].[TrackingIdentifier] AS [TrackingIdentifier],
[Project1].[ClientTrackingIdentifier] AS [ClientTrackingIdentifier],
[Project1].[RequestorEmail] AS [RequestorEmail],
[Project1].[CandidateEmail] AS [CandidateEmail],
[Project1].[ConsumerIdentifier] AS [ConsumerIdentifier],
[Project1].[LocationIdentifier] AS [LocationIdentifier],
[Project1].[CandidateID] AS [CandidateID],
[Project1].[TotalCount] AS [TotalCount],
[Project1].[KitIdentifier] AS [KitIdentifier],
[Project1].[QueryStatus] AS [QueryStatus],
[Project1].[QueryData] AS [QueryData],
[Project1].[CreatedDate1] AS [CreatedDate],
[Project1].[CorporateEntityID] AS [CorporateEntityID],
[Project1].[Requestor_CaseID] AS [Requestor_CaseID],
[Project1].[CandidateSMS] AS [CandidateSMS],
[Project1].[NotificationType] AS [NotificationType],
[Project1].[REDTAILOrderCreationStatus] AS [REDTAILOrderCreationStatus],
[Project1].[REDTAILOrderCreationErrorMessage] AS [REDTAILOrderCreationErrorMessage],
[Project1].[APIInviteData] AS [APIInviteData],
[Project1].[CRN] AS [CRN]
FROM ( SELECT
[Filter1].[ResultStatus] AS [ResultStatus],
[Filter1].[Destination] AS [Destination],
[Filter1].[CreatedDate1] AS [CreatedDate],
[Extent3].[ID] AS [ID],
[Extent3].[TrackingIdentifier] AS [TrackingIdentifier],
[Extent3].[ClientTrackingIdentifier] AS [ClientTrackingIdentifier],
[Extent3].[RequestorEmail] AS [RequestorEmail],
[Extent3].[CandidateEmail] AS [CandidateEmail],
[Extent3].[ConsumerIdentifier] AS [ConsumerIdentifier],
[Extent3].[LocationIdentifier] AS [LocationIdentifier],
[Extent3].[CandidateID] AS [CandidateID],
[Extent3].[TotalCount] AS [TotalCount],
[Extent3].[KitIdentifier] AS [KitIdentifier],
[Extent3].[QueryStatus] AS [QueryStatus],
[Extent3].[QueryData] AS [QueryData],
[Extent3].[CreatedDate] AS [CreatedDate1],
[Extent3].[CorporateEntityID] AS [CorporateEntityID],
[Extent3].[Requestor_CaseID] AS [Requestor_CaseID],
[Extent3].[CandidateSMS] AS [CandidateSMS],
[Extent3].[NotificationType] AS [NotificationType],
[Extent3].[REDTAILOrderCreationStatus] AS [REDTAILOrderCreationStatus],
[Extent3].[REDTAILOrderCreationErrorMessage] AS [REDTAILOrderCreationErrorMessage],
[Extent3].[APIInviteData] AS [APIInviteData],
[Extent3].[CRN] AS [CRN],
1 AS [C1]
FROM (SELECT [Extent1].[IntakeDataID] AS [IntakeDataID], [Extent1].[TrackingIdentifier] AS [TrackingIdentifier1], [Extent1].[ResultStatus] AS [ResultStatus], [Extent1].[Destination] AS [Destination], [Extent1].[CreatedDate] AS [CreatedDate1], [Extent2].[Requestor_CaseID] AS [Requestor_CaseID]
FROM [dbo].[ResultData] AS [Extent1]
LEFT OUTER JOIN [dbo].[IntakeData] AS [Extent2] ON [Extent1].[IntakeDataID] = [Extent2].[ID]
WHERE (''RESPONSE'' = [Extent1].[ResponseType]) AND ([Extent1].[ResponseType] IS NOT NULL) AND (''OK'' = [Extent1].[Status]) AND ([Extent1].[Status] IS NOT NULL) ) AS [Filter1]
LEFT OUTER JOIN [dbo].[IntakeData] AS [Extent3] ON [Filter1].[IntakeDataID] = [Extent3].[ID]
WHERE ((([Filter1].[TrackingIdentifier1] = @p__linq__0) AND ( NOT ([Filter1].[TrackingIdentifier1] IS NULL OR @p__linq__0 IS NULL))) OR (([Filter1].[TrackingIdentifier1] IS NULL) AND (@p__linq__0 IS NULL)) OR (([Filter1].[Requestor_CaseID] = @p__linq__1) AND ( NOT ([Filter1].[Requestor_CaseID] IS NULL OR @p__linq__1 IS NULL))) OR (([Filter1].[Requestor_CaseID] IS NULL) AND (@p__linq__1 IS NULL))) AND ((([Filter1].[Destination] = @p__linq__2) AND ( NOT ([Filter1].[Destination] IS NULL OR @p__linq__2 IS NULL))) OR (([Filter1].[Destination] IS NULL) AND (@p__linq__2 IS NULL)))
) AS [Project1]
ORDER BY [Project1].[CreatedDate] DESC',N'@p__linq__0 varchar(8000),@p__linq__1 varchar(8000),@p__linq__2 varchar(8000)',@p__linq__0='06ONMJ99SN',@p__linq__1='06ONMJ99SN',@p__linq__2='ACCIO'
Any help here to simplify the generated SQL query to a smaller stored procedure would be greatly appreciated.
Thanks
2条答案
按热度按时间tquggr8v1#
Try following procedure. The stored procedure simplifies the query and encapsulates it, making it more maintainable and reusable.:
When you want to retrieve the data using this query, you can call the
GetFilteredResultData
stored procedure and pass the appropriate parameters.7rtdyuoh2#
This is just a super fast reformat and removing aliases and a lot of unneeded parenthesis and square brackets and null checking and obviously NOT tested.
Still to do:
Kind of ugly code: