在phalcon里有一个\Phalcon\Db\Profiler 类,这个类可以用来记录sql语句并计算消耗的时间。那么如何使用它呢?

手册里其实已经提供了方法,总结如下:

1.向$di里注册profiler服务

$di->set('profiler', function(){
return new\Phalcon\Db\Profiler();
}, true);

2.注册db服务时,顺便注册下事件

$di->set('db', function() use ($di) {
//新建一个事件管理器
$eventsManager = new \Phalcon\Events\Manager(); //从di中获取共享的profiler实例
$profiler = $di->getProfiler(); //监听所有的db事件
$eventsManager->attach('db', function($event, $connection) use ($profiler) {
//一条语句查询之前事件,profiler开始记录sql语句
if ($event->getType() == 'beforeQuery') {
$profiler->startProfile($connection->getSQLStatement());
}
//一条语句查询结束,结束本次记录,记录结果会保存在profiler对象中
if ($event->getType() == 'afterQuery') {
$profiler->stopProfile();
}
}); $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => "localhost",
"username" => "root",
"password" => "secret",
"dbname" => "invo"
)); //将事件管理器绑定到db实例中
$connection->setEventsManager($eventsManager); return $connection;
}); 

3.程序中调出sql记录

//执行一些查询
Robots::find();
Robots::find(array("order" => "name"));
Robots::find(array("limit" => 30)); //获取所有的prifler记录结果,这是一个数组,每条记录对应一个sql语句
$profiles = $this->di->get('profiler')->getProfiles();
//遍历输出
foreach ($profiles as $profile) {
echo "SQL语句: ", $profile->getSQLStatement(), "\n";
echo "开始时间: ", $profile->getInitialTime(), "\n";
echo "结束时间: ", $profile->getFinalTime(), "\n";
echo "消耗时间: ", $profile->getTotalElapsedSeconds(), "\n";
} //直接获取最后一条sql语句
echo $this->di->get('profiler')->getLastProfile()->getSQLStatement();

phalcon:跟踪sql语句的更多相关文章

  1. oracle跟踪sql语句

    oracle跟踪sql语句 select * from v$sql 查询客户端电脑名称的ID select terminal, SID,SERIAL#  from v$session where  ( ...

  2. EF Core 日志跟踪sql语句

    EF Core 日志跟踪sql语句 官方文档链接:https://docs.microsoft.com/en-us/ef/core/miscellaneous/logging 1.新增自定义ILogg ...

  3. 通过SQL Server的扩展事件来跟踪SQL语句在运行时,时间都消耗到哪儿了?

    原文:通过SQL Server的扩展事件来跟踪SQL语句在运行时,时间都消耗到哪儿了? 问题就是,一个很简单的语句,在不同的服务器上执行,所需要的时间相差很大,特别提到在性能差的服务器上反而快,在性能 ...

  4. mongodb 跟踪SQL语句及慢查询收集

    有个需求:跟踪mongodb的SQL语句及慢查询收集 第一步:通过mongodb自带函数可以查看在一段时间内DML语句的运行次数. 在bin目录下面运行  ./mongostat -port 端口号  ...

  5. PostgreSQL 慢查询SQL语句跟踪

    示例:启用 SQL 跟踪PostgreSQL 日志支持的输出格式有 stderr(默认), csvlog , syslog 一般的错误跟踪,只需在配置文件 [postgresql.conf]简单设置几 ...

  6. oracle sql语句跟踪及性能分析工具实现

    在网上找了一大圈,没找着合适的工具来跟踪oracle一段时间的sql. 我们的场景是打算自动化跑遍所有场景(rft)+fiddler跟踪请求+后端跟踪sql,根据结果去分析慢的请求和sql,本来awr ...

  7. 十、K3 WISE 开发插件《SQL Profiler跟踪单据操作时产生的SQL语句》

    =================================== 目录: 1.查询帐套的数据库DBID 2.配置需要跟踪数据库的DBID 3.配置跟踪参数 4.跟踪进行 5.分析跟踪语句 === ...

  8. 跟踪oracle中sql语句运行过程及相关知识拓展

    select * from v$sqlarea; select * from v$sqlarea where first_load_time>'2010-11-27/09:30:00'; 这种方 ...

  9. 使用 sql server 默认跟踪分析执行的 SQL 语句

    如果没有启用 SQL SERVER 的跟踪器来跟踪 SQL SERVER 的 SQL 执行情况,又想查最近的 SQL 执行情况,网上一般说是使用 LogExprorer 这个工具,网上找了这个工具很久 ...

随机推荐

  1. JavaScript高级应用(一)

    1.尺寸 //各种尺寸 s += "\r\n网页可见区域宽(document.body.clientWidth):"+ document.body.clientWidth; s + ...

  2. [问题2014A01] 解答二(后 n-1 列拆分法,由郭昱君同学提供)

    [问题2014A01] 解答二(后 n-1 列拆分法,由郭昱君同学提供) \[|A|=\begin{vmatrix} 1 & x_1^2-ax_1 & x_1^3-ax_1^2 &am ...

  3. Codeforces Round #384 (Div. 2)A,B,C,D

    A. Vladik and flights time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  4. touch穿透

    出现穿透的前提:上层绑定touch事件,下层绑定click事件. 解决方案:touchend绑定: e.preventDefault();$(下层).css("pointer-events& ...

  5. 利用gitHub搭建博客

    ##1.gitHub Page的的使用我觉得这边博文写的很清楚,方法.步骤.优缺点以及实例,所以就借用一下啦^_^ [搭建一个免费的,无限流量的Blog](http://www.ruanyifeng. ...

  6. hdu3247Resource Archiver(ac自动机+spfa)

    链接 这题没想到怎么做,问了下p队长,大悟.. 先求出任意两串的在trie树上的最短距离,期间是不能走到不合法的地方,我是用spfa求得,在更新和加入节点时判断一下是不是合法位置. 求出最短距离之后, ...

  7. Windows下安装postgresql_psycopg2时出现 "Unabled to find vcvarsall.bat" 的解决办法

    使用django时会用到postgresql的数据库,如下表: 数据库引擎设置 设置 数据库 适配器 postgresql PostgreSQL psycopg 版本 1.x, http://www. ...

  8. [原创] RT7 Lite win7旗舰版精简方案

    [原创] RT7 Lite win7旗舰版精简方案 墨雪SEED 发表于 2016-1-26 21:23:54  https://www.itsk.com/thread-362912-1-5.html ...

  9. xcode7 打开工程错误 This Document requires xcode8.0 or later.

    xcode7 打开工程错误 This Document requires xcode8.0 or later. 场景: xcode7创建的工程,后来安装了xcode8.0,用8打开7的工程跑了一下: ...

  10. WIN7 java7 环境变量配置

    1 首先要搞清楚,java可能安装了两个部分,一个叫JDK,一个叫jre.其中jre若已经安装了,那么在安装JDK的时候,就不要放在默认的C:\Program File\java\XXX目录.最好另起 ...