admin管理员组文章数量:1122832
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 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.
Share Improve this question edited Aug 2, 2019 at 19:02 Justin J James asked Aug 2, 2019 at 18:38 Justin J JamesJustin J James 391 silver badge7 bronze badges 5 |2 Answers
Reset to default 0The 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.
Without much luck, I solved my own problems by using get_transient(...) and set_transient(...)
Note, this is a wordpress-only fix. The first call has someone suffer, but once cached it's a good enough solution for me.
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.
本文标签: wpdbgtquery() vs wpdbgtgetresults() vs phpMyAdmin
版权声明:本文标题:$wpdb->query() vs. $wpdb->get_results() vs. phpMyAdmin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736293350a1929128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wpdb::query()
and phpMyAdmin. And the query time, I checked it viavar_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. SeeSAVEQUERIES
for details on enabling the$wpdb->queries
. – Sally CJ Commented Aug 3, 2019 at 1:40