Laravel-debugbar: Order queries based on duration?

Created on 14 Jun 2017  Â·  8Comments  Â·  Source: barryvdh/laravel-debugbar

Is there a way to display the db queries ordered by duration on the "Queries" tab in Debugbar, for example from slowest to fastest?

stale

Most helpful comment

If two of you like me I can send a pull request #701

All 8 comments

Not yet.

Would be useful.
And possibility for hide queries by given time threshold.

it would be usefull too to see which query is executed how often.

For Example:

735 statements were executed, 580 of which were duplicated, 155 unique

=> I would like to see which of the duplicated statements was executed the most

If two of you like me I can send a pull request #701

For anyone who needs a quick fix to see longest running queries first:
edit DataCollector/QueryCollector.php#390 to something like this:

$statements = [];
        $statements_tmp = [];
        foreach ($queries as $query) {
            $totalTime += $query['time'];

            $statements_tmp[$query['time'].'.'.$totalTime] = [
                'sql' => $this->getDataFormatter()->formatSql($query['query']),
                'type' => $query['type'],
                'params' => [],
                'bindings' => $query['bindings'],
                'hints' => $query['hints'],
                'backtrace' => array_values($query['source']),
                'duration' => $query['time'],
                'duration_str' => ($query['type'] == 'transaction') ? '' : $this->formatDuration($query['time']),
                'stmt_id' => $this->getDataFormatter()->formatSource(reset($query['source'])),
                'connection' => $query['connection'],
            ];

            //Add the results from the explain as new rows
            foreach($query['explain'] as $explain){
                $statements_tmp[] = [
                    'sql' => ' - EXPLAIN #' . $explain->id . ': `' . $explain->table . '` (' . $explain->select_type . ')',
                    'type' => 'explain',
                    'params' => $explain,
                    'row_count' => $explain->rows,
                    'stmt_id' => $explain->id,
                ];
            }
        }
        krsort($statements_tmp);
        foreach($statements_tmp as $statement)
            $statements[] = $statement;

for a quick solution. run this code on chrome inspect and you will get a nice sort button on query tabs 😉

image

(function() {

    if ($('.sort-queries-by-time').length == 0) {
        $('.phpdebugbar-widgets-sqlqueries .phpdebugbar-widgets-status')
        .append('<a class="sort-queries-by-time" style="padding: 2px 11px 2px 7px;margin-right: 5px;margin-top: -1px;border-radius: 5px;text-transform: uppercase;font-weight: bold;color: #ffffff;font-size: 11px;background: #f4645f;box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);float: right;line-height: 1.8em;">✌ Duration</a>');
    }

    function parseToMicroSeconds(time) {

        var format = time.replace(/[0-9]/g, '').replace('.', '');
        time = parseFloat(time)

        switch (format) {
        case 's':
            return time * 1000000
            break;
        case 'ms':
            return time * 1000
            break;
        case 'Ξs':
            return time
            break;
        }

        return null;
    }

    var type = null;
    $(".sort-queries-by-time").off('click');

    $(".sort-queries-by-time").click(function() {
        var $this = $(this);

        if (!type) {
            $this.text('👆 Duration');
            type = 'desc'
        }

        if (type == 'desc') {
             $this.text('👇 Duration');
            type = 'asc'
        } else {
             $this.text('👆 Duration');
            type = 'desc'
        }

        var items = $this.parent().parent().find('.phpdebugbar-widgets-list li').get();

        items.sort(function(a, b) {
            var valueA = parseToMicroSeconds($(a).find("span.phpdebugbar-widgets-duration").text());
            var valueB = parseToMicroSeconds($(b).find("span.phpdebugbar-widgets-duration").text());

            if (type == 'desc') {
                if (valueA < valueB)
                    return 1;
                if (valueA > valueB)
                    return -1;
            }

            if (type == 'asc') {
                if (valueA < valueB)
                    return -1;
                if (valueA > valueB)
                    return 1;
            }
            return 0;
        });
        // clear the list and re-add sorted items on button click 
        $this.parent().parent().find(".phpdebugbar-widgets-list").empty().append(items);
    });

}
)()

Easier:

    $statements = [];

    $queries = collect($queries)->sortBy('time')->reverse()->toArray();

    foreach ($queries as $query) {

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this issue is still present on the latest version of this library on supported Laravel versions, please let us know by replying to this issue so we can investigate further.
Thank you for your contribution! Apologies for any delayed response on our side.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

beneverard picture beneverard  Â·  6Comments

truongthaison picture truongthaison  Â·  4Comments

kjhatis picture kjhatis  Â·  5Comments

njbarrett picture njbarrett  Â·  5Comments

thomthom picture thomthom  Â·  5Comments