У меня одна и та же хранимая процедура в 2 базах данных. Он отлично работает в первой, но занимает больше 10 секунд в другой базе данных. Я извлек это, чтобы запросить и есть подобная вещь. Вот мой запрос. Буду признателен за любую помощь экспертов.
SELECT
dbo.Installment.Id, dbo.Installment.InstallmentNo,
dbo.Installment.InstallmentOrder, dbo.Installment.Amount,
dbo.Installment.DueDate, dbo.Installment.AmountPaid,
dbo.Installment.PaidOn, dbo.Installment.PlotId,
dbo.Installment.SurchargePaid, dbo.Installment.surchargePaidOn,
dbo.Installment.PartialInstallmentId, dbo.Installment.Is_Lumpsum,
dbo.Plot.PlotNo, dbo.Plot.PhaseId, dbo.Plot.InstallmentPlanId,
dbo.InstallmentPlan.StartDate, dbo.InstallmentPlan.InstSurchargeDueMonth,
dbo.GetInstPlanDueDate(dbo.InstallmentPlan.StartDate,
ISNULL(dbo.Installment.DueDate, GETDATE()),
ISNULL(dbo.Installment.PaidOn, GETDATE()),
dbo.InstallmentPlan.InstSurchargeDueMonth,
dbo.Installment.InstallmentOrder,
ISNULL(dbo.Installment.Is_Lumpsum, 0),
InstallmentStartDate.DueDate, dbo.Installment.PlotId) AS Sutcharge_Start_From,
dbo.CalculateSurchargableDays(dbo.Installment.DueDate,
ISNULL(dbo.Installment.PaidOn, GETDATE()),
dbo.GetInstPlanDueDate(dbo.InstallmentPlan.StartDate,
ISNULL(dbo.Installment.DueDate, GETDATE()),
ISNULL(dbo.Installment.PaidOn, GETDATE()),
dbo.InstallmentPlan.InstSurchargeDueMonth,
dbo.Installment.InstallmentOrder,
ISNULL(dbo.Installment.Is_Lumpsum, 0),
InstallmentStartDate.DueDate,
dbo.Installment.PlotId),
dbo.Installment.InstallmentOrder) AS days,
case isnull(dbo.Installment.SurchargePaid,0) when 0 then
dbo.CalculateSurchargableDays(dbo.Installment.DueDate, ISNULL(dbo.Installment.PaidOn, GETDATE()),
dbo.GetInstPlanDueDate(dbo.InstallmentPlan.StartDate, ISNULL(dbo.Installment.DueDate, GETDATE()), isnull(dbo.Installment.PaidOn,getdate()), dbo.InstallmentPlan.InstSurchargeDueMonth, dbo.Installment.InstallmentOrder,
ISNULL(dbo.Installment.Is_Lumpsum, 0), InstallmentStartDate.DueDate, dbo.Installment.PlotId),dbo.Installment.InstallmentOrder) * (ISNULL(dbo.Installment.Amount, 0) * (dbo.InstallmentPlan.InstSurchargePercentage / 365 / 100))
else
isnull(dbo.Installment.Surcharge,0)
end
AS surcharge_calculated, case isnull(dbo.Installment.AmountPaid,0) when 0 then 0 else 0 end as Payment_Status,dbo.Installment.InstallmentOrder%6 as t
FROM
dbo.Installment
INNER JOIN
dbo.Plot ON dbo.Installment.PlotId = dbo.Plot.Id
INNER JOIN
dbo.InstallmentPlan ON dbo.Plot.InstallmentPlanId = dbo.InstallmentPlan.Id
INNER JOIN
(SELECT PlotId, MIN(DueDate) AS DueDate
FROM dbo.Installment
GROUP BY InstallmentOrder, PlotId
HAVING (InstallmentOrder = 0)) AS InstallmentStartDate ON InstallmentStartDate.plotid = dbo.installment.plotid
WHERE
((dbo.Plot.InstallmentPlanId > 0))
STATISTICS IO для того, кто отлично работает
(5089 row(s) affected)
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Installment'. Scan count 2, logical reads 109228, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Plot'. Scan count 1, logical reads 218, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'InstallmentPlan'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
и медленный:
(64577 row(s) affected)
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Installment'. Scan count 2, logical reads 2842959, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Plot'. Scan count 1, logical reads 272, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'InstallmentPlan'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
Мой фактический планы выполнения здесь
быстрый запрос
медленный запрос