Skip to content

Instantly share code, notes, and snippets.

@petenelson
Last active December 21, 2022 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petenelson/dc36d025798984b21f2b928c4e333287 to your computer and use it in GitHub Desktop.
Save petenelson/dc36d025798984b21f2b928c4e333287 to your computer and use it in GitHub Desktop.
WordPress: Sample Code to debug slow DB queries when in WP-CLI
<?php
global $wpdb;
$wpdb->queries = [];
// Run some slow code here.
if ( ! empty( $wpdb->queries ) ) {
// Convert to ms.
$wpdb->queries = array_map(
function( $q ) {
$q[1] = $q[1] * 1000;
return $q;
},
$wpdb->queries
);
usort( $wpdb->queries, function( $a, $b ) {
return $a[1] - $b[1]; // Query time in seconds.
});
foreach ( $wpdb->queries as $q ) {
\WP_CLI::line( $q[1] . ': ' . $q[0] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment