get the number of queries made on a page(inside my plugin)

Is there an easy way to see how many queries you make on a page(inside my plugin)? As I dont want to many queries in my plugin.

Related posts

Leave a Reply

1 comment

  1. $wpdb counts the queries ran and saves the value in the $num_queries class variable.

    $num_queries
    The number of queries that have been executed.

    https://codex.wordpress.org/Class_Reference/wpdb

    All you need to do is retrieve it:

    echo $wpdb->num_queries;
    

    You may also be interested in the $queries class variable.

    $queries
    You may save all of the queries run on the database and their stop times by setting the SAVEQUERIES constant to TRUE (this constant
    defaults to FALSE). If SAVEQUERIES is TRUE, your queries will be
    stored in this variable as an array.

    $num_queries will reflect all queries. The queries variable should have information enough that you can isolate and count only your plugin’s queries, if that is what you want.