If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Cross Reference Design Problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-19-11, 09:49
tanra tanra is offline
Registered User
 
Join Date: Oct 2011
Posts: 10
Cross Reference Design Problem

I need help in databse design. It sounded very simple but somehow I am not able to come up with a good solution.

In database, we have only two items, user-id and item-id. The only goal is that we should be quickly able to query:

1) Given the user-id, the list of item-id which user has purchased
2) Given the item-id, the list of use-ids who purchased this item

There are more than million unique userids and more than million unique item-ids. Each user might be owning thousands of items. The brute-force approach would be to have duplicate rows but datasize will be too big in that case and I am hoping that someone will point me to a better design.

We use MySQL.

Thanks

Last edited by tanra; 10-19-11 at 09:54.
Reply With Quote
  #2 (permalink)  
Old 10-19-11, 09:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
perhaps you neglected to declare your indexes?
Code:
CREATE TABLE user_items
( user_id INTEGER NOT NULL
, item_id INTEGER NOT NULL 
, FOREIGN KEY ( user_id ) REFERENCES users ( user_id )
, FOREIGN KEY ( item_id ) REFERENCES items ( item_id )
, PRIMARY KEY ( user_id, item_id )
, INDEX item_users ( item_id, user_id )
);
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-19-11, 10:09
tanra tanra is offline
Registered User
 
Join Date: Oct 2011
Posts: 10
Hi,

Can you please explain it a bit more, how the query will look like.

Thanks

Quote:
Originally Posted by r937 View Post
perhaps you neglected to declare your indexes?
Code:
CREATE TABLE user_items
( user_id INTEGER NOT NULL
, item_id INTEGER NOT NULL 
, FOREIGN KEY ( user_id ) REFERENCES users ( user_id )
, FOREIGN KEY ( item_id ) REFERENCES items ( item_id )
, PRIMARY KEY ( user_id, item_id )
, INDEX item_users ( item_id, user_id )
);
Reply With Quote
  #4 (permalink)  
Old 10-19-11, 10:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
1) Given the user-id, the list of item-id which user has purchased
Code:
SELECT item_id
  FROM user_items
 WHERE user_id = 937

2) Given the item-id, the list of use-ids who purchased this item
Code:
SELECT user_id
  FROM user_items
 WHERE item_id = 42
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-26-11, 01:18
tanra tanra is offline
Registered User
 
Join Date: Oct 2011
Posts: 10
Thanks a lot!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On