sql - Run Speed (inefficient query) -


i have following query isnt efficiant , lot of time brings out of memory message, can make recomendations speed up? jim

declare @period_from int set @period_from = 201400  declare @period_to int set @period_to = 201414  declare @length int set @length = '12'  declare @query varchar(max) set @query = '%[^-a-za-z0-9() ]%'  select 'dim_2' field, null length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , dim_2 @query union select 'dim_3' field, null length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , dim_3 @query union select 'dim_4' field, null length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , dim_4 @query union select 'dim_5' field, null length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , dim_5 @query union select 'dim_6' field, null length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , dim_6 @query union select 'dim_7' field, null length,* table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , dim_7 @query union select 'ext_inv_ref' field, null length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , ext_inv_ref @query union select 'ext_ref' field, null length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , ext_ref @query union select 'description' field, null length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , description @query union select 'length dim_2' field,len(dim_2) length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , dim_2 not null , len(dim_2) >@length union select 'length dim_3' field, len(dim_3) length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , dim_3 not null , len(dim_3) >@length union select 'length dim_4' field, len(dim_4) length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , dim_4 not null , len(dim_4) >@length union select 'length dim_5' field, len(dim_5) length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , dim_5 not null , len(dim_5) >@length union select 'length dim_6' field, len(dim_6) length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , dim_6 not null , len(dim_6) >@length union select 'length dim_7' field, len(dim_7) length, * table1 client = 'cl'and period >= @period_from , @period_to <= @period_to , dim_7 not null , len(dim_7) >@length 

you can reduce number of unions significantly, work goes clause. sql query optimiser should figure out need go through rows in table once each union statement, should quicker. try , see!

select  case   when dim_2 @query 'dim_2'   when dim_3 @query 'dim_3'   when dim_4 @query 'dim_4'   when dim_5 @query 'dim_5'   when dim_6 @query 'dim_6'   when dim_7 @query 'dim_7'   when ext_inv_ref @query 'ext_inv_ref'  when ext_ref @query 'ext_ref' end field,  null length,  *  table1  client = 'cl'and period >= @period_from , @period_to <= @period_to  , (dim_2 @query       or dim_3 @query       or dim_4 @query       or dim_5 @query       or dim_6 @query       or dim_7 @query      or ext_inv_ref @query      or ext_ref @query) 

union

select  case   when dim_2 not null , len(dim_2) >@length 'length dim_2'   when dim_3 not null , len(dim_3) >@length 'length dim_3'    ....       end field,  len(dim_2) length, *  table1  client = 'cl'and period >= @period_from , @period_to <= @period_to  , ((dim_2 not null , len(dim_2) >@length)      or      (dim_3 not null , len(dim_3) >@length)      or ....      ) 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -