I have a query in which I consult a record which I do with ajax and I get the data in json. now my questions is how I could optimize the response time since for example the query takes about 4 seconds, the table that I consult has ten rows, then my question is whether the query or the code could be optimized
The query executed directly on phpmyadmin takes 0.0006 sec
Javascript
function datos_factura(){
var parametros =
{
"operacion":"datos_factura",
"numero" : $.trim($("#nro_factura").val()),
}
$.ajax({
data: parametros,
url: 'controlador_factura.php',
type: 'POST',
datatype:'json',
async:true,
success: function (response){
$(".busqueda2").addClass("hide");
console.log("ejecute datos factura");
$(".busqueda").addClass("hide");
console.log(response.rut_persona);
$("#fecha").val(response.fecha);
$("#lst_pago").val(response.id_pago);
$("#lst_categoria").val(response.id_area);
lst_curso(response.id_area,response.folio);
},beforeSend: function() {
$(".busqueda2").text("Buscando espere...");
$(".busqueda2").removeClass("hide");
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("pongase en contacto con el programador: "+errorThrown);
}
});
}
PHP driver
<?php
include_once 'clases/clase_participante.php';
include_once 'clases/clase_persona.php';
include_once 'clases/clase_usuario.php';
include_once 'clases/clase_factura.php';
error_reporting(E_ALL ^ E_NOTICE);
$obj=new Participantes();
$per= new Personas();
$usu= new Usuario();
$obj3= new Factura();
switch ($_POST["operacion"]) {
case 'datos_factura':
$datos=$obj3->datos_factura($_POST["numero"]);
header("Content-Type: application/json", true);
echo json_encode($datos);
break;
case 'datos_participante':
$obj3->rut=$_POST["rut"];
$datos=$obj3->datos_participante();
header("Content-Type: application/json", true);
echo json_encode($datos);
break;
case 'grabar':
$resultado=0;
$per->rut=$_POST["rut"];
$per->nombre=$_POST["nombre"];
$per->ap_m=$_POST["ap_m"];
$per->ap_p=$_POST["ap_p"];
$per->celular=$_POST["celular"];
$per->correo=$_POST["correo"];
$per->nro_calle=$_POST["nro_calle"];
$per->calle=$_POST["calle"];
$per->id_comuna=$_POST["id_comuna"];
$per->fecha=$_POST["f_nac"];
if ($obj3->validar_curso_empresa($_POST["rut_empresa"],$_POST["folio"])==0) {
if($obj3->validar_curso_par($_POST["rut"],$_POST["folio"])==0){
if ($usu->validar_rut($_POST["rut"])==0){
$resultado=$per->insertar_persona(1);
$obj->insertar_tipo($_POST["rut"],$_POST["id_tipo"]);
if (!empty($_POST["rut_empresa"])){
$obj->insertar_per_empresa($_POST["rut_empresa"],$_POST["rut"]);
}
echo $resultado;
}else{
$resultado=$per->modificar_persona();
$obj->modificar_empresa_par($_POST["rut_empresa"],$_POST["rut"],$_POST["empresa_persona"]);
$obj->modificar_tipo($_POST["id_tipo"],$_POST["persona_participante"]);
}
//insertar factura
$obj3->rut=$_POST["rut"];
$obj3->rut_empresa=$_POST["rut_empresa"];
$obj3->nro_fac=$_POST["nro_fac"];
$obj3->id_pago=$_POST["id_pago"];
$obj3->id_tipo=$_POST["id_tipo"];
$obj3->fecha=$_POST["fecha"];
$obj3->costo=$_POST["costo"];
$obj3->folio=$_POST["folio"];
$obj3->grabar_participante();
}else{
echo 11;
}
}else{echo 20;}
break;
}
?>
Function of php class
function datos_factura($nro){
$sql="SELECT cf.nro_factura as nro,(select df.folio from detalle_factura where df.nro_factura=nro limit 1)as folio,cf.id_pago
,DATE_FORMAT(fecha,'%d/%m/%Y') as fecha,cg.id_generico,cg.id_area
from comprobante_factura cf
INNER JOIN detalle_factura df on cf.nro_factura=df.nro_factura
INNER JOIN cursos c on c.folio=df.folio
INNER JOIN curso_generico cg on
cg.id_generico=c.id_generico
where cf.nro_factura=$nro"";
$resultado=mysqli_query($this->conexion,$sql);
$datos=mysqli_fetch_array($resultado);
return($datos);
}
Google chrome console