I have a cte based query which runs for a very long time, and on the Activity Monitor for the query there are multiple rows showing the query is suspended in pageiolatch_sh. The strange thing is that this occurs only when executed using CTE (as first code bellow). If I use a temporary table instead (as in the second code bellow) it finish in a split-second.Any idea what can cause this?
QUERY1:
with cte AS(select a.*, ROW_NUMBER() OVER (ORDER BY RecordDate) as row_number from tbl_ShiurimList a inner join tbl_FolderStructure b on a.MainFolderID=b.FolderID where contains(NodeKeyPath,'0057900 and not (00058058 or 00057901 or 000401980)') and isnull(IsDeleted,0)=0)select * from cte where row_number=5
QUERY2:
select a.*, ROW_NUMBER() OVER (ORDER BY RecordDate) as row_numberinto #tempfrom tbl_ShiurimList a inner join tbl_FolderStructure b on a.MainFolderID=b.FolderIDwhere contains(NodeKeyPath,'0057900 and not (00058058 or 00057901 or 000401980)') and isnull(IsDeleted,0)=0select * from #temp where row_number=5