# Slow Database Query Warning for Admins

> When SAVEQUERIES is enabled, lists the slowest database queries of the page with their duration and caller in an HTML comment visible to administrators.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: debugging, database, performance, queries
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dev-query-monitor-lite/

## Code

```php
add_action( 'wp_footer', function () {
    global $wpdb;

    $limit = 10; // Number of slowest queries to list.

    if ( ! defined( 'SAVEQUERIES' ) || ! SAVEQUERIES || ! current_user_can( 'manage_options' ) || empty( $wpdb->queries ) ) {
        return;
    }

    $queries = $wpdb->queries;

    usort( $queries, function ( $a, $b ) {
        return $b[1] <=> $a[1];
    } );

    $lines = array( sprintf( 'Slowest %d of %d queries', min( $limit, count( $queries ) ), count( $queries ) ) );

    foreach ( array_slice( $queries, 0, $limit ) as $query ) {
        $lines[] = sprintf( '%.4fs | %s | %s', $query[1], preg_replace( '/\s+/', ' ', mb_substr( $query[0], 0, 300 ) ), $query[2] );
    }

    echo "\n<!--\n" . esc_html( str_replace( '--', '- -', implode( "\n", $lines ) ) ) . "\n-->\n";
}, 9999 );
```

## Notes

Add define( 'SAVEQUERIES', true ); to wp-config.php while debugging only; it slows the site down. Change $limit to show more queries.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Slow Database Query Warning for Admins" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
