sql server - How to increase the performance of sql query that contains a lot of joins and union -


i have created procedure in following way,

create procedure proc_foo  begin declare @somevar int set @somevar=0 if exists (select column1 tbl1) begin set @somevar = 1 end if exists (select column2 tbl2) begin set @somevar = 1 end .... ... .... if exists (select columnn tbln) begin set @somevar = 1 end if(@somevar =1) begin  select count(column1) tbl1  union  select count(column2) tbl2  union  ...  ...  ...  select count(columnn) tbln  end end 

the select statements contains lot of joins , taking lot of time execute.can explain me different approach more faster.

i first suggest use "with recompile" hint on stored procedure. recompile sp , generate new execution plan based on how variable ends being set. fresh execution plan every time.

additionally, adding indexes on columns joining on , use include columns if creating non-clustered indexes columns you're outputting.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -