I am creating a wordpress plugin that does a custom query around the WordPress database, after which I am looping with the results listing each publish title like a connect to the particular publish.

I am using get_permalink($id) to get the URI of every publish, consider I am carrying this out outdoors from the loop, my suspicion is all these demands is creating a separate database query.

I have examined the function code and attempted to follow along with what's happening in the WordPress core files, but what I am really thinking about is really a general method of doing this, in order to make certain I am always writing probably the most enhanced code in most of my plug ins.

Is anybody conscious of the easiest method to make this happen?

In wordpress-config.php add this line

define('SAVEQUERIES', true);

In you are theme functions.php file ( or perhaps a pluggin file for your matter ) this can be used

add_action('shutdown', 'sql_logger');
function sql_logger() {
    global $wpdb;
    $log_file = fopen(ABSPATH.'/sql_log.txt', '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);
}

Make shure ABSPATH.'/sql_log.txt' is writeble from php .

Hope this can help