【发布时间】:2012-10-10 16:26:59
【问题描述】:
Author 和 Book 之间存在 n 对 m 关系。
表格作者
ID Name
1 Follett
2 Rowling
3 Martin
桌书
ID Title Category
1 A Dance with Dragons Fantasy
2 Harry Potter Fantasy
3 The Key to Rebecca Thriller
4 World without end Drama
表 book_author
authorId bookId
1 3
2 2
3 1
1 4
系统中有更多的作者和书籍。现在我想选择所有拥有“奇幻”类型的书的作者。
这是我目前想到的:
select distinct a.id
from author a, book b, written w
where w.authorId = a.id and w.bookId = b.id and b.category = "Fantasy";
我想知道如何优化这个查询,因为特别是表书真的很大。
【问题讨论】:
-
你的表有索引吗?在哪些列上?
标签: sql