I have an n-to-m relationship between Author and Book .
Table author
ID Name 1 Follett 2 Rowling 3 Martin
Table
ID Title Category 1 A Dance with Dragons Fantasy 2 Harry Potter Fantasy 3 The Key to Rebecca Thriller 4 World without end Drama
Table book_author
authorId bookId 1 3 2 2 3 1 1 4
There are many more authors and books in the system. Now I want to select all authors who have a book in the Fantasy genre.
This is what I have come to so far:
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";
I am wondering how to optimize this query since the book with books is really big.
source share