【发布时间】:2015-05-24 08:27:26
【问题描述】:
我有 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?
谢谢
【问题讨论】:
-
第一个查询只选择表中的一行,第二个查询选择所有行。将所有行从数据库复制到 PHP 需要更长的时间。