Slowness in generating reports

Asked

Viewed 192 times

2

Well I’m having some bottlenecks in my application and I’m not identifying the problem. I will try to explain as much as possible what happens and the configuration I use.

Server settings

I use a Jelastic server with 2 environments. One running php 5.6 and the other running mysql 5.6.24. With this I have separate environments, improving performance since each server has its own processor and memory.

Site settings

Php performs a simple query query, but the user has many records, which can reach as many as 20,000 records per report. So this report takes some time to be finalized but without any problem.

Description of the problem

When a user generates the report it takes on average 1 to 3 minutes to complete, due to the large amount of data, but the whole system is slow, even for other people who are not on the same network. The whole server takes time to respond and the situation normalizes only after the end of the process of generating the report. Jelastic provides graphics to track memory and processor consumption. I follow them but the load does not reach half of what I have configured in my environment, ie does not seem to be problem of little memory or processor. Is there any algae thing I’m forgetting to set up or check out? It is possible to make the system control the resources for each connection (user) so that this does not occur anymore?

———————————————————— Edit ————————————————

Good to make it easier I’ll post the way I’m assembling the report. The problem occurs in the back end when I’m assembling the array.

1 º I have 3 tables, being them: cadastro,f_pagamento and pedidos.

2º Mount the array like this:

// Consultation in the comic book $query = $connection->query(" select * from requests Where date >= '01-07-2017' and data <= '27-07-2017' ORDER BY date desc ");

// Lista resultados
while ($resultado = mysqli_fetch_object($consulta)) {

    // Verifica se tem algum vendedor se não tiver coloca null no array
    if ($resultado->id_vendedor !== null) {

        // Consulta cadastro
        $consulta_vendedor = $conexao->query("select nome from cadastro where id = '$resultado->id_vendedor' LIMIT 1");
        $resultado_vendedor = mysqli_fetch_object($consulta_vendedor);
        $vendedor_nome = $resultado_vendedor->nome;
    } else {
        $vendedor_nome = null;
    }

    // Verifica se tem forma de pagamento se não tiver coloca null no array
    if ($resultado->id_pagamento !== null) {

        // Consulta forma de pagamento
        $consulta_forma = $conexao->query("select forma from f_pagamento where id = '$resultado->id_pagamento' LIMIT 1");
        $resultado_forma = mysqli_fetch_object($consulta_forma);
        $nome_forma = $resultado_forma->forma;
    } else {
        $nome_forma = null;
    }

    // Verifica se tem cliente se não tiver coloca null no array
    if ($resultado->id_cliente !== null) {

        // Consulta nome do cliente
        $consulta_cliente = $conexao->query("select nome from cadastro where id = '$resultado->id_cliente' LIMIT 1");
        $resultado_cliente = mysqli_fetch_object($consulta_cliente);
        $nome_cliente = $resultado_cliente->nome;
    } else {
        $nome_cliente = null;
    }

   // Inicia variáveis
    $total = 0;

    // Consulta os produtos no pedido
    $consulta = Query($mysqli, "select valor,qtd from produtos_pedidos where id_pedido = '$resultado->id'");
    while ($resultado = mysqli_fetch_object($consulta)) {

        // Calculo
        $total += $resultado->qtd * $resultado->valor;
    }


    // Array com dados dos produtos
    $pedidos[] = array(
        "id" => $resultado->id,
        "folha_pg" => $resultado->id_folha_pagamento,
        "vendedor" => $vendedor_nome,
        "cliente" => $nome_cliente,
        "data" => $resultado->data,
        "forma_pg" => $nome_forma,
        “total” => $total
    );  
}

NOTE: The result is around 20,000 orders.

The table cadastro has about 3,600 records The table f_pagamento has about 100 records The table pedidos has about 50.00 records but I am only filtering the results for 1 month. The table produtos_pedidos has on average 10 products for each order

I noticed that from removing the full field of the array the report does not lock, it gets slow but does not lock. So the problem should be in this part:

   // Inicia variáveis
    $total = 0;

    // Consulta os produtos no pedido
    $consulta = Query($mysqli, "select valor,qtd from produtos_pedidos where id_pedido = '$resultado->id'");
    while ($resultado = mysqli_fetch_object($consulta)) {

        // Calculo
        $total += $resultado->qtd * $resultado->valor;
    }
  • Will not be your query could not be optimized, Indexes, etc ?

  • 1

    The problem is that even so, I have to prevent the system from slowing down for everyone, has some way of doing this?

  • Put your query here. Maybe it is not a processor/memory problem but the query optimization. 20k of results was for Mysql to print

  • Another question, your program makes a connection to bring these 20k of results or it accesses one by one?

  • I use only one connection, and by it I pass the query.

  • Your query does some JOIN?

  • No, I’ll post my query here early morning.

  • @Brunofolle edited my question with a full example of the query and code in php, if you can help me I will be very grateful. Thank you.

  • The problem "is and is not the query". I had a similar problem to assemble a report on a production site. Each loop of your while it queries once in the bank, that is, if your first query has 20k of results, it will make 20k of queries in your Tables. That’s what’s weighing on your report. Have you tried or thought about using Join, left Join and right Join?

  • The communication of PHP with mysql when there are several queries as well as yours, tend to be a problem because the answer between the two is slow and ends up locking or taking the sql out of the air. What I did to get around this problem in my case was to make a query return all the necessary data and then tidy up this data via PHP. It took work, but it got super fast.

  • You can try using Procedure http://www.devmedia.com.br/stored-procedures-no-mysql/29030

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.