2

I have this code:

$query = "SELECT * 
            FROM $wpdb->posts
                INNER JOIN $wpdb->postmeta 
                    ON $wpdb->posts.ID = $wpdb->postmeta.post_id
                INNER JOIN $wpdb->term_relationships 
                    ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
            WHERE ((post_type = 'projects') OR (post_type = 'post_cost_codes'));";

$results = $wpdb->query($query); // Takes 1.5 seconds

I also tried this:

$query = "SELECT * 
            FROM $wpdb->posts
                INNER JOIN $wpdb->postmeta 
                    ON $wpdb->posts.ID = $wpdb->postmeta.post_id
                INNER JOIN $wpdb->term_relationships 
                    ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
            WHERE ((post_type = 'projects') OR (post_type = 'post_cost_codes'));";

$results = $wpdb->get_results($query); // Still takes 1.5 seconds

But when I grab the query and put it in phpmyadmin:

(18588 total, Query took 0.0102 seconds.)
SELECT * FROM wp_dev_posts, wp_dev_postmeta, wp_dev_term_relationships WHERE ((post_type = 'projects') OR (post_type = 'post_cost_codes')) AND (wp_dev_posts.ID = wp_dev_postmeta.post_id) AND (wp_dev_posts.ID = wp_dev_term_relationships.object_id)
// Takes 0.0102 seconds

Why do both $wpdb methods take so much longer? The same query is copy and pasted directly on phpmyadmin, it takes the expected amount of time.

  • I answered based on the difference between the WPDB methods as I originally thought that's what you were asking. But it appears you are actually asking why it takes longer to run it through the WPDB object rather than as a query in phpMyAdmin. If that's what your question is, I'll change my answer, but you should also edit your question to clarify exactly what it is you're asking. – butlerblog Aug 2 '19 at 18:49
  • PHPMyAdmin will typically automatically limit the number of results returned to 25 by default. Are you that’s not what’s happening? – Jacob Peattie Aug 3 '19 at 1:18
  • How did you measure the query time? Because I used the exact query (except different post types) and the query time was ~0.002 seconds both using wpdb::query() and phpMyAdmin. And the query time, I checked it via var_dump( $wpdb->queries ); (just find the relevant query) - the query time is the second item in each array in the $wpdb->queries which is an array of queries. See SAVEQUERIES for details on enabling the $wpdb->queries. – Sally CJ Aug 3 '19 at 1:40
  • @SallyCJ I'm measuring it with a call to: $time = (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']); – Justin J James Aug 6 '19 at 16:31
  • You're measuring the script execution time, but what you see in phpMyAdmin is just the time that was taken to execute the SQL query. Try installing Query Monitor, run your query, and check the Query Monitor (QM) menu in the admin bar. Then just find your query and check the query time - example. – Sally CJ Aug 6 '19 at 23:23
0

The difference is what is returned.

The WPDB query() method returns true for CREATE, ALTER, TRUNCATE and DROP queries, an integer of how many results for all other queries, or simply false if if there's an error.

The WPDB get_results() method actually returns the entire result of the query (like you would get running the query in phpMyAdmin). You can return the result as an array or an object, depending on how you want to work with the results.

|improve this answer|||||
  • Yeah I have seen these answers, which is why I wanted to test both. I will edit my first post to better ask the question, but I want to know why both take so much longer, when the same query directly on phpmyadmin takes the expected time. – Justin J James Aug 2 '19 at 19:01
0

Without much luck, I solved my own problems by using get_transient(...) and set_transient(...)

Note, this is a wordpress-only fix. and I still makes the first call the query the one who has to suffer.

In my case, it was acceptable since I am fine with storing the result for the entire day. Even 12 hours is fine for me. I'm still looking for answers to the original question since I'm very curious what is going wrong.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.