【问题标题】:php script takes much time to loadphp脚本需要很长时间才能加载
【发布时间】:2018-07-10 10:17:12
【问题描述】:

我使用 Codeigniter 创建了一个网站,但遇到了与速度相关的问题。

所有其他功能都快得多,但我不知道为什么加载需要太多时间。

好吧,该操作只会执行一个查询,但等待时间太长了。

第一次加载网站时出现此问题。

// 控制器

function hardwareFolder($uId){
    $Folder = $this->System_model->get_folder_category_id($uId);
    $menu = [
        [
            'id' => 'all',
            'pId' => '1',
            'name' => _('All'),
            'icon' => base_url('images/directory_icon.gif'),
            'isParent' => true,
            'checked' => true,
            'nocheck' => true,
            'open' => true,
            'dirlevel' => 'main',
            'dirType' => 'all',
        ],
        [
            'id' => $Folder[0]['c_type_id'],
            'pId' => $Folder[0]['c_type_pid'],
            'name' => 'Unfiled',
            'icon' => base_url('images/unfield.png'),
            'isParent' => true,
            'checked' => true,
            'nocheck' => true,
            'open' => true,
            'dirlevel' => 'main',
            'dirType' => 'unfiled',
        ],
        [
            'id' => $Folder[1]['c_type_id'],
            'pId' => $Folder[1]['c_type_pid'],
            'name' => _('Shared'),
            'icon' => base_url('images/sharedir.png'),
            'isParent' => true,
            'checked' => true,
            'nocheck' => true,
            'open' => true,
            'dirlevel' => 'main',
            'dirType' => 'shared',
        ],
    ];
    return $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($menu));
}

// 模型

function get_folder_category_id($uid){
     $q_s_type = "SELECT c_type_id,c_type_pid,c_type_name " .  
                    "FROM `codeigniter_system_category_type` " .  
                    "   WHERE `c_type_pid` = 0 " . 
                          "AND `c_type_name` = 'Others' " . 
                          "AND `c_module` = 'System' " . 
                          "AND `c_created_userID` = ".$uid . 
                    " UNION SELECT c_type_id,c_type_pid,c_type_name ". 
                    "    FROM `codeigniter_system_category_type` " .
                           "WHERE `c_type_pid` = 0 " . 
                               "AND `c_type_name` = 'Shared' " . 
                               "AND `c_module` = 'System' " .
                               "AND `c_created_userID` = ".$uid;

     return $this->db->query($q_s_type)->result_array();
 }

使用 Ajax 进行脚本调用。

【问题讨论】:

  • 您是否使用例如EXPLAIN 测试过查询?您是否验证过查询确实是问题所在?
  • 分享更多信息。这是在某种循环中吗?显示了多少条记录?
  • @jeroen 我已经在 mysql 中测试过这个查询,它只需要 0.00007 秒。
  • @Akintunde007 没有循环,它只是从 ajax 调用,这里只触发一个查询并返回 Json。
  • 我给你的建议是检查你使用的函数以及它们与你的框架和php版本的兼容性。很久以前,我在使用 php 时遇到过同样的问题,在检查了一些功能后,我发现它导致了一些问题。

标签: php mysql ajax codeigniter


【解决方案1】:

除非我大错特错,否则唯一改变这两个SELECT 语句的就是检查c_type_name 的值

如果是这样,您可以简化 UNION,并通过从

function get_folder_category_id($uid){
 $q_s_type = "SELECT c_type_id,c_type_pid,c_type_name " .  
                "FROM `codeigniter_system_category_type` " .  
                "   WHERE `c_type_pid` = 0 " . 
                      "AND `c_type_name` = 'Others' " . 
                      "AND `c_module` = 'System' " . 
                      "AND `c_created_userID` = ".$uid . 
                " UNION SELECT c_type_id,c_type_pid,c_type_name ". 
                "    FROM `codeigniter_system_category_type` " .
                       "WHERE `c_type_pid` = 0 " . 
                           "AND `c_type_name` = 'Shared' " . 
                           "AND `c_module` = 'System' " .
                           "AND `c_created_userID` = ".$uid;

 return $this->db->query($q_s_type)->result_array();
}

到(见this question

function get_folder_category_id($uid){
 $q_s_type = "SELECT c_type_id,c_type_pid,c_type_name " .  
                "FROM `codeigniter_system_category_type` " .  
                "   WHERE `c_type_pid` = 0 " . 
                      "AND `c_type_name` IN ('Others', 'Shared') " . 
                      "AND `c_module` = 'System' " . 
                      "AND `c_created_userID` = ".$uid;

 return $this->db->query($q_s_type)->result_array();
}

【讨论】:

    猜你喜欢
    • 2014-08-15
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多