Pergunta

I am developing a recommendation engine, so that requires storing lots of data and keeping track of every move made by the user. So, basically my website is a product search engine and will be having sets of queries as users data. Following are some examples of the data set

Example

User1 :
1. Apple Ipod tOuch
2. Samsung Galaxy Ace Plus
3. HArry Porter 

User2 :
1. Product1
2. Product2

and so on.

One way(naive) could be having an ID associated with each of my users and then having a string corresponding to that ID which will be of this form(strings separated with ~) :-

Unique ID - Apple IPod TOuch~Samsung Galaxy Ace Plus~HArry Porter 

But this method won't be efficient considering how I would be playing with those data later on.

Can any one come up with a very efficient model fairly easily implementable in mysql ?

Comment if I am unclear in asking my doubt.

Foi útil?

Solução

The classic design is a table for users :

Users(user_id,user_name,reg_date....)

table for products :

Products(prod_id,prod_name,prod_cost....)

table with mapping user-->products :

User_products(user_id,prod_id ....)

Example :

Users :

user_id|user_name
1200   | User1 
7856   | User2

Products :

prod_id | prod_name
12900   | Apple Ipod tOuch
45673   | Samsung Galaxy Ace Plus
99876   | HArry Porter 
34590   | Product1
56283   | Product2

User_products :

user_id | prod_id
1200    |12900
1200    |45673
1200    |99876
7856    |34590
7856    |56283

Outras dicas

Avoid strings separated with some identifier because you'll have to work with the data submitted, otherwise your search engine will be very slow when you'll get really big amount of data. I think Grisha is absolutely right - user or product searches (numeric id searches), joined with mapping tables will output the result much faster than searches through text/varchar fields, separating the results, etc.

Using the canonical approach as proposed by Grisha, the query 'who has product 1' would be represented thus

select user.user_name
from users inner join user_products on users.user_id = user_products.user_id
inner join products on products.prod_id = user.products.prod_id
where products.prod_name = 'Product 1'

This may look complicated but it's actually very simple and very powerful. If there were another field in the user_products table such as purchase date, you could also find out when those users bought product 1, or find all the users who bought the product during a given period, by means of a simple extension to the query.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top