简体   繁体   中英

php zend db profiler filter by table name

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..

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();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM