I have to use firebird, but I do not get very well with the database, I made the select as follows:
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, the only problem is that it gets extremely slow. How could I improve this select so that it works faster and bringing me the same data.