你能帮我写一个困难的查询来计算wordpress上的点数吗?
情况如下:
在wordpress网站上有一个广告系统并搜索(过滤)。
主页会显示所有类别的列表,其中包含每个类别中的广告数量。
我的脚本有效,但速度非常慢。
<?php
$querystr = "SELECT * FROM $wpdb->posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE (wp_term_taxonomy.term_id = ".$category->cat_ID." AND wp_term_taxonomy.taxonomy = 'category'
AND wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish')";
$pageposts = $wpdb->get_results($querystr, OBJECT);
$count=0;
foreach($pageposts as $post) {
setup_postdata($post);
$today = date('Y-m-d H:i:S');
$validity = get_post_meta($post->ID, 'valid_until', true);
$city = get_post_meta($post->ID, 'post_location', true);
$action = get_post_meta($post->ID, 'post_action', true);
$cities = explode("|", $city);
$session_city = $_SESSION["session_city"];
if($_SESSION["session_city"] and !$_SESSION["session_action"] or $_SESSION["session_action"]=='All') {
if($validity >= $today and in_array($session_city, $cities)) { $count++;}
}
if($_SESSION["session_action"] and !$_SESSION["session_city"] or $_SESSION["session_city"]=='All') {
if($validity >= $today and $action == $_SESSION["session_action"]) { $count++;}
}
if($_SESSION["session_action"] and $_SESSION["session_city"]) {
if($validity >= $today and $action == $_SESSION["session_action"] and in_array($session_city, $cities)) { $count++;}
}
if(!isset($_SESSION["session_city"]) and !isset($_SESSION["session_action"])) {
if($validity >= $today) { $count++;}
}
}
echo $count;
?>
答案 0 :(得分:0)
wp_term_taxonomy needs INDEX(taxonomy, term_id)
wp_term_relationships needs INDEX(term_taxonomy_id, object_id)
请提供SHOW CREATE TABLE wp_term_relationships
;我怀疑它没有最佳定义。 (我假设它是另外两个表之间的多对多关系?)
如果可能,请提供EXPLAIN SELECT ...
。