php zend db profiler 按表名过滤

发布于 2024-12-05 11:56:10 字数 182 浏览 2 评论 0原文

Is there a way to filter the queries by table name in the zend db profiler? The documentation doesn't have anything but I don't know if I can just rely on this document completely..if you know a way, please advise..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中的蝴蝶 2024-12-12 11:56:10

目前还没有一种方法可以根据表名称来过滤探查器,只能根据查询类型(INSERT、UPDATE 等)或查询的运行时间来过滤。

这是一些您可以尝试的代码,它们可能会帮助您完成您想要的操作,但请注意,我还没有测试过它,但希望它可以帮助您入门。

基本上,它循环遍历分析的每个查询,并使用 preg_match 来查看查询是否针对您的表,如果不是,则取消设置查询信息并继续,如果是,则更新一些统计信息。在 foreach 的末尾,$queries 应该只是对您要分析的表的查询。

    <?php
    $tableName = 'my_table';

    /** var $profiler Zend_Db_Profiler */
    $profiler  = $db->getProfiler();

    $queries   = $profiler->getQueryProfiles();

    $totalQueries   = 0;
    $totalTime      = 0;

    if ($queries !== false) {
        foreach ($queries as $index => $query) {
            $queryString = $query->getQuery();

            $t = preg_quote($tableName);

            if (!preg_match("/UPDATE .?$t.? /i", $queryString) ||
                !preg_match("/INSERT INTO .?$t.?/i", $queryString) ||
                !preg_match("/DELETE FROM .?$t.?/i", $queryString) ||
                !preg_match("/REPLACE .*?INTO .?$t.?/i", $queryString) ||
            ) {
                unset($queries[$index]);
                continue;
            }

            $totalQueries++;
            $totalTime += $query->getElapsedSecs();
        }
    }

There is currently not a way to filter the profiler based on table name, only by query type (INSERT, UPDATE, etc) or elapsed time of the query.

Here is some code you could try that may help you do what you want though, note, I have not tested it, but hopefully it can get you started.

Basically, it loops over each query that was profiled and uses preg_match to see if the query was to your table, and if not it unsets the query info and continues, if it was then it updates some stats. At the end of the foreach, $queries, should be only the queries to the table you want to profile.

    <?php
    $tableName = 'my_table';

    /** var $profiler Zend_Db_Profiler */
    $profiler  = $db->getProfiler();

    $queries   = $profiler->getQueryProfiles();

    $totalQueries   = 0;
    $totalTime      = 0;

    if ($queries !== false) {
        foreach ($queries as $index => $query) {
            $queryString = $query->getQuery();

            $t = preg_quote($tableName);

            if (!preg_match("/UPDATE .?$t.? /i", $queryString) ||
                !preg_match("/INSERT INTO .?$t.?/i", $queryString) ||
                !preg_match("/DELETE FROM .?$t.?/i", $queryString) ||
                !preg_match("/REPLACE .*?INTO .?$t.?/i", $queryString) ||
            ) {
                unset($queries[$index]);
                continue;
            }

            $totalQueries++;
            $totalTime += $query->getElapsedSecs();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文