Codeigniter 中 $this->db->query_times 数组的时间格式是什么?
What is time format of $this->db->query_times array in Codeigniter?
令人惊讶的是,没有关于 codeigniter v2 和 v3 的文档。我刚刚从这里的教程中自己找到代码是 link。该教程并不完美,也无法正常工作,但我倾向于改进它。
我指的是 $this->db->query_times 和 $this->db->queries.我很惊讶地发现这些有效。
现在您可以在 Codginiter 挂钩中使用这些来确定使用 $this->db->queries 执行哪个查询以及使用 [= 花费多少时间50=]$this->db->query_times 并在 post controller
之后实现钩子
codeigniter v3 中 $this->db->queries 的结果是
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
)
而 codeigniter v3 中 $this->db->query_times 的结果是
Array
(
[0] => 0.00015497207641602
[1] => 0.0003199577331543
[2] => 0.0003359317779541
)
两个数组具有相同数量的索引,因为第一个数组包含执行的查询,第二个数组包含这些查询的执行时间。
我有一个小问题,就是要确定 query_time 格式以正确转换为秒或分钟。
会这样使用
(如果您对它的使用方式不感兴趣,请忽略它)
我将通过挂钩 LogQueryHook class 在 codeigniter 中 post 控制器挂钩之后。
转到 application/config/config.php 并更改此
$config['enable_hooks'] = FALSE;
至此
switch (ENVIRONMENT)
{
case 'development':
$config['enable_hooks'] = TRUE;
break;
case 'testing':
case 'production':
default:
$config['enable_hooks'] = FALSE;
break;
}
所以只有在开发中你才记录所有查询。
现在转到 application/config/hooks.php 以在 post 系统之后挂接您的 LogQueryHook class。只需复制粘贴此代码。
$hook['post_system'][] = array(
'class' => 'LogQueryHook',
'function' => 'log_queries',
'filename' => 'LogQueryHook.php',
'filepath' => 'hooks'
);
在应用程序文件夹的 hooks 文件夹中创建一个文件名 "LogQueryHook.php"。
它看起来像这样 application\hooks\LogQueryHook.php
现在粘贴此代码以记录每个查询的执行时间。
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');
}
}
}
之后运行开发环境中的任何页面和所有查询都会记录在application\logs\queries.log.txt文件中。
query_time
将 return 秒作为输出 ($time_end - $time_start)
。
$time_end
和 $time_start
都使用 microtime
函数 in PHP 并将参数设置为 true。
microtime(TRUE)
: 这 return 秒而不是微秒。
令人惊讶的是,没有关于 codeigniter v2 和 v3 的文档。我刚刚从这里的教程中自己找到代码是 link。该教程并不完美,也无法正常工作,但我倾向于改进它。
我指的是 $this->db->query_times 和 $this->db->queries.我很惊讶地发现这些有效。
现在您可以在 Codginiter 挂钩中使用这些来确定使用 $this->db->queries 执行哪个查询以及使用 [= 花费多少时间50=]$this->db->query_times 并在 post controller
之后实现钩子codeigniter v3 中 $this->db->queries 的结果是
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
)
而 codeigniter v3 中 $this->db->query_times 的结果是
Array
(
[0] => 0.00015497207641602
[1] => 0.0003199577331543
[2] => 0.0003359317779541
)
两个数组具有相同数量的索引,因为第一个数组包含执行的查询,第二个数组包含这些查询的执行时间。
我有一个小问题,就是要确定 query_time 格式以正确转换为秒或分钟。
会这样使用
(如果您对它的使用方式不感兴趣,请忽略它)
我将通过挂钩 LogQueryHook class 在 codeigniter 中 post 控制器挂钩之后。
转到 application/config/config.php 并更改此
$config['enable_hooks'] = FALSE;
至此
switch (ENVIRONMENT)
{
case 'development':
$config['enable_hooks'] = TRUE;
break;
case 'testing':
case 'production':
default:
$config['enable_hooks'] = FALSE;
break;
}
所以只有在开发中你才记录所有查询。
现在转到 application/config/hooks.php 以在 post 系统之后挂接您的 LogQueryHook class。只需复制粘贴此代码。
$hook['post_system'][] = array(
'class' => 'LogQueryHook',
'function' => 'log_queries',
'filename' => 'LogQueryHook.php',
'filepath' => 'hooks'
);
在应用程序文件夹的 hooks 文件夹中创建一个文件名 "LogQueryHook.php"。 它看起来像这样 application\hooks\LogQueryHook.php 现在粘贴此代码以记录每个查询的执行时间。
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');
}
}
}
之后运行开发环境中的任何页面和所有查询都会记录在application\logs\queries.log.txt文件中。
query_time
将 return 秒作为输出 ($time_end - $time_start)
。
$time_end
和 $time_start
都使用 microtime
函数 in PHP 并将参数设置为 true。
microtime(TRUE)
: 这 return 秒而不是微秒。