There are several factors why a code can go slow.
Notice that I said code and I did not say query , because sometimes the problem may not be the query.
However, your SQL statement is not optimized .
So the first thing in this case would be to optimize your SQL query. For this there is a first step that is the following: Do not use never SELECT *
, unless you are going to use all fields or columns of the table . Imagine a table with 20, 40, 50, 100 columns of which in a given query you will only need id, nombre, apellidos ...
, if you do SELECT *
, which I see unfortunately very often ... if your table has 50 columns and you only need 3, you would be unnecessarily selecting 47 columns with which you will not operate at that moment. Then: 1st optimization rule, no, no and no%% co
That the indexes of the tables are appropriate A poorly indexed table, even if it has 900 records, can start to be leeenta and end up being really descending as it grows. To determine if the indexes of the table are well established you can use SELECT *
At first it seems complicated, but through EXPLAIN SELECT columna1, column3, solocolumnaquenecesites FROM tabla WHERE ...
you will quickly see how the indexes of the table are working. It would be worth it to spend a little time. In this question and your answers you can understand the operation of EXPLAIN
clearly. It is an excellent ally to optimize our tables. Here when I speak of indexes I mean not only the primary and foreign keys, but also the indexes of uniqueness. For example when an index is wrong EXPLAIN
will tell you that your query instead of reading 1, 2, 5 ... rows is reading 900 or 10,000 or several million rows, especially when you join several tables by EXPLAIN
in your SQL statement (This is not the case here, but I think it is convenient to point it out.)
Wordpress or PDO-MySQLi to manage the database? I would in particular use PDO, and would process my data through prepared queries, stop avoiding the SQL injection , this is a broad subject but we have to know about the security of our data. I could not say it properly, but I'm pretty sure Wordpress would be slower handling our database than using the PDO drive itself. (It would be an interesting question here in SO).
The other part of the code that does not have to do with the query to the BD. It may be that the optimization problem is on the side of the code that handles the data already obtained, in this case the functions:
$clCliente = new acClientes();
$clCliente->formateaSalida($fila);
Or also here:
$array = array();
$i=0;
foreach($resultados as $fila){
$array[$i] = new acClientes();
$array[$i]->formateaSalida($fila);
$i++;
}
return $array;
It turns out that PDO has methods to return to our array of results in an array (or in many other ways). And it's another error that I see frequently in OS code, I mean I see code that reinvents the wheel , trying to do, sometimes with little success, something that the driver of the Database makes through its own methods.
P. D.:
If you decide for PDO here you have a class already made (although I have yet to improve it) that would serve you for the functions basic consultation and management of data safely.