我的网站上的某个页面加载时间过长 - 大约2秒。在整个代码中使用microtime之后,我已经计算出它花费的时间太长,并要求如何加速它的帮助。php,mysql,开放数据库花费时间过长
我用于查找倍的代码是
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,所以我只需要改变一个变量,当我更新到生产服务器。主机名是预生产服务器上的'locaLhost'。
所以我的问题是 - 为什么打开数据库需要这么久?请注意,因为我处于试生产阶段,所以数据库非常小。
注意:会话*可能*存储在文件系统中。不同的后端可用。 – Borealid 2012-02-05 04:44:09
是的,但通常根据我的经验,这些不同的后端在数据库中。由于他没有连接到数据库,我认为他会将会话存储在文件系统中。 – thenetimp 2012-02-05 04:48:19
实际上,如果他在访问会话存储时遇到性能问题,我推荐的第一件事就是使用memcached支持的会话... – Borealid 2012-02-05 04:50:08