Ajax 响应时间太长


Ajax response time too long

    <table class="table table-bordered table-hover">
                            <tbody>
                                <tr>                        
        <td><select  name="ser" id="ser" class="bs-select form-control" onchange="getPrice(this.value);" data-live-search="true" required="">
        <option value="">--Select--</option>
        <?php
        $queryprd=$db->execute("select * from product_add where productcode LIKE '%$q%' and delet='0'");
        while($result=$queryprd->fetch_assoc())
        {
        ?>
        <option value="<?php echo $result['productcode']."|".$result['productname'];?>"><?php echo $result['productcode']."&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;".$result['productname'];?></option>
        <?php
        }
        ?>
        </select></td>
        <td><input type="text" name="sale" id="saleprice"  class="form-control" autocomplete="off" placeholder="Price"  required=""/></td>
        <td><select class="form-control msre" name="measure[]" id="measure">
            </select></td>
                                </tr>
                            </tbody>
    </table>
<script>
        function getPrice(val)
{
   $.ajax({
     type: 'post',
     url: 'get_sales_price.php',
     data: {
       get_option:val
     },
     success: function (response) {
       document.getElementById("saleprice").value=response; 
       getquty(val);
       getmsur(val);
     }
   });
}
function getmsur(val)
{
   $.ajax({
     type: 'post',
     url: 'get_measure.php',
     data: {
       get_option:val
     },
     success: function (response) {
        document.getElementById("measure").innerHTML=response;
        }
   });
}

</script>

get_sales_price.php

<?php
session_start();
ob_start();
require "../model/configuration.php";
$option = $_POST['get_option'];
list($code,$pname) = explode("|", $option);
$name=$pname;
$cod=$code;
$_SESSION['prdcode']=$cod;
$querydep=$db->execute("select * from productsale_price where product_code='$cod'");
$pprce=$querydep->fetch_assoc();
$queryty=$db->execute("select * from product_add where productcode='$cod' and delet='0'");
$resultro=$queryty->fetch_assoc();
$_SESSION['prdtype']=$resultro['product_type'];
$_SESSION['mrp']=$pprce['mrp'];
echo $pprce['sale_price'];
?>

get_measure.php

<?php
session_start();
ob_start();
require "../model/configuration.php";
$option = $_POST['get_option'];
list($code,$pname) = explode("|", $option);
$name=$pname;
$cod=$code;
       $prdqty=$db->execute("select * from master_purchase where product_code='".$cod."' and delet='0' order by master_purchase_id DESC");//and vendor_id='".$_SESSION['vnid']."'
       $tqty=$prdqty->fetch_assoc();
?>
<option value="">--Select--</option><option value="Box" <?php if($tqty['measure']=='Box'){ echo "selected";}?>>Box</option><option value="Ml"<?php if($tqty['measure']=='M'){ echo "selected";}?>>Ml</option>

此代码运行良好,但我的问题是....当我从选择框中选择一个选项时,它的 id="ser"> 我在 onchange="getPrice(this.value(;"> 中调用一个函数它的工作完美 ajax 结果显示在指定字段中 有时 1 或 3 分钟 有时超过 5 分钟 如何减少时间浪费
请帮忙...

与其使用两个SQL来获取数据,不如使用一个SQL来获取数据,使用LEFT join这样可以节省一些时间。

<?php
    $querydep=$db->execute("select pp.mrp, pp.sale_price, pa.product_type
    from productsale_price As pp LEFT JOIN product_add As pa 
       ON pa.product_code = pp.product_code AND pa.delet='0' 
        where pp.product_code='$cod'");
    $pprce=$querydep->fetch_assoc();
    $_SESSION['prdtype']=$pprce['product_type'];
    $_SESSION['mrp']=$pprce['mrp'];
    echo $pprce['sale_price'];
?>
您是否

为表productsale_price、product_add和master_purchase创建了索引?

如果没有,则需要索引: - productsale_price product_code - product_add产品代码和 - master_purchase product_code

这应该可以解决问题!当然,我在这里假设您的SQL服务器在最佳条件下运行,而不是响应缓慢的原因!