Though denormalizing the table helped your performance for one aspect, I believe it was not the way to achieve the improvement. Now all data retrieval operations will be difficult and slower than it needs to be. But to answer your question, this is the best I could come up with:set nocount onuse pubsgocreate table dbo.UF2_rt_images (Image_ID int ,dt_portfolio1_id int ,dt_portfolio2_id int ,dt_portfolio3_id int ,Image_Active bit ,User_ID int)goinsert dbo.UF2_rt_images select 1,1,1,3,1,82 union allselect 2,1,2,3,1,82 union allselect 3,1,2,3,1,82 union allselect 4,3,3,3,1,82 union allselect 5,1,2,3,1,82 union allselect 6,1,2,3,1,82 union allselect 7,1,2,3,1,82 union allselect 8,1,2,3,1,82 union allselect 9,1,2,3,1,82goselect count(*) [count] ,b.portfolio_id from ( select image_id, portfolio_id from (--normalize the columns select image_id, dt_portfolio1_id portfolio_id from dbo.UF2_rt_images WHERE image_active = 1 AND User_ID = 82 union all select image_id, dt_portfolio2_id portfolio_id from dbo.UF2_rt_images WHERE image_active = 1 AND User_ID = 82 union all select image_id, dt_portfolio3_id portfolio_id from dbo.UF2_rt_images WHERE image_active = 1 AND User_ID = 82 ) a --distinct the list so as to not double count portfolio_ids for same image_id group by image_id, portfolio_id ) bgroup by b.portfolio_idgodrop table dbo.UF2_rt_images
Be One with the OptimizerTG