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 asubselect
precisely for performance reasons...– Jefferson Quesado
I’m trying somehow to get these off
sebselcts
, maybe if I do it in two separate selects it will help improve performance.– Erick Zanetti