Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Pessoal,
Esta consulta esta bastante lenta no meu banco.
Quando removo a tabela trelsld a consulta roda rapido.
Gostaria, se possível, de alguma sugestão para melhorar a performance:
select saldo+quantidade as saldoatual,case when valoranterior is null then 0 else VALORANTERIOR end as valoranterior,*
from (select case when saldoprev is null then 0 else saldoprev end as saldo,
*,case when valorantigo is null then 0 else valorantigo end as antigo
from (
select distinct tt.nseqitmmov,tt.idprd,codigoreduzido,nomefantasia,preco1 as aprazo,
case when qtdeentrada is null then tt.quantidade * TUND.FATORCONVERSAO else tr.qtdeentrada END as quantidade,
case when tr.custoreposicaoa is null then tp.CUSTOREPOSICAO else tr.CUSTOREPOSICAOA end as precocheio,
preco1,margembrutalucro,tp.custounitario,
case when prdcesta is null then 'NÃO' else prdcesta end as prdcesta
,case when prdexterno is null then 'NÃO' else prdexterno end as prdexterno
,(select top 1 valoranterior from historicoprecosprodutos where idprd = tp.idprd
and idreajustepreco <= (select top 1 idreajustepreco from historicoprecosprodutos where idprd = tp.idprd order by idreajustepreco desc)order by idreajustepreco desc) as valoranterior
,(select top 1 novovalor from historicoprecosprodutos where idprd = tp.idprd
and idreajustepreco <= (select top 1 idreajustepreco from historicoprecosprodutos where idprd = tp.idprd order by idreajustepreco desc)order by idreajustepreco desc) as valorantigo
,prodpdv,libsaldo,codcfo, SALDOANTERIOR as saldoprev,tpcp.matlimp,ttb.VALOR as IPI
from tmov t WITH (NOLOCK)
inner join titmmov tt (NOLOCK) on (tt.idmov = t.idmov)
left join TUND (NOLOCK) on (TUND.CODUND = TT.CODUND)
left join TRELSLD tr on (tr.IDMOV = tt.IDMOV and tt.NSEQITMMOV = tr.NSEQITMMOV)
inner join tprd tp (NOLOCK) on (tp.idprd = tt.idprd)
left join tprdcompl tpcp (NOLOCK) on (tp.idprd = tpcp.idprd)
left join ttrbprd ttp (NOLOCK) on(ttp.idprd = tp.idprd)
left join TTRBMOV ttb (NOLOCK) on (ttb.IDMOV = tt.IDMOV and ttb.NSEQITMMOV = tt.NSEQITMMOV)
where substring(t.codtmv,1,1) = '1' and ttb.codtrb = 'IPI'
and t.numeromov = '000002470'
and t.codfilial = '1'
)TGERAL )TGERAL2 order by nseqitmmovCarregando comentários...