0
I have this function below that runs this SQL, but the running time of this SQL is too high would have some way to decrease the running time ? At the time of loading the page takes around 5 seconds to execute the query below. I would like to decrease this time in some way ?
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 has any cart, if yes it brings all information of this cart if it does not have even a cart it returns blank. Each else
is a select in each column to bring information if there is trailer. That roughly speaking.
Give a summary of what it does, what the tables are, etc. It will help us understand what needs to return and try to improve.
– Ricardo
Which bank?
– Reginaldo Rigo