Search code examples
phpcodeignitertimecodeigniter-2codeigniter-3

What is time format of $this->db->query_times array in Codeigniter?


Surprisingly there is no documentation of it for both codeigniter v2 and v3. I just found out the code myself from a tutorial here is the link. The tutorial is not perfect and also not working but I tend to improve that.

What I'm referring is $this->db->query_times and $this->db->queries. And I'm amazed to found out that these are working.

Now you can use these in Codginiter hooks to determine which query is executed by using $this->db->queries and how much time it is taking by using $this->db->query_times and implement the hook after post controller

The result of $this->db->queries in codeigniter v3 is

Array
(
    [0] => SELECT GET_LOCK('0ae383feca87611f9ef779f13ae5d3f3', 300) AS ci_session_lock
    [1] => SELECT `data`
FROM `ci_sessions_3`
WHERE `id` = '9sh99dhtp1i51t21g498gerhem4e6gl6'
    [2] => SELECT * FROM `tradelog` WHERE status=1
)

and the result of $this->db->query_times in codeigniter v3 is

Array
(
    [0] => 0.00015497207641602
    [1] => 0.0003199577331543
    [2] => 0.0003359317779541
)

Both array have same number of indexes because first array contains queries which is executed and second array contains these queries executed time.

I have a little problem which is to determine the query_time format to properly convert into seconds or in minutes.

It'll be used like this

(Ignore it if you're not interested how it will be used)

I'll use it to record all my queries in queries.log.txt file in development Environment by hooking LogQueryHook class after post controller hooks in codeigniter.

Goto to application/config/config.php and change this

$config['enable_hooks'] = FALSE;

to this

switch (ENVIRONMENT)
{
    case 'development':
        $config['enable_hooks'] = TRUE;
        break;
    case 'testing':
    case 'production':
    default:
        $config['enable_hooks'] = FALSE;
        break;
}

so only in development you log all queries.

Now goto application/config/hooks.php to hook your LogQueryHook class after post system . Just copy paste this code.

$hook['post_system'][] = array(
    'class' => 'LogQueryHook',
    'function' => 'log_queries',
    'filename' => 'LogQueryHook.php',
    'filepath' => 'hooks'
);

Create a file name "LogQueryHook.php" in hooks folder in application folder. It'll look like this application\hooks\LogQueryHook.php Now paste this code to log every query with execution time took.

class LogQueryHook
{
    function log_queries()
    {
        $CI = &get_instance ();
        $times = $CI->db->query_times;
        $output = null;
        $queries = $CI->db->queries;
        $date_now = date('Y-m-d h:i:sa');

        /** Spaces are given for proper alignment of text */
        if (count ($queries) == 0) {
            $output .= $date_now . "        no queries\n";
        } else {
            foreach ($queries as $key => $query) {
                $took = round (doubleval ($times[$key]), 3);
                // I need to improve this line to record time properly
                // and I don't know in which format codeginiter is giving me time either its is seconds or miliseconds.
                $output .= $date_now . "        ===[took:{$took}]\n";
                $query = str_replace (array("\r\n", "\r", "\n", "\\r", "\\n", "\\r\\n"), "\n                             ", "                             " . $query);
                $output .= $query . "\n\n\n";
            }
        }

        $CI->load->helper ('file');
        if (!write_file (APPPATH . "/logs/queries.log.txt", $output, 'a+')) {
            log_message ('debug', 'Unable to write query the file');
        }
    }
}

After that run any page in development environment and all the queries will be recorded in application\logs\queries.log.txt file.


Solution

  • query_time will return seconds as an output ($time_end - $time_start). Both these $time_end and $time_start uses microtime function in PHP with parameter set to true. microtime(TRUE) : this returns seconds instead of micro seconds.