Recently as part of one of the troubleshooting activities, I had to log all the SQL queries generated by WordPress. I have followed the below approach to log the queries.

Add the below snippet of code to the functions.php file within your themes folder.

add_action('shutdown', 'sql_logger');
function sql_logger() {
    global $wpdb;
    $log_file = fopen(ABSPATH.'/sqlqueries.log', 'a');
    fwrite($log_file, "===============================\n\n" . date("F j, Y, g:i:s a")."\n");
    foreach($wpdb->queries as $q) {
        fwrite($log_file, $q[0] . " - ($q[1] s)" . "\n\n");
    }
    fclose($log_file);
}

After adding the above code, add the following line to the wp-config.php

define('SAVEQUERIES', true);

This will write the SQL queries to a log file within the WordPress home directory. Ensure the file sqlqueries.log in the WordPress home directory is writable by the php. Restart the webserver and refresh the page.