Skip to content

Commit

Permalink
Use SAVEQUERIES if set
Browse files Browse the repository at this point in the history
If SAVEQUERIES has been enabled on the server, use that data instead of
generating our own and potentially breaking SAVEQUERIES.
  • Loading branch information
LinuxJedi committed Jun 10, 2023
1 parent b35e76a commit 3d89a73
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
34 changes: 24 additions & 10 deletions inc/App/ExecutionTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,30 @@ function __construct() {
public static function save_average_query_execution_time() {
global $wpdb;

if( !empty( $wpdb->total_query_time ) && !empty( $wpdb->num_queries ) && $wpdb->num_queries > 0 ) {

$wpdb->insert(
$wpdb->prefix . self::TABLE_NAME,
[
'seconds' => $wpdb->total_query_time,
'queries_num' => $wpdb->num_queries
]
);

if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
if ( !empty($wpdb->queries) && !empty($wpdb->num_queries) && $wpdb->num_queries > 0) {
$query_times = array();
foreach ( $wpdb->queries as $key => $value ) {
$query_times[] = $value[1];
}
$wpdb->insert(
$wpdb->prefix . self::TABLE_NAME,
[
'seconds' => array_sum($query_times),
'queries_num' => $wpdb->num_queries
]
);
}
} else {
if( !empty( $wpdb->total_query_time ) && !empty( $wpdb->num_queries ) && $wpdb->num_queries > 0 ) {
$wpdb->insert(
$wpdb->prefix . self::TABLE_NAME,
[
'seconds' => $wpdb->total_query_time,
'queries_num' => $wpdb->num_queries
]
);
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions inc/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ public function query($query)
}
}

$tmp = new MDB_DB(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
$tmp->loadFromParentObj($wpdb);
$wpdb = $tmp;
// If savequeries is set, let's not duplicate effort
if ( !defined( 'SAVEQUERIES' ) || !SAVEQUERIES ) {
$tmp = new MDB_DB(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
$tmp->loadFromParentObj($wpdb);
$wpdb = $tmp;
}

function mdbhc_save_average_query_execution_time()
{
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Extract the contents of the ZIP and upload the contents to the `/wp-content/plug
* Add more useful metrics to the graph
* Fix version check for MariaDB < 10.2
* Drop plugin's tables on uninstall
* Use SAVEQUERIES data if exists

= 1.0.3 =

Expand Down

0 comments on commit 3d89a73

Please sign in to comment.