我有以下代码:
$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
我也试过:
$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
但当我抓取查询并将其放入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
为什么两种$wpdb方法都要花这么多时间?将同一查询直接复制并粘贴到phpmyadmin上,这将花费预期的时间。