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

时间:2022-01-18 20:56:10

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:

当我使用jQuery Ajax执行异步请求时,有时响应会在800毫秒内快速返回,有时响应很慢,响应以2.50秒(平均)返回,有时会挂起,只显示加载图像。我不确定是不是因为我的PHP代码或jQuery Ajax代码。我使用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:

在PHP文件中,我有以下代码来检索请求的数据:

//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.

我想知道你是否可以在我的代码中发现任何导致延迟的错误;另外我使用PEAR DB对象与数据库进行交互。

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?

当我在此页面中输入我的问题的标题时,建议将在大约500毫秒内返回。为什么这个ajax交互这么快但我的不是?

3 个解决方案

#1


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.

我假设你通过FireBug获得了响应时间(你提到的800毫秒--2.5秒)?如果是这种情况,那就是请求本身的时间。那时,你的所有开发者JS已经执行了,并且唯一运行的JS是ajax()函数中的jQuery代码。

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

所以我认为你可以合理地确定它是你的PHP代码。

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.

我要做的是使用一些php microtime()调用并直接命中该脚本(从浏览器或命令行,而不是通过ajax调用)并打印出microtime结果。

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.

然后,尝试隔离在任何一致的慢查询期间使用的params / etc。与大多数使用CRUD应用程序的情况一样,数据库通常是罪魁祸首。

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.

通过执行上面提到的操作 - 直接通过浏览器而不是ajax调用来访问脚本 - 您可以隔离PHP代码,然后通过移动这些微时间语句的位置来隔离特定的代码行。

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.

但如果是我,我会首先关注SQL查询。最喜欢你没有像APC那样使用任何字节码缓存。它是一个很棒的工具,现在还没有广泛使用。

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.

所以代码使用的唯一缓存机制可能是MySQL Query Cache。如果subcatid字段上没有索引,则可能是MySQL正在进行表扫描。但是下次您请求相同的查询(具有相同的subcatid值)时,结果将在查询缓存中,MySQL将立即返回它们。

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.

您可以通过隔离进一步测试:忘记AJAX调用和PHP代码,只需复制并粘贴该查询,插入一些有效的subcatid值,然后直接运行到phpMyAdmin或MySQL CLI或您喜欢的MySQL工具中。如果您在插入新的和不同的subcatid值时看到间歇性的表现,那么您就知道问题所在。

#2


Possibly caching both in the browser and any php mem-caching going on.

可能在浏览器和任何php mem-caching中都进行缓存。

#3


Network latency can have a huge impact on your ajax calls, particularly if one side or the other doesn't have a consistent internet connection. Since the problem seems to be intermittent I'd suggest looking at network or cacheing as the likely source.

网络延迟会对您的ajax呼叫产生巨大影响,特别是如果一方或另一方没有一致的互联网连接。由于问题似乎是间歇性的,我建议将网络或缓存视为可能的来源。

#1


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.

我假设你通过FireBug获得了响应时间(你提到的800毫秒--2.5秒)?如果是这种情况,那就是请求本身的时间。那时,你的所有开发者JS已经执行了,并且唯一运行的JS是ajax()函数中的jQuery代码。

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

所以我认为你可以合理地确定它是你的PHP代码。

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.

我要做的是使用一些php microtime()调用并直接命中该脚本(从浏览器或命令行,而不是通过ajax调用)并打印出microtime结果。

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.

然后,尝试隔离在任何一致的慢查询期间使用的params / etc。与大多数使用CRUD应用程序的情况一样,数据库通常是罪魁祸首。

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.

通过执行上面提到的操作 - 直接通过浏览器而不是ajax调用来访问脚本 - 您可以隔离PHP代码,然后通过移动这些微时间语句的位置来隔离特定的代码行。

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.

但如果是我,我会首先关注SQL查询。最喜欢你没有像APC那样使用任何字节码缓存。它是一个很棒的工具,现在还没有广泛使用。

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.

所以代码使用的唯一缓存机制可能是MySQL Query Cache。如果subcatid字段上没有索引,则可能是MySQL正在进行表扫描。但是下次您请求相同的查询(具有相同的subcatid值)时,结果将在查询缓存中,MySQL将立即返回它们。

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.

您可以通过隔离进一步测试:忘记AJAX调用和PHP代码,只需复制并粘贴该查询,插入一些有效的subcatid值,然后直接运行到phpMyAdmin或MySQL CLI或您喜欢的MySQL工具中。如果您在插入新的和不同的subcatid值时看到间歇性的表现,那么您就知道问题所在。

#2


Possibly caching both in the browser and any php mem-caching going on.

可能在浏览器和任何php mem-caching中都进行缓存。

#3


Network latency can have a huge impact on your ajax calls, particularly if one side or the other doesn't have a consistent internet connection. Since the problem seems to be intermittent I'd suggest looking at network or cacheing as the likely source.

网络延迟会对您的ajax呼叫产生巨大影响,特别是如果一方或另一方没有一致的互联网连接。由于问题似乎是间歇性的,我建议将网络或缓存视为可能的来源。