I ask for help on the next problem.
We're using a view with a recursive select statement in it (see below). On a specific server (server A) the performance of this view is varying a lot. Respons times differ between 100 ms and 30 seconds or even more. We've tested the same view on
a backup of the database on an other server (server B) which results in a stable and good performance. A backup of the database installed on server A without any other interactions results again in varying performance. We compared the OS and database settings
of server A and server B. There are no differences.
We need extra input to solve this problem. So please help!
Regards
Ad
create view [dbo].[vwMarketSegmentsAll] as
WITH MarketSegmentsAll (MSID,MSVersion, MSName, MarketSegmentFK, SegmentCategoryFK, RegionFK, CountryCode, CountryFK, GlobalSegmentFK, MSLevel, MSCode, FirstActiveYear, LastActiveYear, Label, RootMarketSegmentFK, CropSegmentFK, CropFK) AS
(
-- Anchor member definition
SELECT ms.ID, ms.Version, ms.Name, ms.MarketSegmentFK, ms.SegmentCategoryFK, ms.RegionFK, r.CountryCode, r.ID as CountryFK, ms.GlobalSegmentFK, 1, MSCode = CAST(ms.GlobalSegmentFK as nvarchar(50)), ms.FirstActiveYear, ms.LastActiveYear,gs.Label,
ms.ID, gs.CropSegmentFK,gs.CropFK
FROM MarketSegment AS ms
join Region r on r.id = ms.RegionFK
join GlobalSegment as gs on gs.id = ms.GlobalSegmentFK
where not ms.GlobalSegmentFK is null
UNION ALL
-- Recursive member definition
SELECT ms.ID, ms.Version, ms.Name, ms.MarketSegmentFK, ms.SegmentCategoryFK, ms.RegionFK, msc.CountryCode, msc.CountryFK, msc.GlobalSegmentFK, msc.MSLevel + 1, CAST(MSCode + case when ms.SegmentCategoryFK is null then '' else
'-' + CAST(ms.SegmentCategoryFK as nvarchar(3)) end as nvarchar(50)), ms.FirstActiveYear, ms.LastActiveYear, msc.Label,msc.RootMarketSegmentFK, msc.CropSegmentFK, msc.CropFK
FROM MarketSegment AS ms
JOIN MarketSegmentsAll AS msc ON msc.MSID = ms.MarketSegmentFK
where ms.GlobalSegmentFK is null
)
-- Statement that executes the CTE
select msA.MSLevel,
msA.MSVersion,
msA.MSCode,
msA.MSName,
msA.MSID,
msA.MarketSegmentFK,
msA.SegmentCategoryFK,
msA.GlobalSegmentFK,
msA.RegionFK,
msA.CountryCode,
msA.CountryFK,
msA.FirstActiveYear,
msA.LastActiveYear,
msA.Label,
RootMarketSegmentFK =
CASE
WHEN msA.MSLevel > 1 THEN msA.RootMarketSegmentFK
ELSE null
END,
msa.CropSegmentFK,
msa.CropFK
FROM MarketSegmentsAll msA
GO