0

我有以下 3 个表:

出版物(主键 =文章

article | title | pubType 

作者(主键 = *author_name*)

author_name | author_somethingelse

pub_author_map

article | author_name 

给定 的值pubType,我需要选择有关文章的信息,包括作者。为此,我有

SELECT p.article, p.title, p.dateTime, pam.author_name FROM publications p 
LEFT JOIN pub_author_map pam ON pam.article = p.article
LEFT JOIN authors a ON a.author_name = pam.author_name
WHERE p.pubType = '$pubType' ORDER BY p.article LIMIT 10

即使使用LIMIT 10,此查询也大约在 29 秒内完成。中有 1500 行publications和 3000 行pub_author_map

如何优化上述查询?

4

3 回答 3

1

您可以使用索引来优化表。例如,在检索数据时用作外键的索引列:这是一个很好的索引教程,http://www.tizag.com/mysqlTutorial/mysql-index.php索引会占用更多的磁盘空间,但是当您对数据库进行相对较多的查询时,它们可以节省数小时里面的数据。

于 2012-08-08T16:51:14.753 回答
1

添加索引

  • 专栏文章上的 pub_author_map

  • pubType 列上的出版物

例如像这样。

CREATE INDEX pub_author_idx_1 
    ON pub_author_map (article);

CREATE INDEX publications_idx_1
    ON publications (pubType);
于 2012-08-08T16:52:11.947 回答
1

如果 INNER JOIN 给出正确的结果,请使用它。仅当您需要它提供的结果时才使用 LEFT JOIN。

你想看没有作者的文章吗?

于 2012-08-08T16:53:13.813 回答