[firebase-br] RES: Query muuuiiiitttooo lenta (ainda)

Alysson Gonçalves de Azevedo agalysson em gmail.com
Dom Ago 5 20:28:00 -03 2012


Olha, sei que você quer otimizar a query, mas se nenhum santo estiver
ajudando, tenho uma sugestão...

Antes de tudo, a tabela ctarec é modificada com qual frequência? A todo
momento... De vez em quando... Raramente... ?

Pergunto isso porque talvez, em vez de fazer 12 selects para descobrir
counts e sums, crie 12 campos normais na tabela de cliente e atualize eles
com triggers na tabela ctarec sempre que tiver alguma alteração.

É provável que essa baixa performance da sql vai ser refletida na hora que
o usuário alterar algum registro, mas será poupado na hora que fizer
consultas simples, pois o select não terá mais subselects.

Alysson Gonçalves de Azevedo - (11) 8491-7730

"É curioso como as pessoas ficam confusas quando a frase não terminam do
jeito que elas periquito."



2012/8/3 Zottis <bzottis em ig.com.br>

> O Único indice que esta estranho é o Vcto_Original que esta retornando 1
> O resto esta bem abaixo de 1
>
>
> -----Mensagem original-----
> De: lista-bounces em firebase.com.br [mailto:lista-bounces em firebase.com.br]
> Em
> nome de marcelo
> Enviada em: sexta-feira, 3 de agosto de 2012 00:17
> Para: FireBase
> Assunto: Re: [firebase-br] Query muuuiiiitttooo lenta (ainda)
>
> zottis, verifique se você tem esses índices criados:
> CREATE INDEX IND_CTAREC_COD_CLIENTE ON CTAREC (COD_CLIENTE); CREATE INDEX
> IND_CTAREC_PAGO ON CTAREC (PAGO); CREATE INDEX IND_CTAREC_DELETADO ON
> CTAREC
> (DELETADO); CREATE INDEX IND_CTAREC_VENCIMENTO ON CTAREC (VENCIMENTO);
> CREATE DESCENDING INDEX IND_CTAREC_VENCIMENTOD ON CTAREC (VENCIMENTO);
> CREATE INDEX IND_CTAREC_DATA_PGTO ON CTAREC (DATA_PGTO); CREATE DESCENDING
> INDEX IND_CTAREC_DATA_PGTOD ON CTAREC (DATA_PGTO); CREATE INDEX
> IND_CTAREC_DATA_COLETA ON CTAREC (DATA_COLETA); CREATE DESCENDING INDEX
> IND_CTAREC_DATA_COLETAD ON CTAREC (DATA_COLETA); CREATE INDEX
> IND_VALORES_CLIENTE_COD_CLIENTE ON VALORES_CLIENTE(COD_CLIENTE); CREATE
> INDEX IND_FAR_MTR_COD_CLIENTE ON FAT_MTR (COD_CLIENTE);
>
>
> Em 02/08/2012 23:02, Zottis escreveu:
> > select C.COD_CLIENTE,
> >
> > coalesce(
> >
> > (select sum(CR.saldo) from ctarec CR where CR.COD_CLIENTE = :COD_CLIENTE
> and
> > CR.Pago = 'N' and CR.deletado = 'N' ),0) as TotalaPagar ,
> >
> >
> >
> > coalesce(
> >
> > (select sum(CR.VALOR + CR.VLR_JUROS + CR.DESPESAS_EXTRAS - CR.DESCONTO)
> from
> > ctarec CR where CR.COD_CLIENTE = :COD_CLIENTE and CR.deletado = 'N'),0)
> as
> > TotalContas ,
> >
> >
> >
> > coalesce(
> >
> > (select sum(CR.saldo) from ctarec CR where CR.COD_CLIENTE = :COD_CLIENTE
> and
> > CR.Pago = 'N' and CR.vencimento >= current_date and CR.deletado = 'N'
> ),0)
> > as TotalAbertas,
> >
> >
> >
> > coalesce(
> >
> > (select sum(CR.saldo) from ctarec CR where CR.COD_CLIENTE = :COD_CLIENTE
> and
> > CR.Pago = 'N' and CR.vencimento < current_date and CR.deletado = 'N' ),0)
> as
> > TotalVencidas,
> >
> >
> >
> > coalesce(
> >
> > (select Count(CR.COD_CONTA) from ctarec CR where CR.COD_CLIENTE =
> > :COD_CLIENTE and CR.Pago = 'S' and
> >
> > CR.Data_Pgto <= CR.Vencimento and CR.deletado = 'N' ),0) as NoPontuais,
> >
> >
> >
> > coalesce(
> >
> > (Select count(CR.COD_CONTA) FROM Ctarec cr where
> >
> >    CR.COD_CLIENTE = :COD_CLIENTE and CR.Pago = 'S' and CR.deletado = 'N'
> ),0)
> > as NoCtasPagas,
> >
> >
> >
> > coalesce(
> >
> > (Select Count(CR.COD_CONTA) FROM Ctarec cr where
> >
> > CR.COD_CLIENTE = :COD_CLIENTE and CR.Pago = 'S' and CR.DATA_PGTO >
> > CR.VENCIMENTO and CR.deletado = 'N' ),0) as NoAtrasoPorcento,
> >
> >
> >
> > coalesce(
> >
> > (select Sum(CR.DIAS_ATRASO) from Ctarec cr where
> >
> > CR.COD_CLIENTE = :COD_CLIENTE and CR.Pago = 'S' and CR.deletado = 'N'
> ),0)
> > as AtrasoDias,
> >
> >
> >
> > coalesce(
> >
> > (select count(CR.COD_CONTA) from Ctarec cr where
> >
> > CR.COD_CLIENTE =  :COD_CLIENTE and CR.Pago = 'N' and CR.deletado = 'N'
> ),0)
> > as NoCtasNaoPagas,
> >
> >
> >
> > coalesce(
> >
> > (select count(CR.COD_CONTA) from ctarec CR where CR.COD_CLIENTE =
> > :COD_CLIENTE and CR.Pago='N' and CR.vencimento >= current_date and
> > CR.deletado = 'N' ),0) as NoNaovencidas,
> >
> >
> >
> > coalesce(
> >
> > (select count(CR.COD_CONTA) from ctarec CR where CR.COD_CLIENTE =
> > :COD_CLIENTE and CR.Pago = 'N' and CR.vencimento < current_date and
> > CR.deletado = 'N' ),0) as noVencidas,
> >
> >
> >
> >      min(F.Data_Coleta) as Primeira_Coleta,
> >
> >      max(F.Data_Coleta) as ultima_Coleta,
> >
> >
> >
> > coalesce(
> >
> > (select first 1(V.codigo) from valores_cliente V where V.cod_cliente =
> > :COD_CLIENTE) ,0) as COD_VALOR
> >
> >
> >
> >
> >
> > from clientes C
> >
> > left join Far_Mtr  F on C.cod_cliente = F.cod_cliente
> >
> > left join ctarec r   on c.cod_cliente = r.cod_cliente
> >
> >
> >
> > where C.deletado = 'N' and C.cod_cliente = :COD_CLIENTE
> >
> > group by C.cod_cliente
>
>
> ______________________________________________
> FireBase-BR (www.firebase.com.br) - Hospedado em www.locador.com.br
> Para saber como gerenciar/excluir seu cadastro na lista, use:
> http://www.firebase.com.br/fb/artigo.php?id=1107
> Para consultar mensagens antigas: http://firebase.com.br/pesquisa
>
>
> ______________________________________________
> FireBase-BR (www.firebase.com.br) - Hospedado em www.locador.com.br
> Para saber como gerenciar/excluir seu cadastro na lista, use:
> http://www.firebase.com.br/fb/artigo.php?id=1107
> Para consultar mensagens antigas: http://firebase.com.br/pesquisa
>



Mais detalhes sobre a lista de discussão lista