我有一个复杂的存储过程要求,返回大约100000行,我需要返回所有行,以便可以导出数据,但是查询需要很长时间才能执行,是否有可能查看查询和提供任何性能改进,因为我尝试了很多不同的想法?实际上,我认为这个问题的调用让我觉得这个问题变慢了吗?
非常感谢任何提示
ALTER PROCEDURE [dbo].[Orders_GetItems]
@DateType int,
@DateFrom datetime,
@DateTo datetime,
@PaymentType int,
@PaymentReceived int,
@ProductsList nvarchar(2000) = '',
@StartRowIndex int,
@MaximumRows int,
@VirtualCount int output
AS
-- The number of rows affected by the different commands
-- does not interest the application, so turn NOCOUNT ON
SET NOCOUNT ON
-- Determine the first record and last record
DECLARE @FirstRec int, @LastRec int
SELECT @FirstRec = (@StartRowIndex - 1) * @MaximumRows
SELECT @LastRec = (@StartRowIndex * @MaximumRows + 1);
SELECT @VirtualCount = count(*)
from qdos_OrderPayments p
left join qdos_PaymentTypes pt on p.PaymentTypeID = pt.PaymentTypeID
inner join qdos_orders o on p.OrderID = o.OrderID
inner join sf_user_profile_link pl on o.customerid = pl.user_id
inner join sf_sitefinity_profile sp on pl.profile_id = sp.id
left join qdos_WhiteLabels wl on wl.WhiteLabelID = sp.white_label_id
left join qdos_Coupons c on o.CouponID = c.CouponID
where
o.ShopID = 1
and ((@DateType = 1 and p.PaymentAddedDate >=@DateFrom and p.PaymentAddedDate <=Dateadd(n,-1,Dateadd(d,1,@DateTo))) or
(@DateType = 2 and p.PaymentReceivedDate >=@DateFrom and p.PaymentReceivedDate <=Dateadd(n,-1,Dateadd(d,1,@DateTo))))
and (@PaymentType = -1 or (@PaymentType = 1 and p.PaymentTypeID IN (1,2,3,4)) or (@PaymentType = 2 and p.PaymentTypeID IN (5,6)) )
and (@PaymentReceived = -1 or (@PaymentReceived = 0 and p.PaymentReceivedDate IS Null or (@PaymentReceived = 1 and p.PaymentReceivedDate IS Not Null)))
and (@ProductsList = '' or o.ProductID in (Select IntValue from dbo.CsvToInt(@ProductsList)))
SELECT top(@MaximumRows) p.PaymentID, p.OrderID, o.ProductID, o.GroupRef, sp.account_number as AccountNumber, o.CompanyName, sp.Address1, sp.Address2, sp.City, sp.Postcode,
o.Trade, o.SubTrade, o.PolicyNum, isnull(NULLIF(o.MultiProductTitle,''), o.ProductTitle) as OrderTitle, o.IndemnityLevel, o.PolicyStartDate, o.PolicyExpiryDate,
o.ApplicationDate, o.PaymentDate, o.TotalPrice, (o.TotalPrice - (select SUM(pay.PaymentAmount) from qdos_OrderPayments pay where pay.PaymentReceivedDate is not null and pay.OrderID = p.OrderID)) AS TotalBalanceOutstanding,
pt.Type AS PaymentType, p.PaymentAddedDate, p.PaymentReceivedDate,
TotalVAT = ((o.TotalPrice ) - ((o.TotalPrice) / ((100 + o.VATRate) / 100))),
TotalIPT = ((o.TotalPrice - o.IPTExcemptAmount) - ((o.TotalPrice - o.IPTExcemptAmount) / ((100 + o.IPTRate) / 100))),
TotalNET = (o.TotalPrice - ((o.TotalPrice ) - ((o.TotalPrice) / ((100 + o.VATRate) / 100))) - ((o.TotalPrice - o.IPTExcemptAmount) - ((o.TotalPrice - o.IPTExcemptAmount) / ((100 + o.IPTRate) / 100)))),
p.PaymentAmount,
PublicLiabilityPrice = (case(o.ProductID) WHEN 4 then (p.PaymentAmount / 100) * (100 - ((select sum(UnitCost) from qdos_OrderExtras e where e.OrderID = p.OrderID) / o.TotalPrice) * 100) end),
EmployersLiabilityPrice = (case(o.ProductID) WHEN 4 then (p.PaymentAmount / 100) * ((select sum(UnitCost) from qdos_OrderExtras e where e.OrderID = p.OrderID) / o.TotalPrice) * 100 end),
CoverPastWork = (case(o.ProductID) WHEN 3 then (select max(ExtraOption) from qdos_OrderExtras e where e.ProductID = 12 and e.OrderID = p.OrderID) end),
ERNNumber = (select max(Response) from qdos_OrderQuestions q where q.QuestionID = 79 and q.OrderID = p.OrderID),
NameOfRig = (select max(Response) from qdos_OrderQuestions q where q.QuestionID = 153 and q.OrderID = p.OrderID),
GadgetDetails = (case(o.ProductID) WHEN 65 then
(select max(Response) from qdos_OrderQuestions q where q.QuestionID = 173 and q.OrderID = p.OrderID) + ' ' + (select max(Response) from qdos_OrderQuestions q where q.QuestionID = 177 and q.OrderID = p.OrderID) + ' ' + (select max(Response) from qdos_OrderQuestions q where q.QuestionID = 172 and q.OrderID = p.OrderID) + ' | ' +
(select max(Response) from qdos_OrderQuestions q where q.QuestionID = 179 and q.OrderID = p.OrderID) + ' ' + (select max(Response) from qdos_OrderQuestions q where q.QuestionID = 180 and q.OrderID = p.OrderID) + ' ' + (select max(Response) from qdos_OrderQuestions q where q.QuestionID = 181 and q.OrderID = p.OrderID) + ' | ' +
(select max(Response) from qdos_OrderQuestions q where q.QuestionID = 185 and q.OrderID = p.OrderID) + ' ' + (select max(Response) from qdos_OrderQuestions q where q.QuestionID = 191 and q.OrderID = p.OrderID) + ' ' + (select max(Response) from qdos_OrderQuestions q where q.QuestionID = 196 and q.OrderID = p.OrderID)
end),
InsuredFullName = (select max(Response) from qdos_OrderQuestions q where q.QuestionID = 56 and q.OrderID = p.OrderID),
InsuredDOB = (select max(Response) from qdos_OrderQuestions q where q.QuestionID = 55 and q.OrderID = p.OrderID),
InsuredGender = (select max(Response) from qdos_OrderQuestions q where q.QuestionID = 57 and q.OrderID = p.OrderID),
o.Renewal, sp.Marketing, sp.How_Hear, sp.How_Hear_Other, wl.title as WhiteLabelName, c.CouponCode
from qdos_OrderPayments p
left join qdos_PaymentTypes pt on p.PaymentTypeID = pt.PaymentTypeID
inner join qdos_orders o on p.OrderID = o.OrderID
inner join sf_user_profile_link pl on o.customerid = pl.user_id
inner join sf_sitefinity_profile sp on pl.profile_id = sp.id
left join qdos_WhiteLabels wl on wl.WhiteLabelID = sp.white_label_id
left join qdos_Coupons c on o.CouponID = c.CouponID
where
o.ShopID = 1
and ((@DateType = 1 and p.PaymentAddedDate >=@DateFrom and p.PaymentAddedDate <=Dateadd(n,-1,Dateadd(d,1,@DateTo))) or
(@DateType = 2 and p.PaymentReceivedDate >=@DateFrom and p.PaymentReceivedDate <=Dateadd(n,-1,Dateadd(d,1,@DateTo))))
and (@PaymentType = -1 or (@PaymentType = 1 and p.PaymentTypeID IN (1,2,3,4)) or (@PaymentType = 2 and p.PaymentTypeID IN (5,6)) )
and (@PaymentReceived = -1 or (@PaymentReceived = 0 and p.PaymentReceivedDate IS Null or (@PaymentReceived = 1 and p.PaymentReceivedDate IS Not Null)))
and (@ProductsList = '' or o.ProductID in (Select IntValue from dbo.CsvToInt(@ProductsList)))
-- Turn NOCOUNT back OFF
SET NOCOUNT OFF