Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

mpfleger

Performance de SQL

Recommended Posts

Bom tarde a todos, sou novato aqui e preciso da ajuda de vocês!


tenho o seguinte comando SQL:



select consulta.data, max(consulta.mes) as mes
from ( select TO_CHAR(a.DATA_MOVIMENTO, 'FMMONTH YYYY',
'nls_date_language=portuguese') as mes,
to_date(to_char(a.DATA_MOVIMENTO, 'mm/yyyy'), 'mm/yyyy') as data
from ESTQ_300_ESTQ_310 a
) consulta
group by consulta.data
order by consulta.data desc


porém o mesmo demora 40 segundos, e utilizo esse comando ao abrir a tela, ou seja não rola esperar 40 segundos...


a idéia é buscar de uma tabela de movimentações, as datas de movimentação e criar uma lista de períodos, por exemplo:

data de movimentações:

01/02/2015

05/02/2015

07/03/2015

deverá me retornar:

FEVEREIRO 2015

MARÇO 2015

na lista de retorno eu preciso ter pelo menos uma informação data, que seja: 01/02/2015 e 01/03/2015, pra depois eu poder fazer o filtro!

por que eu adiciono no combobox da seguinte forma:



while not eof do
begin
s := TStringList.Create;
s.Add(fieldbyname('data').AsString);


data_fim := IntToStr(DaysInMonth(StrToDate(fieldbyname('data').AsString))) + Copy(fieldbyname('data').AsString, 3, Length(fieldbyname('data').AsString));
data_inicio := '01' + Copy(fieldbyname('data').AsString, 3, Length(fieldbyname('data').AsString));


cbPeriodo.Items.AddObject(fieldbyname('mes').AsString + ' ' + data_inicio + ' até ' + data_fim, TObject(s[0]));
next;
end;


se alguém poder me ajudar para que meu sql fique mais rápido, agradeço!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Parece ser um full table scan , não há muito o que melhorar

Compartilhar este post


Link para o post
Compartilhar em outros sites

gente, fiz assim:

select consulta.data, max(consulta.mes) as mes
from ( select
              (case when to_char(a.DATA_MOVIMENTO, 'mm') = '01' then 'JANEIRO' || ' ' || to_char(a.DATA_MOVIMENTO, 'yyyy')
                   when to_char(a.DATA_MOVIMENTO, 'mm') = '02' then 'FEVEREIRO' || ' ' || to_char(a.DATA_MOVIMENTO, 'yyyy')
                   when to_char(a.DATA_MOVIMENTO, 'mm') = '03' then 'MARÇO' || ' ' || to_char(a.DATA_MOVIMENTO, 'yyyy')
                   when to_char(a.DATA_MOVIMENTO, 'mm') = '04' then 'ABRIL' || ' ' || to_char(a.DATA_MOVIMENTO, 'yyyy')
                   when to_char(a.DATA_MOVIMENTO, 'mm') = '05' then 'MAIO' || ' ' || to_char(a.DATA_MOVIMENTO, 'yyyy')
                   when to_char(a.DATA_MOVIMENTO, 'mm') = '06' then 'JUNHO' || ' ' || to_char(a.DATA_MOVIMENTO, 'yyyy')
                   when to_char(a.DATA_MOVIMENTO, 'mm') = '07' then 'JULHO' || ' ' || to_char(a.DATA_MOVIMENTO, 'yyyy')
                   when to_char(a.DATA_MOVIMENTO, 'mm') = '08' then 'AGOSTO' || ' ' || to_char(a.DATA_MOVIMENTO, 'yyyy')
                   when to_char(a.DATA_MOVIMENTO, 'mm') = '09' then 'SETEMBRO' || ' ' || to_char(a.DATA_MOVIMENTO, 'yyyy')
                   when to_char(a.DATA_MOVIMENTO, 'mm') = '10' then 'OUTUBRO' || ' ' || to_char(a.DATA_MOVIMENTO, 'yyyy')
                   when to_char(a.DATA_MOVIMENTO, 'mm') = '11' then 'NOVEMBRO' || ' ' || to_char(a.DATA_MOVIMENTO, 'yyyy')
                   when to_char(a.DATA_MOVIMENTO, 'mm') = '12' then 'DEZEMBRO' || ' ' || to_char(a.DATA_MOVIMENTO, 'yyyy') end) mes
                   
                   ,to_date(to_char(a.DATA_MOVIMENTO, 'mm/yyyy'), 'mm/yyyy') as data
       from ESTQ_300_ESTQ_310 a
      ) consulta
group by consulta.data
order by consulta.data desc

e ao invés de levar 40 secs, levou 16,45... está aceitável.

obrigado!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Este max com o mês por extenso ...

 

Novembro será maior que dezembro ...

 

 

Não entendi bem o que se precisa, creio ...

Compartilhar este post


Link para o post
Compartilhar em outros sites

tenho uma tabela que contém as datas de movimentação do estoque.

então lá tenho gravado assim:

 

DATA_MOVIMENTO

01/10/2014

05/10/2014

05/10/2014

09/10/2014

25/10/2014

13/11/2014

16/11/2014

17/11/2014

17/11/2014

05/12/2014

03/01/2015

07/01/2015

 

então preciso montar no meu combobox, uma lista de períodos em cima destas datas, devendo ficar assim neste exemplo:

JANEIRO 2015

DEZEMBRO 2014

NOVEMBRO 2014

OUTUBRO 2014

 

o resultado do meu sql já traz essas informações e nesta ordem, porém demora muito.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Select distinct to_char(data_movimento,'MMM YYYY')

from ESTQ_300_ESTQ_310

 

Só não lembro do formato para a data em extenso em português.

A query tende a ser demorada pois lê a tabela toda.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Obrigado Motta, consegui algo aqui:

select to_date(to_char(consulta.data_movimento,'mm/yyyy'),'mm/yyyy') data,
  (case when to_char(consulta.DATA_MOVIMENTO, 'mm') = '01' then 'JANEIRO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '02' then 'FEVEREIRO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '03' then 'MARÇO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '04' then 'ABRIL' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '05' then 'MAIO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '06' then 'JUNHO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '07' then 'JULHO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '08' then 'AGOSTO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '09' then 'SETEMBRO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '10' then 'OUTUBRO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '11' then 'NOVEMBRO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '12' then 'DEZEMBRO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy') end) mes
from
  (select max(DATA_MOVIMENTO) as DATA_MOVIMENTO from ESTQ_300_ESTQ_310 a
  group by DATA_MOVIMENTO) consulta
group by to_date(to_char(consulta.data_movimento,'mm/yyyy'),'mm/yyyy'),
  (case when to_char(consulta.DATA_MOVIMENTO, 'mm') = '01' then 'JANEIRO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '02' then 'FEVEREIRO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '03' then 'MARÇO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '04' then 'ABRIL' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '05' then 'MAIO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '06' then 'JUNHO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '07' then 'JULHO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '08' then 'AGOSTO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '09' then 'SETEMBRO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '10' then 'OUTUBRO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '11' then 'NOVEMBRO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy')
        when to_char(consulta.DATA_MOVIMENTO, 'mm') = '12' then 'DEZEMBRO' || ' ' || to_char(consulta.DATA_MOVIMENTO, 'yyyy') end)
order by to_date(to_char(consulta.data_movimento,'mm/yyyy'),'mm/yyyy') desc

Está demorando 5 segundos. podemos fechar o tópico, agora está aceitável.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.