I am trying to optimize this table-valued function. If I could, I would change it to the procedure but I cant. The problem is with two update statements. I kept only those two in the function because they are causing main performance issues. I rewrote the first one from outer apply to inner join and I looked at the statistics and they were wrong so I added an option(recompile) and it significantly helped. The problem is within the second update. The statistics are wrong and I do not know how to make an appropriate execution plan and optimize it with hints. Do you please have any idea how to get the time down? I tried to index table variable but with no result.
Here is an execution plan https://www.brentozar.com/pastetheplan/?id=B1EdBo5e4
Thanks.
CREATE FUNCTION [dbo].[cfn_PlanServis_Seznam](
@IDVazRole INT,
@IDUzivatel INT,
@IDRole INT,
@IDLokalita INT,
@lCid INT
)
RETURNS @PlanServis TABLE(
lIDAuto INT,
szSPZ VARCHAR(100),
lDepozit INT,
szTypVozidla varchar(100),
szTypServisu NVARCHAR(300),
szServisniPlan NVARCHAR(300),
lZbyvaDni INT,
lZbyvaKm INT,
lNajetoKm INT,
dtServis DATETIME,
dcZbyvaMotohodin DECIMAL(15,1),
dcNajetoMotohodin DECIMAL(15,1),
IDVazPlanServisAuto INT,
IDPlanServisDefinice INT,
lBarva INT
)
AS
BEGIN
DECLARE @Auto TABLE(
lIDAuto INT,
szSPZ VARCHAR(100),
szTyp VARCHAR(100),
IDCisTypServis INT,
szTypServisu NVARCHAR(500),
szServisniPlan NVARCHAR(500),
lKmStart INT,
dtStart DATETIME,
lKmPriZavedeni INT,
lUjetoPredZavedenim INT,
dcMotohodinyStart DECIMAL(15,1),
lIntervalKm INT,
dcIntervalMotohodiny DECIMAL(15,1),
lUjeto INT,
dcMotohodiny DECIMAL(15,1),
IDServis INT,
lKmServis INT,
dcMotohodinyServis DECIMAL(15,1),
dtServis DATETIME,
lIntervalDatum INT,
lDniUbehlo INT,
lBarva INT,
lZbyvaKm INT,
dcZbyvaMotohodin DECIMAL(15,2),
lZbyvaDni INT,
lDepozit INT,
IDVazPlanServisAuto INT,
IDPlanServisDefinice INT,
lMaxTachograf INT,
lKmPretaceni INT,
dtOd DATE,
lKmPosledniServis INT
)
DECLARE @IDCisAutoParametrKmPriZavadeni INT = 10012
DECLARE @lKmPred INT = 30000
DECLARE @lKmPredMensi INT = 15000
DECLARE @lDniPred INT = 60
DECLARE @lDniPredMensi INT = 30
DECLARE @lMotohodinyPred INT = 100
DECLARE @lMotohodinyPredMensi INT = 50
DECLARE @IDBarvaBlizi INT = 1010 --Odkaz do CisTermBarva
DECLARE @IDBarvaBliziMensi INT = 1016 --Odkaz do CisTermBarva
DECLARE @IDBarvaPres INT = 1017 --Odkaz do CisTermBarva
--============ Koenc deklarace promennych ===========
INSERT INTO @Auto (lIDAuto, szSPZ, szTyp, IDCisTypServis, szTypServisu, lKmStart, dtStart, lKmPriZavedeni, dcMotohodinyStart,[@Auto].lIntervalKm,[@Auto].dcIntervalMotohodiny,[@Auto].lIntervalDatum,szServisniPlan,[@Auto].IDVazPlanServisAuto,[@Auto].IDPlanServisDefinice)
SELECT Auto.lIDAuto,
Auto.szSpz,
CASE WHEN Auto.lTyp = 0 THEN 'Taha?' WHEN Auto.lTyp = 1 THEN 'N?v?s' ELSE '' END,
PlanServisDefinice.IDCisTypServis,
dbo.GetLocalText('CisTypServis','szNazev',CisTypServis.lIDCisTypServis,@lCid,CisTypServis.szNazev,''),
PlanServisDefinice.lStartKM,
PlanServisDefinice.dtStartDatum,
CONVERT(INT,VazAutoParametr.varHodnota),
PlanServisDefinice.dcMotohodinyStart,
PlanServisDefinice.lIntervalKM,
PlanServisDefinice.dcIntervalMotohodin,
PlanServisDefinice.lIntervalDatum,
PlanServis.szNazev,
PlanServisDefinice.IDVazPlanServisAuto,
PlanServisDefinice.lIDPlanServisDefinice
FROM Auto INNER JOIN VazPlanServisAuto ON Auto.lIDAuto = VazPlanServisAuto.IDAuto
INNER JOIN PlanServisDefinice ON VazPlanServisAuto.lIDVazPlanServisAuto = PlanServisDefinice.IDVazPlanServisAuto
INNER JOIN CisTypServis ON PlanServisDefinice.IDCisTypServis = CisTypServis.lIDCisTypServis
LEFT OUTER JOIN VazAutoParametr ON VazAutoParametr.IDAuto = Auto.lIDAuto AND VazAutoParametr.IDCisAutoParametr = @IDCisAutoParametrKmPriZavadeni
INNER JOIN PlanServis ON VazPlanServisAuto.IDPlanServisu = PlanServis.lIDPlanServis
UPDATE @Auto SET lUjeto = Km.lKm
FROM @Auto INNER JOIN
(SELECT
SUM(JizdaTachograf.lkmDo - JizdaTachograf.lkmOd) AS lKm, JizdaTachograf.IDAuto,JizdaTachograf.IDNaves
FROM Jizda
INNER JOIN JizdaTachograf ON JizdaTachograf.IDJizda = Jizda.lIDJizda
WHERE JizdaTachograf.lkmOd IS NOT NULL
AND JizdaTachograf.lkmDo IS NOT NULL
AND Jizda.lProvozne = 1
GROUP BY
JizdaTachograf.IDAuto,JizdaTachograf.IDNaves
) as Km
ON Km.IDAuto = [@Auto].lIDAuto OR Km.IDNaves = [@Auto].lIDAuto
OPTION (RECOMPILE)
UPDATE @Auto SET lMaxTachograf = ISNULL(ISNULL(Km.lKm, [@Auto].lKmServis),[@Auto].lKmStart)
FROM @Auto
OUTER APPLY
(SELECT TOP 1 JizdaTachograf.lkmDo lKm
FROM Jizda INNER JOIN
JizdaTachograf ON JizdaTachograf.IDJizda = Jizda.lIDJizda
WHERE JizdaTachograf.lkmOd IS NOT NULL AND
JizdaTachograf.lkmDo IS NOT NULL AND Jizda.lProvozne = 1
AND (JizdaTachograf.IDAuto = [@Auto].lIDAuto
OR JizdaTachograf.IDNaves = [@Auto].lIDAuto)
AND Jizda.dtZacatek > ISNULL(ISNULL([@Auto].dtServis,
[@Auto].dtStart),DATEADD(YEAR,-100,GETDATE()))
ORDER BY
Jizda.dtZacatek DESC, JizdaTachograf.lkmDo desc) Km
INSERT INTO @PlanServis (lIDAuto,
szSPZ,
lDepozit,
szTypVozidla,
szTypServisu,
szServisniPlan,
lZbyvaDni,
lZbyvaKm,
lNajetoKm,
dtServis,
dcZbyvaMotohodin,
dcNajetoMotohodin,
IDVazPlanServisAuto,
IDPlanServisDefinice,
lBarva)
SELECT lIDAuto,
szSPZ,
lDepozit,
szTyp,
szTypServisu,
szServisniPlan,
lZbyvaDni,
lZbyvaKm,
lUjeto,--lNajetoKm,
dtServis,
dcZbyvaMotohodin,
dcMotohodiny,--dcNajetoMotohodin,
IDVazPlanServisAuto,
IDPlanServisDefinice,
lBarva
FROM @Auto
RETURN
END
GO