CROSS APPLY IN SQL-SERVER

علی ذوالفقار
1402/06/10 11:23:35 (273)
functions in sql-server can cause slow performance when we run a query on tons of rows of data.
we can use cross apply and cte to reduce  the use of functions  in our query like this 

there is two functions that generate FULLNAME and DISPLAYNAME
on this query we replace two function with CROSS APPLY

SELECT 
	ID , FNAME , LNAME , EMAIL, A.FULLNAME , B.DISPLAYNAME 
FROM  TBL_TEST T 
CROSS APPLY (SELECT FNAME + LNAME AS FULLNAME FROM TBL_TEST WHERE ID = T.ID  ) AS A 
CROSS APPLY (SELECT A.FULLNAME + EMAIL AS DISPLAYNAME FROM TBL_TEST WHERE ID = T.ID ) AS B 
Back