【发布时间】:2012-02-05 04:38:20
【问题描述】:
我网站上的一个页面加载时间过长 - 大约 2 秒。在整个代码中使用 microtime 之后,我发现了它花费的时间太长并请求帮助以加快速度。
我用来查找时间的代码是
echo "<BR>3 - ".(microtime(true)-$startTime); #How long it takes for this page to do
$sqldb=open_database(); #Open the database
echo "<BR>4 - ".(microtime(true)-$startTime); #How long it takes for this page to do
我的结果是
3 - 5.1140758991241
4 - 6.1172299385071
因此我推断打开数据库的功能需要 1 秒。 用于打开数据库的函数是
//This function is to open the database
function open_database() {
//Now we will connect to the database
$sqldb=mysql_connect($_SESSION['hostname'], $_SESSION['dbusername'], $_SESSION['password']) OR die ('Unable to connect to database! Please try again later.');
mysql_select_db($_SESSION['dbname'], $sqldb); #Select which database we wish to use - in this case the customer one
if(empty($sqldb)) { //If there is no database then we have an error - MASSIVE ERROR!
header('Location:'.$_SESSION['web_site'].'/error.php');
die();
}
return($sqldb);
}
请注意,mysql_connect 中使用了 SESSION 变量,因此当我更新到生产服务器时,我只需更改一个变量。预生产服务器上的主机名是“localLhost”。
所以我的问题是 - 为什么打开数据库需要这么长时间?请注意,因为我处于预生产阶段,所以数据库非常小。
【问题讨论】: