I created a view with cte WITH UserGroupUserGroups AS ( SELECT CAST(NULL AS UNIQUEIDENTIFIER) AS ParentUserGroupID, ug.UserGroupID AS ChildUserGroupID FROM UserSecurity.UserGroups AS ug WHERE NOT EXISTS(SELECT 'X' FROM UserSecurity.UserGroupUserGroups AS ugug WHERE ugug.ChildUserGroupID = ug.UserGroupID) UNION ALL SELECT parentGroups.ChildUserGroupID AS ParentUserGroupID, childGroups.ChildUserGroupID AS ChildUserGroupID FROM UserGroupUserGroups AS parentGroups INNER JOIN UserSecurity.UserGroupUserGroups AS childGroups ON childGroups.ParentUserGroupID = parentGroups.ChildUserGroupID ) SELECT * FROM UserGroupUserGroups AS ugugf WHERE ParentUserGroupID IS NOT NULL As of now i dont have any data into it but i expect millinons of records once this query is implemented. How can i test perofrmance in before so tht i dont get into problems with real data.
Check the indexes available on the table. In the first look you may have Clustered index scan or table scan because you are using NOT Exists. Madhu
You can check using Execution plan within query editor as it will give idea about temporary worktabels used in TEMPDB, by design a worktable is used, performance is generally hurt because of the extra I/O required to maintain the worktable. Refer to this blog post http://blogs.msdn.com/sqltips/archive/2007/08/30/spool-operators-in-query-plan.aspx on checking execution plan for CTE and this http://www.sql-server-performance.com/article_print.aspx?id=114&type=tip article about worktables.