【问题标题】:select from large data with database or public variable从带有数据库或公共变量的大数据中选择
【发布时间】: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 需要更长的时间。

标签: php arrays search


【解决方案1】:

有两个问题:

  1. 第一个版本只将选定的行从数据库传输到 PHP,第二个版本传输所有行。复制所有这些数据需要时间。

  2. 第一个版本可以利用索引来加快查找选定行的速度。第二个版本读取所有行。

【讨论】:

    【解决方案2】:

    加快速度:

    1. 为 CON_CATEGORY + CON_ID(或 CON_ID + CON_CATEGORY,取决于其他查询)创建和索引

    2. 更改查找代码:

    新代码:

    $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;
    }
    
    1. 第三种优化方法是结合这两种方法。首先,有一些统计数据很重要:同一个 id 需要多少次?例如,如果一个 ID 平均被请求 20 次,那么您只需要 10 次查询而不是 200 次。

    喜欢:

    $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;
    }
    

    【讨论】:

    • 谢谢,这种方式很好,现在运行代码 800ms (wwwooowww)。
    【解决方案3】:

    另外,第二个版本需要遍历(foreach)所有元素。这需要更多时间。

    【讨论】:

      【解决方案4】:

      将 CON_CATEGORY 列添加到表的索引

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-12
        • 2021-11-10
        • 2010-10-30
        • 1970-01-01
        • 2021-02-23
        • 1970-01-01
        • 2015-04-12
        • 1970-01-01
        相关资源
        最近更新 更多