1

I have 2 servers on different geographical locations Singapore and India.

I need to connect server 1 from a php script at server 2. script looks like this:

<?php 

echo microtime(true)."\n";
$con = mysql_pconnect('server1','username','password');

$db = mysql_select_db('database_name',$con);                      

echo microtime(true)."\n";

$q = "select * from tablename where id='35'";
$result = mysql_query($q);

echo microtime(true)."\n";

?>

The out put of this script is like this:

1373977322.9081
1373977324.377
1373977324.6625

As you can see the time between the 2nd and 3rd is around 2 seconds, which means mysql_pconnect is taking more 2 seconds. And time between 3rd and 4th(select query) is very less.

Also if I run this script on server1 connecting it to server1 itself it takes 20 ms.

Can't figure out why connection time is soo long. I have also tried some things like skip-name-resolve and persistent connections. but :(

How can I debug this thing???

4

2 回答 2

2

从计时可以看出,打开一个连接需要1.4689秒,查询需要0.2855秒。如果一次性 DNS 查找是唯一的问题,那么查询会快很多:300 毫秒是一个非常长的时间。这意味着问题一定出在其他地方。

当连接首次打开时,客户端和服务器会进行多次协商,其中一方向对方提出问题,然后多次等待回复。如果网络具有高延迟,则每个问答周期都会花费大量时间,并且会加起来。您可以ping用来测量两台机器之间的网络延迟。

这就是为什么您看到本地连接几乎没有延迟(低延迟)以及为什么在建立连接后查询运行速度很快(没有协商)。没有真正的解决方法,只要确保一旦建立连接就可以充分利用它,除非绝对必要,否则避免建立新的连接。

于 2013-07-16T12:57:02.870 回答
-1

首先,尝试用 PDO 做同样的事情,就像这个 php 代码:

echo microtime(true)."\n";
$con = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');    
echo microtime(true)."\n";    
$q = $con->query("select * from tablename where id='35'");    
echo microtime(true)."\n";

如果执行时间仍然相同,您将不得不做一个缓存系统来减少与数据库的连接数。

于 2013-07-16T12:44:09.947 回答