dsy48837 2009-07-12 15:29
浏览 116
已采纳

使用jQuery Ajax请求的可变响应速度

When I perform an asynchronous request with jQuery Ajax, sometimes the response is returned quickly in 800 ms, somtimes it's slow and the response is returned in 2.50s (avg), and sometimes it hangs, and just shows the loading images. I'm not sure if it's because of my PHP code or jQuery Ajax code. I send some values using jQuery Ajax:

function get_detail_product(obj)
{       
    var id = obj.id ; 
    var $this = jQuery('#'+id); 
    var Thumb = jQuery('#Scroller div.Container') ;
    jQuery.each(Thumb , function(){
        jQuery(this).css('border' , '#ccc 2px solid');
    });
    $this.parent().css('border' , '#ff8500 2px solid') ;
    var load_area = jQuery('.detail') ;
    //ajax request 
    load_area.html("");
    load_area.html('<div id="loading" style="margin-top:60px;margin-left:350px;"><img src="../images/loading.gif"><br>Loding ... </div>');
    jQuery.ajax({
        url: 'index.php?module=product&popup=on ',
        type: 'POST',
        data: 'pid=' + id ,         
        success: function(result) {
            jQuery('#response').remove();
            load_area.html(result);
            jQuery('#loading').fadeOut(500, function() {
                jQuery(this).remove();
            });
        }
    });
}

and in the PHP file I have the following code to retrieve the requested data:

//ajax requests 
if(isset($_POST['subcatid']) && is_numeric($_POST['subcatid']))
{
    $subcatid = $_POST['subcatid'] ;
    $products = $dbc->getAll("select * from xxproduct where xsubcatid='$subcatid'") ;
    //send result 
    echo '<table cellpadding="0" cellspacing="0" border="0" id="ScrollerTable"><tr>'; 
    foreach ($products as $p) : echo '<td><div style="border:#ccc 2px solid ; padding:0px;margin-top:20px ; margin-bottom:20px ; margin-left:8px ; margin-right:8px;" class="Container"><a href="javascript:void(0)" id="'.$p['xproductid'].'" onclick="get_detail_product(this)" ><img src="imgsize.phpw=100&h=100&img=../uploads/product/'.$p['xproductid'].'/'.$p['xproductid'].'__1.jpg" border="0"/></a><div style="background-color:#ccc ;text-align:center ; padding:5px; ">'.$p['xproductname'].'</div></div></td>';
    endforeach ;
    echo ' </tr></table>'; 
}

I wonder if you can find any mistakes in my code that cause the delay; additionally I'm using a PEAR DB object to interact with the database.

When i type the title of my question in this page, suggestions are returned in about 500 ms. Why is this ajax interaction so fast but mine is not?

  • 写回答

3条回答 默认 最新

  • douju8113 2009-07-12 15:39
    关注

    I'm assuming you're getting the response times (the 800ms - 2.5s you mentioned) via FireBug? If that's the case, then that's the time of the request itself. At that point, all of your developer-land JS has already executed and the only JS that's running is the jQuery code in the ajax() function.

    So I think you can be reasonably sure that it's your PHP code.

    What I would do is use some php microtime() calls and hit that script directly (from a browser or commandline, not via an ajax call) and print out the microtime result.

    Specifically, you'd add this at the top of your script:

    $start = microtime(true);
    

    And this at the end:

    echo "Time: " . (microtime(true) - $start);
    

    Then, try to isolate what params/etc are used during any consistently slow queries. As in most cases with CRUD apps, the DB is most often the culprit.

    Edit:

    After looking more closely at your code, there's nothing obviously wrong. What I wonder is if this is the only request that has such crazy response times. If it is, that suggests that the connection between your client and server is not the issue. That it's specifically this script.

    And by doing what I mentioned above--hitting the script directly via your browser rather than an ajax call--you can isolate the PHP code and then, by moving the placement of those microtime statements, isolate the specific lines of code.

    But if it were me, I'd focus first on that SQL Query. Most like you're not using any bytecode caching like APC. It's a great tool it's just not in such widespread use right now.

    So the only caching mechanism that code is using is probably the MySQL Query Cache. If there's no index on the subcatid field, it could be that MySQL is doing a table scan. But the next time you request that same query (with that same subcatid value), the results will be in the query cache and MySQL will return them instantly.

    You could test this out by isolation even further: Forget the AJAX call and the PHP code, just copy and paste that query, plug in some valid subcatid values, and run it directly into phpMyAdmin or the MySQL CLI or your favorite MySQL tool. If you're seeing intermittent performance as you plug-in new and different subcatid values, then you know where your problem lies.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘