salut am o problema cu codul asta...ma poate ajuta cineva, cum as putea sa optimizez ce incerc sa fac mai jos....un sfat ceva
declare @Data varchar(20)
select @Data = convert(varchar, convert(datetime, dateadd(day, -1, getdate()), 112), 112)
if object_id('tempdb..#tmp_LGX ') is not null drop table #tmp_LGX
create table #tmp_LGX
(
CustomerCode int,
CZL money
)
insert into #tmp_LGX
select b.CUSTOMER, b.CZL
from DATA..LGX b
where b.CUSTOMER is not null and b.JDATA >= dateadd(year,-1,@Data) and JDATA <= @Data
if object_id('tempdb..#tmp_LGX_J ') is not null drop table #tmp_LGX_J
create table #tmp_LGX_J
(
CustomerCode int,
Suma money
)
insert into #tmp_LGX_J
select b.CustomerCode, sum(b.CZL)/ case
when datediff(month, min(a.OpeningDate), @Data) >= 1
and datediff(month, min(a.OpeningDate), @Data) <= 12
then datediff(month, min(a.OpeningDate), @Data)
when datediff(month, min(a.OpeningDate), @Data) = 0 then 1
else 12
end
from #tmp_LGX b, TBL_ACC a
where b.CustomerCode = a.CustomerId
group by b.CustomerCode
update a set a.ValoareRulaj = b.Suma
from TEST a, #tmp_LGX_J b
where a.CustomerCode = b.CustomerCode
update a set Rulaj = case
when a.ValoareRulaj < 200 and a.TipClient = 'PF' then 'sub 200 ron pe luna'
when a.ValoareRulaj >= 200 and a.ValoareRulaj <= 500 and a.TipClient = 'PF' then 'intre 200 si 500 ron pe luna'
when a.ValoareRulaj > 500 and a.TipClient = 'PF' then 'peste 500 ron pe luna'
when a.ValoareRulaj < 400 and a.TipClient = 'PFA' then 'sub 400 ron pe luna'
when a.ValoareRulaj >= 400 and a.ValoareRulaj <= 1000 and a.TipClient = 'PFA' then 'intre 400 si 1000 ron pe luna'
when a.ValoareRulaj > 1000 and a.TipClient = 'PFA' then 'peste 1000 ron pe luna'
when a.ValoareRulaj < 2000 and a.TipClient = 'PJ' then 'sub 2000 ron pe luna'
when a.ValoareRulaj >= 2000 and a.ValoareRulaj <= 20000 and a.TipClient = 'PJ' then 'intre 2000 si 20000 ron pe luna'
when a.ValoareRulaj > 20000 and a.TipClient = 'PJ' then 'peste 20000 ron pe luna'
else ''
end
from TEST a
mai specific ca tabela LGX de pe baza de date DATA (DATA..LGX) este f mare are 28 GB si vreo 30 milioane de inregistari.
vreun index pe tabele temporare ar ajuta?
multumesc.
alex.