I have this function below that executes this SQL, but the execution time of this SQL is too high would I have any way to decrease the execution time? At the time of loading the page takes about 5 seconds to execute the query below. Would you like to reduce this time somehow?
public function ListaPorTipoP($obj){
$results = array();
$stmt = $this->conn->prepare
('select
case when exists (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase) Then (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase)
else null
end as [DsCavalo],
B.ID,
B.NrPlaca,
case when exists (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase) Then (select top 1 A.DtProg from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase)
else B.DtProg
end as [DtProg],
case when exists (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase) Then (select top 1 A.DsMotorista from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase)
else B.DsMotorista
end as [DsMotorista],
case when exists (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase) Then (select top 1 A.DtSaida from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase)
else B.DtSaida
end as [DtSaida],
case when exists (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase) Then (select top 1 A.HrSaida from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase)
else B.HrSaida
end as [HrSaida],
case when exists (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase) Then (select top 1 A.DtChegada from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase)
else B.DtChegada
end as [DtChegada],
case when exists (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase) Then (select top 1 A.HrChegada from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase)
else B.HrChegada
end as [HrChegada],
case when exists (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase) Then (select top 1 A.DsOrigem from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase)
else B.DsOrigem
end as [DsOrigem],
case when exists (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase) Then (select top 1 A.DsDestino from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase)
else B.DsDestino
end as [DsDestino],
case when exists (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase) Then (select top 1 A.DsAgendas from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase)
else B.DsAgendas
end as [DsAgendas],
case when exists (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase) Then (select top 1 A.DsCarga from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase)
else B.DsCarga
end as [DsCarga],
case when exists (select top 1 A.NrPlaca from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase) Then (select top 1 A.DtPrevChegDest from GTCLogist A
where A.DsTpVeiculo = \'Cavalo Truck\'
AND (A.NrPlacaCarreta=B.NrPlaca OR A.NrPlacaReboque2=B.NrPlaca)
and A.DtBase = B.DtBase)
else B.DtPrevChegDest
end as [DtPrevChegDest],
B.DtBase,
B.DsObservacao,
B.DsStatus
from GTCLogist B
where B.DsTpVeiculo like \'Carreta%\'
and B.DtBase = \''.self::convertData($obj->getdata()).'\'
and (exists (select top 1 H.NrPlaca from GTCLogist H
where H.DsTpVeiculo = \'Cavalo Truck\'
AND (H.NrPlacaCarreta=B.NrPlaca OR H.NrPlacaReboque2=B.NrPlaca)
and H.DtBase = B.DtBase
and H.DsDestino LIKE \''.$obj->getdest().'\')
OR (NOT exists (select top 1 K.NrPlaca from GTCLogist K
where K.DsTpVeiculo = \'Cavalo Truck\'
AND (K.NrPlacaCarreta=B.NrPlaca OR K.NrPlacaReboque2=B.NrPlaca)
and K.DtBase = B.DtBase)
AND B.DsDestino LIKE \''.$obj->getdest().'\'))
');
$stmt->execute();
if($stmt) {
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
$prog = new Prog();
$prog->setid($row->ID);
$prog->setst($row->DsStatus);
$prog->setplaca($row->NrPlaca);
$prog->setcavalo($row->DsCavalo);
$prog->setmot(stripslashes($row->DsMotorista));
$prog->setsaida(date('d/m/Y', strtotime($row->DtSaida)));
$prog->setorig($row->DsOrigem);
$prog->setdest($row->DsDestino);
$prog->setprev(date('d/m/Y', strtotime($row->DtPrevChegDest)));
$prog->setcarga($row->DsCarga);
$prog->setagen($row->DsAgendas);
$prog->setobs($row->DsObservacao);
$results[] = $prog;
}
}
return $results;
}
This SQL checks in the column Horse Truck if the horse owns some cart, if yes it brings all information of that cart if it does not have nor a cart it returns in white. Each else
is a select in each column to bring the information if there is a cart. That's rough talk.