Last active
December 21, 2022 15:00
-
-
Save petenelson/dc36d025798984b21f2b928c4e333287 to your computer and use it in GitHub Desktop.
WordPress: Sample Code to debug slow DB queries when in WP-CLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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