views:

48

answers:

2

we have three types of data (tables):

  • Book (id,name,author...) ( about 3 million of rows)
  • Category (id,name) ( about 2000 rows)
  • Location (id,name) ( about 10000 rows)

A Book must have at least 1 type of Category (up to 3) AND a Book must have only one Location.

I need to correlate this data to get this query faster:

Select Books where Category = 'cat_id' AND Location = 'loc_id'

Select Books where match(name) against ('name of book') AND Location = 'loc_id'

Please I need some help. Thanks

A: 

With tables this size, which I would consider medium-sized (not small, not large), it is more likely the indices which will make the difference. Create the tables correctly and join them appropriately (on the indexed primary and foreign keys) and your performance should be fine.

MJB
Yes but how to correlate them... I mean a book can have 3 diff categories, where I put those ids? In Book table or I need another table to correlate them?Book->Category_ID1 Book->Category_ID2 Book->Category_ID3 and alsoBook->Location_ID
robert
+1  A: 

Have another table, say bookscategories, which has 'id , bookid, categoryid' as fields.

Use this to map books to categories.

Both your original queries will not be affected since the first query wants books in ONE specific category and location and the second query wants books that match a title and ONE location.

zaf
hmmm... if I make this table:| ID | BOOK_ID | CAT_ID | LOC_ID |How will be the query matching the NAME and LOC_ID ?
robert
I agree with zaf. Make the extra table, then try it out. I did not specifically say that in my answer -- I guess I should have.
MJB
@robert: don't make that table. Loc_Id should be in the BOOKS table, not the BOOKCATEGORY table.
MJB
@robert There is no mention of LOC_ID in my answer. The new table proposed is called 'bookscategories' - only BOOK ID AND CATEGORY ID mappings. The location information stays where it is in its own table.
zaf
Thanks all... I now try the solution... keep in touch.
robert
Another question:I want to select the total numbers of book grouped by category in a location but the query is still slow beacuse the 3 tables join.SELECT Category.name, count(*) as TOT FROM category, book, location, bookscategory WHERE bookscategory.book_id = book.id ANDbookscategory.cat_id = category.id ANDbook.loc_id = 'number'group by cat_id oreder by TOT desc
robert
Whoa - How many books you got?
zaf
:) it's a big project :)... there was a problem with an index.. now it seems to be ok... sys
robert
Yes, if you set the index's up wisely then you shouldn't really be having problems until you get to a million or two of rows. If you do then call me ;)
zaf
By the way, you should probably accept zaf's answer, given that it appears to be doing what you need.
MJB
@MJB cheers....
zaf