troubleshooting Question

binary masks or other solution for query

Avatar of sheep7
sheep7 asked on
MySQL ServerAlgorithmsSQL
5 Comments1 Solution665 ViewsLast Modified:
Hello, I was wondering someone could help me with this problem:

A person has the desire to find products matching his needs on ingredients: vitamines, proteines, sodium. He selects these options using a website(php) out of a list of 500 ingredients, no use of db so far, the user isn't in the db.

There is a db with 3 products
Product 1 consists of vitamines, proteines and sodium
Product 2 consists of vitamines and sodium
Product 3 consists of vitamines, proteines, sodium and fat

How can I query that only products 1 and 2 are in the result?
(since product 3 also contains fat as ingredient)

The database hasn't been created yet so I can design this for optium usage.
I expect a load of 5 user request a minute with 500,000 products containing each 1-30 ingredients. These ingredients are dynamic and change/add or get deleted.

---
I have found some solution in using subqueries
PRODUCT
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | YES  |     | NULL    |       |
| name  | varchar(255) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
 
INGEDIENT
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | YES  |     | NULL    |       |
| name  | varchar(255) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
 
PRODUCT_VITAMINE
+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| prod_id | int(11) | YES  |     | NULL    |       |
| ig_id  | int(11) | YES  |     | NULL    |       |
+---------+---------+------+-----+---------+-------+

select * from PRODUCT p where exists(select * FROM PRODUCT_VITAMINE pv,INGEDIENT i where pv.ig_id=i.id AND p.id = pv.prod_id and i.name in('vitamines','sodium','proteines')) AND not exists (select * FROM PRODUCT_VITAMINE pv,INGEDIENT i where pv.ig_id=i.id AND p.id = pv.prod_id and i.name  not in('vitamines','sodium','proteines'))
----

But I rather have 1 query and more simplier and faster. Maybe using binary masks. Anyone has an idea about how to arrange that for this problem or some other solution?

Regards and merry christmas!
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros