How to improve select performance

Asked

Viewed 698 times

0

I have to use Firebird, but I do not get along very well with database, I selected the following way:

select first 5 skip ((1 - 1) * 10) distinct
 PRO.CODIGO,
 PRO.NIVEL_INTERESSE,
 PRO.DATA_RETORNO,
 PRO.RESPONSAVEL_PROSPECT,
 PRO.EMPRESA_PROSPECT,
 PRO.CIDADE_IBGE,
 PRO.VENDEDOR,
 PRO.STATUS,
 PRO.TIPO_CONTATO,
 PRO.EMAIL,
 PRO.DATA_ALTERACAO,
 PRO.DATA_CADASTRO,
 VEN.NOME as NOME_VENDEDOR,
 PRO.TELEFONE,
 (select first 1
   CODIGO
   from CAD_PROSPECTOS_PROPOSTAS WHERE PROSPECTO = PRO.CODIGO
   order by CODIGO desc) as COD_PROPOSTA,
 (select first 1
   CODIGO
   from CAD_PROSPECTOS_AGENDAMENTOS
   where PROSPECTO = PRO.CODIGO
   order by CODIGO desc) as COD_AGENDAMENTO,
 (select first 1
   CODIGO
   from CAD_PROSPECTOS_CONTRATOS
   where PROSPECTO = PRO.CODIGO
   order by CODIGO desc) as COD_CONTRATO,
 (select first 1
   CONTRATO_CONFIRMADO
   from CAD_PROSPECTOS_CONTRATOS
   where PROSPECTO = PRO.CODIGO
   order by CODIGO desc) as CONTRATO_CONFIRMADO,
 (select first 1
   NOME
   from CAD_MUNICIPIOS
   where IBGE = PRO.CIDADE_IBGE) as NOME_MUNICIPIO
 from CAD_PROSPECTOS PRO
 left join CAD_TERCEIROS VEN on PRO.VENDEDOR = VEN.CODIGO and PRO.EMPRESA = 
 VEN.EMPRESA
 left join CAD_TERCEIROS_VENDEDORES CTV on PRO.CODIGO = VEN.CODIGO and 
 PRO.EMPRESA = VEN.CODIGO
 where PRO.EMPRESA = 1 and
       PRO.ESTABELEC = 1
 order by PRO.CODIGO desc  

It works perfectly and meets all my needs, only problem is it gets extremely slow. How could I improve this select so that it works faster and bringing me the same data.

  • those subselects degrade performance in general. I could not analyze in your specific case, but usually I try to get away from them when I can, even for performance issues (my focus is more SQL Server and Sqlite). But I’ve had to put on a subselect precisely for performance reasons...

  • I’m trying somehow to get these off sebselcts, maybe if I do it in two separate selects it will help improve performance.

1 answer

1


There are some forms, but it depends on the version of the Firebird you are running. I’ll tell you 3, if you have problems implementing them, send me the DDL or the fdb itself to recreate the bank on my machine and give you the select ready and tested.

1 - the inves of making subselect in the select clause, use derived query:

select tabela1.campo2, sub1.campo, ....
from tabela1,
     ( select campo
        from subtabela s
       where .....
      ) as sub1
where ....

2 - create stored procedures with a for:

 create procedure select_exemplo.....
 //....
  for
     select ...
      from ...
  do begin
    select subcampo
      from substabela
    where .....
    into :subcampo-retorno;     
    suspend;
  end

3 - use clausa with before select. See the following link in the 'Common Table Expressions' section ("WITH ... AS ... SELECT")'
https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-dml-select.html#fblangref25-dml-select-Cte

  • I really did through a trial is already helped a lot in the performance, thank you.

  • may be missing some Dice also..... optimizing queries really isn’t a cake recipe...eu que Agradeco.... and anyway, I’m at your service.

  • 1

    another important thing, eh q if you are still not using version 3 of Firebird, strongly recommend migration. In version 3 they changed the ODS, and implemented new forms/protocols of User, Compression and Encryption. In addition, they said that the timespan of the releases will decrease a lot, and the version 4 Jah will have another ODS.

  • Unfortunately I use version 2.5, on account of the company where I work, so it is not easy to make a change like this, but I appreciate the tip.

  • I understand perfectly... where I worked we had the same problem, with the aggravating use of udf, but I managed to do even with these and other difficulties.... if you need help, so tell me.... good luck, friend.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.