0

I've made a web site using Codeigniter and I'm facing the issue related with speed.

All other functionality is much faster but I don't know why it takes too much time to load.

Well Only one query will perform for that operation but waiting time is too much high.

enter image description here

This issue is occurred first time when website is load.

// Controller

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

// Model

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();
 }

Script call with Ajax.

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 1
    Have you tested the query using for example `EXPLAIN`? And have you verified that the query really is the problem? – jeroen Jan 31 '18 at 10:28
  • share more information. Is this in some kind of loop? How many records are being displayed? – Rotimi Jan 31 '18 at 10:29
  • @jeroen I've tested this query and in mysql and it will takes only 0.00007 sec. – Shailesh Prajapati Jan 31 '18 at 10:31
  • @Akintunde007 There is no loop, It just call from ajax and here fire only one query and return Json. – Shailesh Prajapati Jan 31 '18 at 10:32
  • My suggestion to you is check functions you use and their compatibility with your framework and php version. I had faced same problem long time ago with php and after checking some functions I found it's cause some problems. – Mourad Karoudi Jan 31 '18 at 10:36
  • Let me check @MouradKaroudi – Shailesh Prajapati Jan 31 '18 at 10:39
  • Try feeding your query to https://explain.depesz.com/ and feeding the output of that to https://mariadb.org/explain_analyzer/analyze/ – Mawg says reinstate Monica Jan 31 '18 at 10:42
  • does your connection is with 'localhost' or '127.0.0.1' ? – Ahmed Sunny Jan 31 '18 at 10:42
  • @AhmedSunny Yes Currently I'm using my localhost, But On server it takes much more time to load while first time (34 sec). – Shailesh Prajapati Jan 31 '18 at 10:44
  • try '127.0.0.1' on local and see if it changes anything – Ahmed Sunny Jan 31 '18 at 10:45
  • What do you see when you set the Network tab to view all activity and not just XHR? It could be the initial load is being blocked by the loading of other assets - i.e. javascript, css. – DFriend Jan 31 '18 at 10:45
  • and check this also . probably duplicate https://stackoverflow.com/questions/15451732/why-is-the-waiting-length-so-long-on-my-ajax-call-chrome-network-panel – Ahmed Sunny Jan 31 '18 at 10:46
  • Ok, just for testing purposes can you save the query result then remove the query statement and return the saved query result (to avoid hitting the database) ... this way we can narrow down the problem. conversely, you could also use the benchmarking functionality of CI: https://www.codeigniter.com/userguide3/libraries/benchmark.html – Alex Feb 01 '18 at 00:27

1 Answers1

0

Unless I am very much wrong, the only thing that change sin those two SELECT statements is the check of the value of c_type_name

If so, you can simplify that UNION, and lose the second SELECT, by changing from

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();
}

to (see 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();
}
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551