Categories
Database MySQL WordPress

MySQL Queries In WordPress

I’ve started looking at the MySQL queries in WordPress to see what sort of optimizations are possible. To help with this I uncommented the HTML comments around the query count and the timer at the bottom of index.php. To look at all of the queries being run I add the following line to the top index.php define(‘SAVEQUERIES’, true);. This tells the $wpdb class to save all of the SQL queries, storing them in the $wpdb->savedqueries array. To look at those queries I added the following PHP code at the bottom of index.php (just above ):

    $num_queries = count($wpdb->savedqueries);
    for($i = 0; $i savedqueries[$i] =
                 wordwrap($wpdb->savedqueries[$i]);
    }
    print_r($wpdb->savedqueries);

It isn’t the prettiest in the world, but it works. So far I’ve found a handful of queries that have a 1=1 in the WHERE clause, which seemed pretty pointless. I’ve also noticed a query that is completely useless in the case where someone is browsing a WordPress blog without being logged in. The query will always fail if no one is logged in. So with an additional !empty() test, the number of queries went down from 19 to 18 in the case where no one is logged in.

I’m not sure how many other “easy” SQL oddities I’ll find though. Only time will tell.

Leave a Reply

Your email address will not be published. Required fields are marked *