我的表内容包含100,000条记录,我将函数getNameProcess调用了200次左右
从数据库中选择的PHP代码
function getNameProcess($id)
{
$time1=microtime(true);
$baseClass = new MsDatabase();
$query = "select CON_VALUE,CON_ID,CON_CATEGORY from content where CON_ID=$id and CON_VALUE<>'' and CON_CATEGORY='PRO_TITLE'";
$res= $baseClass->query($query,WF_WORKFLOW_DB_NAME);
$time2=microtime(true);
$timeTotal=($time2-$time1);
echo $timeTotal;
return $res[0]["CON_VALUE"];
}
从公共变量中选择的PHP代码
$contentTable=array();
function getNameProcess($id)
{
$time1=microtime(true);
$baseClass = new MsDatabase();
if(empty($GLOBALS['contentTable']))
{
$query = "select CON_VALUE,CON_ID,CON_CATEGORY from content ";
$GLOBALS['contentTable']= $baseClass->query($query,WF_WORKFLOW_DB_NAME_MARKAZE);
}
foreach($GLOBALS['contentTable'] as $R)
{
if($R['CON_ID']==$id && $R['CON_VALUE']!='' && $R['CON_CATEGORY']=='PRO_TITLE' )
{
$time2=microtime(true);
$timeTotal=($time2-$time1);
echo $timeTotal;
return $R["CON_VALUE"];
}
}
return 0;
}
使用数据库获取进程名称时$totalTime
为1.2秒,使用公共变量totalTime为3.5秒时?
为什么我使用公共变量$totalTime
大于使用数据库时?
如何减少$totalTime
?
谢谢
为了加快速度:
为CON_CATEGORY + CON_ID(或CON_ID + CON_CATEGORY,具体取决于其他查询)创建并编制索引
更改您的查找代码:
新代码:
$contentTable=array();
function getNameProcess($id)
{
$time1=microtime(true);
if(empty($GLOBALS['contentTable']))
{
$baseClass = new MsDatabase();
$query = "select CON_VALUE,CON_ID from content WHERE CON_CATEGORY = 'PRO_TITLE'";
$result= $baseClass->query($query,WF_WORKFLOW_DB_NAME_MARKAZE);
$GLOBALS['contentTable'] = array();
foreach($result as $R) {
if ($R['CON_VALUE'] != '') $GLOBALS['contentTable'][$R['CON_ID']] = $R['CON_VALUE'];
}
}
$retval = 0;
if (isset($GLOBALS['contentTable'][$id])) {
$retval = $GLOBALS['contentTable'][$id];
}
$time2=microtime(true);
$timeTotal=($time2-$time1);
echo $timeTotal;
return $retval;
}
喜欢:
$contentTable=array();
function getNameProcess($id)
{
$time1=microtime(true);
if (isset($GLOBALS['contentTable'][$id])) {
$retval = $GLOBALS['contentTable'][$id];
}
else {
$baseClass = new MsDatabase();
$query = "select CON_VALUE,CON_ID,CON_CATEGORY from content where CON_ID=$id and CON_CATEGORY='PRO_TITLE'";
$res= $baseClass->query($query,WF_WORKFLOW_DB_NAME);
$retval = ($res[0]['CON_VALUE'] == '' ? 0 : $res[0]['CON_VALUE']);
$GLOBALS['contentTable'][$res[0]['CON_ID']] = $retval;
}
$time2=microtime(true);
$timeTotal=($time2-$time1);
echo $timeTotal;
return $retval;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句