PHP Slow - Code or Hosting? [closed]

1

I'm having frequent problems with PHP, maybe because I'm creating it in a "simple" way, or would it be my hosting that is basic?

I use the Hosting Plan I of Locaweb.

I am creating a system that will do two SELECTs in SQL Server, picking up all the zip codes of two different months, and then comparing them.

In units where they return fewer results, the program works correctly. But in units that return is much larger, the page crashes with error 503.

Index.php

<?
function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

function cmp($a, $b) {
    $cep = preg_replace("/[^0-9\s]/", "", $a['cep']);
    $cepCompara = preg_replace("/[^0-9\s]/", "", $b['cep']);
 return $a['cep'] > $b['cep'];
}

$mes = $_GET['mes'];
$mesAnt = $mes-1;
$unidade = $_GET['unidade'];


    $dbhost   = "HOST HEHE";
    $db       = "BANCO";
    $user     = "USER";
    $password = "PASS";

    mssql_connect($dbhost,$user,$password) or die("Não foi possível a conexão com o servidor!");
    mssql_select_db("$db") or die("Não foi possível selecionar o banco de dados!");

    $sql = "SELECT CtrNro, CtrDstNroCep, CtrDat, CtrQtdPeso, CtrValFreteBase
            FROM rlt005
            WHERE CtrUnnCodDestino = $unidade
            AND DATEPART(MONTH, CtrDat) = $mesAnt";

    $consulta = mssql_query($sql);
    $numRegistros = mssql_num_rows($consulta);
    $mesUm = array();
    if ($numRegistros!=0) {
        while ($cadaLinha = mssql_fetch_array($consulta)) {
            $dados = array(
                "operacional" => $cadaLinha['CtrNro'],
                "cep" => $cadaLinha['CtrDstNroCep'],
                "data" => substr($cadaLinha['CtrDat'],0,3),
                "peso" => $cadaLinha['CtrQtdPeso'],
                "frete" => $cadaLinha['CtrValFreteBase'],
            );
            array_push($mesUm, $dados);
        }
    }

    $sql = "SELECT CtrNro, CtrDstNroCep, CtrDat, CtrQtdPeso, CtrValFreteBase
            FROM rlt005
            WHERE CtrUnnCodDestino = $unidade
            AND DATEPART(MONTH, CtrDat) = $mes";

    $consulta = mssql_query($sql);
    $numRegistros = mssql_num_rows($consulta);
    $mesDois = array();
    if ($numRegistros!=0) {
        while ($cadaLinha = mssql_fetch_array($consulta)) {
            $dados = array(
                "operacional" => $cadaLinha['CtrNro'],
                "cep" => $cadaLinha['CtrDstNroCep'],
                "data" => substr($cadaLinha['CtrDat'],0,3),
                "peso" => $cadaLinha['CtrQtdPeso'],
                "frete" => $cadaLinha['CtrValFreteBase'],
            );
            array_push($mesDois, $dados);
        }
    }

    $novo = array();
    $perda = array();
    $totalNovo=0;
    $totalPerda=0;

    $tam = sizeof($mesDois);
    for($i=0; $i < $tam; $i++){

        if (!in_array_r($mesDois[$i]['cep'], $mesUm)) {
            if(!in_array_r($mesDois[$i]['cep'], $novo)){
                $totalNovo++;
            }
            array_push($novo, $mesDois[$i]);
        }
    }

    $tam = sizeof($mesUm);
    for($i=0; $i < $tam; $i++){
        if (!in_array_r($mesUm[$i]['cep'], $mesDois)) {
            if(!in_array_r($mesUm[$i]['cep'], $perda)){
                $totalPerda++;
            }
            array_push($perda, $mesUm[$i]);
        }
    }

?>
<header>
        <section class="wrap-center clearfix">
            <div class="content-head">
                <h1>Unidade <?php echo $unidade; ?></h1>
                <p>Perdas e Ganhos do mês <?php echo $mes; ?> em relação ao mês <?php echo $mesAnt; ?></p>
            </div>
        </section>
    </header>
        <div class="container">
            <section class="card">
                <h3><i class="fa fa-long-arrow-down" aria-hidden="true"></i> <?php echo $totalPerda; ?></h3>

                  <div class="card-block">
                    <table class="table">
                        <thead>
                          <tr>
                            <th style="padding: 2px 4px 2px 17px !important;">●</th>
                            <th>Operacional</th>
                            <th>CEP</th>
                            <th>Peso (Kg)</th>
                            <th>Frete (R$)</th>

                          </tr>
                        </thead>
                        <tbody>
                         <?php
                         $tam = sizeof($perda);
                            for($i=0; $i < $tam; $i++){
                                echo '<tr>
                                <td style="padding:2px 0px 2px 17px !important;"><a href="#" onclick="popUP('.preg_replace("/^(\d{5})(\d{3})$/", "\1-\2", $novo[$i]['cep']).')"><i class="fa fa-long-arrow-down" aria-hidden="true"></i></a></td>
                                        <td>'.$perda[$i]['operacional'].'</td>
                                        <td>'.preg_replace("/^(\d{5})(\d{3})$/", "\1-\2", $perda[$i]['cep']).'</td>
                                        <td>'.$perda[$i]['peso'].'</td>
                                        <td>'.str_replace(".", ",", $perda[$i]['frete']).'</td>

                                    </tr>';
                            }
                         ?>
                        </tbody>
                      </table>
                  </div>

            </section>
            <section class="card">
                <h3><i class="fa fa-long-arrow-up" aria-hidden="true"></i> <?php echo $totalNovo; ?></h3>

                  <div class="card-block">
                    <table class="table">
                        <thead>
                          <tr>
                            <th style="padding: 2px 4px 2px 17px !important;">●</th>
                            <th>Operacional</th>
                            <th>CEP</th>
                            <th>Peso (Kg)</th>
                            <th>Frete (R$)</th>
                          </tr>
                        </thead>
                        <tbody>
                         <?php
                            $tam = sizeof($novo);
                            for($i=0; $i < $tam; $i++){
                                echo '<tr>

                                    <td style="padding:2px 0px 2px 17px !important;"><a href="#" onclick="popUP('.preg_replace("/^(\d{5})(\d{3})$/", "\1-\2", $novo[$i]['cep']).')"><i class="fa fa-long-arrow-up" aria-hidden="true"></i></a></td>

                                <td>'.$novo[$i]['operacional'].'</td>
                                        <td>'.preg_replace("/^(\d{5})(\d{3})$/", "\1-\2", $novo[$i]['cep']).'</td>
                                        <td>'.$novo[$i]['peso'].'</td>
                                        <td>'.str_replace(".", ",", $novo[$i]['frete']).'</td>

                                    </tr>';
                            }
                         ?>
                        </tbody>
                      </table>
                  </div>

            </section>
        </div>
    
asked by anonymous 10.07.2017 / 17:44

1 answer

2

The question is confusing but if you are trying to pick up two separate months ceps to compare which zip lost in the current month. You should mount a querie with just a select picking up all records from month 05 and using the NOT IN command to filter all records not held by month 06. The logic would be

  

" RECOVER ALL RECORDS OF THE MONTH 05 THAT ARE NOT CONTAINED IN THE MONTH 06 "

Simple example of use:

Select * from funcionario where CODFUNC not in (select CODFUNC from FILIAIS) 

You could put this together:

SELECT CtrNro, CtrDstNroCep, CtrDat, CtrQtdPeso, CtrValFreteBase
        FROM rlt005
        WHERE CtrUnnCodDestino = $unidade 
        AND DATEPART(MONTH, CtrDat) = '05'
        AND CtrDstNroCep not in 
        (SELECT CtrDstNroCep FROM rlt005 WHERE CtrUnnCodDestino = $unidade 
        AND DATEPART(MONTH, CtrDat) = '06');

I believe the query is correct, I do not have full understanding of its table structure. But that's the right way to do it, saving you the most features instead of using a pound of loops with php that takes up a lot of memory and CPU. Note that the CtrDat , CtrDstNroCep field should also be an index in your table to expedite queries.

    
10.07.2017 / 19:16