以下查詢在我的WordPress的博客使用,它得到用戶已張貼在類別。當他在類別後,顯示該類別的名稱。非常非常慢的查詢。我該如何改進它?
這是一個非常非常慢的查詢,因爲它是一個大的數據庫,我有一個託管公司的問題。
我有3類,用所謂的新聞編號3,4名爲文章和5個名爲其他。我的代碼是:
<?php
$author = get_query_var('author');
$categories = $wpdb->get_results("
SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description
FROM $wpdb->posts as posts
LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
WHERE 1=1 AND (
posts.post_status = 'publish' AND
posts.post_author = '{$post->post_author}' AND
tax.taxonomy = 'category')
ORDER BY terms.term_id ASC
");
?>
<ul>
<?php foreach($categories as $category) : ?>
<?php if (($category->ID == '3') || ($category->ID == '4') || ($category->ID == '5')) { ?>
<li>
<a href="<?php echo get_category_link($category->ID); ?>/?author_name=<?php echo $curuser->user_login; ?>" title="<?php echo $category->name ?>">
<?php echo $category->name; ?>
</a>
</li>
<?php } ?>
<?php endforeach; ?>
</ul>
謝謝大家!
INDEX會做什麼? – EnexoOnoma 2011-04-19 22:28:47
索引是數據庫的基本功能。有很多需要了解的內容,超過Stack Overflow帖子中的內容,但是本質上它們加快了SELECT查詢的速度。缺點是它們減慢了INSERT/UPDATE查詢,並且*可能會佔用大量的磁盤空間。 MySQL手冊是一個很好的開始閱讀它們的地方:http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html – zombat 2011-04-19 22:32:13
我已經運行了查詢並且它說它已被正確執行。所以我猜他們沒有索引? – EnexoOnoma 2011-04-19 22:51:30