![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have already posted this under 'Simple table organisation question' here is a more lucid version. It will be under MySQL v4.0 which now supports unions Consider 2 entities - books & authors and their respective tables: book(id, title, author_id) author(id, name) Now consider another entity: review. Reviews can be of either books or authors, so is this one entity or two? One table: · review(id, book_id, author_id, comments) Here for any one row either book_id or author_id will be null. · review(id, book_or_author_id, discriminator, comments) Here the foreign key (book_or_author_id) can represent either table and thus avoids null field values, the 'discriminator' column simply states which entity the review represents (eg 'A' for author or 'B' for book). Two tables: · book_review(id, book_id, comments) author_review(id, author_id, comments) Now the queries. For one table we have · select * from reviews left join book on book.id = review.book_id left join author on author.id = review.author_id The problem here is that for book reviews the row has a null value for author, obviously it would be nice to know this. · select * from reviews left join book on book.id = review.book_id left join author on author.id = review.author_id or author.id = book.author_id This addition achieves this but with the 'or' in the join condition forces a full table scan and long query times. May be restructuring the query can avoid this? For two tables a 'union' seems unavoidable. · select author.name, book.title, book_review.comments from book_review join book on book_review.book_id = book.id join author on author.id = book.author_id union select author.name, null, author_review.comments from author_review join author on author.id = author_review.author_id The null in the 2nd select replaces the book.title column for author reviews. This is very fast, but unions are a bit inelegant. So which approach is best, can the query for 1 table be improved or is it more correct to have 2 tables and do the union? Any other comments you may have on the approach to this would be welcomed |
#3
| |||
| |||
|
|
"VisionSet" <spam (AT) ntlworld (DOT) com> wrote in message news:AugYa.6804$yl6.2436 (AT) newsfep4-winn (DOT) server.ntli.net... I have already posted this under 'Simple table organisation question' here is a more lucid version. It will be under MySQL v4.0 which now supports unions Consider 2 entities - books & authors and their respective tables: book(id, title, author_id) author(id, name) Now consider another entity: review. Reviews can be of either books or authors, so is this one entity or two? One table: · review(id, book_id, author_id, comments) Here for any one row either book_id or author_id will be null. · review(id, book_or_author_id, discriminator, comments) Here the foreign key (book_or_author_id) can represent either table and thus avoids null field values, the 'discriminator' column simply states which entity the review represents (eg 'A' for author or 'B' for book). Two tables: · book_review(id, book_id, comments) author_review(id, author_id, comments) Now the queries. For one table we have · select * from reviews left join book on book.id = review.book_id left join author on author.id = review.author_id The problem here is that for book reviews the row has a null value for author, obviously it would be nice to know this. · select * from reviews left join book on book.id = review.book_id left join author on author.id = review.author_id or author.id = book.author_id This addition achieves this but with the 'or' in the join condition forces a full table scan and long query times. May be restructuring the query can avoid this? For two tables a 'union' seems unavoidable. · select author.name, book.title, book_review.comments from book_review join book on book_review.book_id = book.id join author on author.id = book.author_id union select author.name, null, author_review.comments from author_review join author on author.id = author_review.author_id The null in the 2nd select replaces the book.title column for author reviews. This is very fast, but unions are a bit inelegant. So which approach is best, can the query for 1 table be improved or is it more correct to have 2 tables and do the union? Any other comments you may have on the approach to this would be welcomed What, exactly, is inelegant about union? I marvel that you find union inelegant yet you apparently find NULL markers and outer joins elegant. |
#4
| |||
| |||
|
|
I have already posted this under 'Simple table organisation question' here is a more lucid version. It will be under MySQL v4.0 which now supports unions Consider 2 entities - books & authors and their respective tables: book(id, title, author_id) author(id, name) |
#5
| |||
| |||
|
|
"Bob Badour" <bbadour (AT) golden (DOT) net> wrote in message news:XRiYa.2148$aD.329118745 (AT) mantis (DOT) golden.net... "VisionSet" <spam (AT) ntlworld (DOT) com> wrote in message news:AugYa.6804$yl6.2436 (AT) newsfep4-winn (DOT) server.ntli.net... I have already posted this under 'Simple table organisation question' here is a more lucid version. It will be under MySQL v4.0 which now supports unions Consider 2 entities - books & authors and their respective tables: book(id, title, author_id) author(id, name) Now consider another entity: review. Reviews can be of either books or authors, so is this one entity or two? One table: · review(id, book_id, author_id, comments) Here for any one row either book_id or author_id will be null. · review(id, book_or_author_id, discriminator, comments) Here the foreign key (book_or_author_id) can represent either table and thus avoids null field values, the 'discriminator' column simply states which entity the review represents (eg 'A' for author or 'B' for book). Two tables: · book_review(id, book_id, comments) author_review(id, author_id, comments) Now the queries. For one table we have · select * from reviews left join book on book.id = review.book_id left join author on author.id = review.author_id The problem here is that for book reviews the row has a null value for author, obviously it would be nice to know this. · select * from reviews left join book on book.id = review.book_id left join author on author.id = review.author_id or author.id = book.author_id This addition achieves this but with the 'or' in the join condition forces a full table scan and long query times. May be restructuring the query can avoid this? For two tables a 'union' seems unavoidable. · select author.name, book.title, book_review.comments from book_review join book on book_review.book_id = book.id join author on author.id = book.author_id union select author.name, null, author_review.comments from author_review join author on author.id = author_review.author_id The null in the 2nd select replaces the book.title column for author reviews. This is very fast, but unions are a bit inelegant. So which approach is best, can the query for 1 table be improved or is it more correct to have 2 tables and do the union? Any other comments you may have on the approach to this would be welcomed What, exactly, is inelegant about union? I marvel that you find union inelegant yet you apparently find NULL markers and outer joins elegant. I'm asking for advice, I know little. |
|
I find extracting info on database from the internet desperate. |
|
Do you know of any resources that explain data modeling of real and other than straightforward situations. |
#6
| |||
| |||
|
|
"VisionSet" <spam (AT) ntlworld (DOT) com> wrote in message news:AugYa.6804$yl6.2436 (AT) newsfep4-winn (DOT) server.ntli.net... I have already posted this under 'Simple table organisation question' here is a more lucid version. It will be under MySQL v4.0 which now supports unions Consider 2 entities - books & authors and their respective tables: book(id, title, author_id) author(id, name) Now consider another entity: review. Reviews can be of either books or authors, so is this one entity or two? One table: · review(id, book_id, author_id, comments) Here for any one row either book_id or author_id will be null. · review(id, book_or_author_id, discriminator, comments) Here the foreign key (book_or_author_id) can represent either table and thus avoids null field values, the 'discriminator' column simply states which entity the review represents (eg 'A' for author or 'B' for book). Two tables: · book_review(id, book_id, comments) author_review(id, author_id, comments) Now the queries. For one table we have · select * from reviews left join book on book.id = review.book_id left join author on author.id = review.author_id The problem here is that for book reviews the row has a null value for author, obviously it would be nice to know this. · select * from reviews left join book on book.id = review.book_id left join author on author.id = review.author_id or author.id = book.author_id This addition achieves this but with the 'or' in the join condition forces a full table scan and long query times. May be restructuring the query can avoid this? For two tables a 'union' seems unavoidable. · select author.name, book.title, book_review.comments from book_review join book on book_review.book_id = book.id join author on author.id = book.author_id union select author.name, null, author_review.comments from author_review join author on author.id = author_review.author_id The null in the 2nd select replaces the book.title column for author reviews. This is very fast, but unions are a bit inelegant. So which approach is best, can the query for 1 table be improved or is it more correct to have 2 tables and do the union? Any other comments you may have on the approach to this would be welcomed What, exactly, is inelegant about union? I marvel that you find union inelegant yet you apparently find NULL markers and outer joins elegant. |
![]() |
| Thread Tools | |
| Display Modes | |
| |