sql - Multi-dimensional search on a table -
i have following table stores preferences of users in system
userid | product | brand | city | ------------------------------------------- | soap | tide | nyc | | cereal | dont-care | nyc | b | dont-care | tide | dont-care | c | shampoo | dont-care | dont-care |
i search based on user provided search values. if 1 searches for
city: nyc, brand: tide
the output should be:
| soap | tide | nyc | b | dont-care | tide | dont-care |
where if search for
brand: tide, product: soap
the result should be:
| soap | tide | nyc |
the current solution have, following query (where null represents 'don't care') going against mysql table:
select * user_preferences (product null or product = <user provided value>) , (brand null or brand = <user provided value>) , (city null or city = <user provided value>)
though works expected, [and + (or)] combination makes me think not right way this. once dataset increases, query not perform well.
what efficient way of storing , retrieving such data? there no-sql type approaches can used make efficient?
update
after googling around figured approach have may safest bet. 1 factor still ambivalent approach adding 'searchable' attribute mean adding new column.
this blog eav anti-pattern provides reading material on such scheme. see how friend-feed uses mysql take on storing variable attributes in table.
you use ifnull() function.
it this:
select * user_preferences ifnull(product, <user provided value>) = <user provided value> , ifnull(brand, <user provided value>) = <user provided value> , ifnull(city, <user provided value>) = <user provided value>
Comments
Post a Comment