Zend framework调试查看sql

更新时间:2023-10-22 06:06:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Zend framework调试查看sql执行

调试查看sql执行 (2008-12-9) 1.model文件

$this->_db = Zend_Registry::get('db'); $obj_profiler = $this->_db->getProfiler(); var_dump($obj_profiler);

2.controller文件

$db = Zend_Registry::get('db');

$db->getProfiler()->setEnabled(true); $profiler = $db->getProfiler(); var_dump($profiler);

3.zend文件 (推荐)

在Zend_Db_Adapter_Abstract的query方法里面打印,任何sql都经过这里

4.mysql底层

a.通过日志查看mysql正在执行的SQL语句 在mysql的配置文件 my.ini 中最后添加 log=d:/mysql/log/log.txt 重启mysql

可以记录所有的mysql执行的sql语句

b.show processlist

a.进入 mysql/bin 目录下输入 mysqladmin processlist; b.启动 mysql ,输入 show processlist;

出结果的字段解释中可以分析执行了的sql语句类型

$db->getProfiler()->setEnabled(false); $db->getProfiler()->setEnabled(true);

$profiler = $db->getProfiler();

$query = $profiler->getLastQueryProfile(); $query->getQuery();

$query->getElapsedSecs();

$totalTime = $profiler->getTotalElapsedSecs(); $queryCount = $profiler->getTotalNumQueries();

$profiler->setFilterElapsedSecs(5); $profiler->setFilterElapsedSecs(null);

$profiler->setFilterQueryType(Zend_Db_Profiler::SELECT);

$profiler->setFilterQueryType(Zend_Db_Profiler::SELECT | Zend_Db_Profiler::INSERT | Zend_Db_Profiler::UPDATE);

$profiler->setFilterQueryType(Zend_Db_Profiler::DELETE); $profiler->setFilterQueryType(null);

$profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT); $profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT | Zend_Db_Profiler::INSERT | Zend_Db_Profiler::UPDATE);

$profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::DELETE);

$profiler = new Zend_Db_Profiler_Firebug('All DB Queries'); $profiler->setEnabled(true); $db->setProfiler($profiler)

提供记录、分析数据库操作的功能,帮助调试和检查性能问题 可以打印sql,执行时间,分析性能问题 设置

// turn off profiler:

$db->getProfiler()->setEnabled(false);

// turn on profiler:

$db->getProfiler()->setEnabled(true); 使用

$profiler = $db->getProfiler();

$query = $profiler->getLastQueryProfile(); echo $query->getQuery();

$totalTime = $profiler->getTotalElapsedSecs(); $queryCount = $profiler->getTotalNumQueries(); $longestTime = 0; $longestQuery = null;

foreach ($profiler->getQueryProfiles() as $query) { if ($query->getElapsedSecs() > $longestTime) { $longestTime = $query->getElapsedSecs(); $longestQuery = $query->getQuery();

} }

echo 'Executed ' . $queryCount . ' queries in ' . $totalTime . ' seconds' . \

echo 'Average query length: ' . $totalTime / $queryCount . ' seconds' . \

echo 'Queries per second: ' . $queryCount / $totalTime . \ echo 'Longest query length: ' . $longestTime . \ echo \

方法

getTotalNumQueries() returns the total number of queries that have been profiled.

getTotalElapsedSecs() returns the total number of seconds elapsed for all profiled queries.

getQueryProfiles() returns an array of all query profiles.

getLastQueryProfile() returns the last (most recent) query profile, regardless of whether or not the query has finished (if it hasn't, the end time will be null)

clear() clears any past query profiles from the stack.

getQuery() returns the SQL text of the query.

getQueryParams() returns an array of parameter values used when executing a prepared query.

getElapsedSecs() returns the number of seconds the query ran.

高级用法

// Only profile queries that take at least 5 seconds: $profiler->setFilterElapsedSecs(5);

// Profile all queries regardless of length: $profiler->setFilterElapsedSecs(null);

// profile only SELECT queries

$profiler->setFilterQueryType(Zend_Db_Profiler::SELECT);

// profile SELECT, INSERT, and UPDATE queries

$profiler->setFilterQueryType(Zend_Db_Profiler::SELECT | Zend_Db_Profiler::INSERT | Zend_Db_Profiler::UPDATE);

// profile DELETE queries

$profiler->setFilterQueryType(Zend_Db_Profiler::DELETE);

// Remove all filters

$profiler->setFilterQueryType(null);

// Retrieve only SELECT query profiles

$profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT);

// Retrieve only SELECT, INSERT, and UPDATE query profiles

$profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT | Zend_Db_Profiler::INSERT | Zend_Db_Profiler::UPDATE);

// Retrieve DELETE query profiles

$profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::DELETE);

Profiling with Firebug 结合firebug调试

// In your bootstrap file

$profiler = new Zend_Db_Profiler_Firebug('All DB Queries'); $profiler->setEnabled(true);

// Attach the profiler to your db adapter $db->setProfiler($profiler)

// Dispatch your front controller

// All DB queries in your model, view and controller // files will now be profiled and sent to Firebug

本文来源:https://www.bwwdw.com/article/iv7f.html

Top