Hello to all. I have problems with the performance of the / php request. It seems like I'm just looping through too many result sets in inner loops in my php. I am sure there is a more efficient way to do this. Any help is greatly appreciated.
I have a table containing 3500 recipes ([recipe]):
rid | recipe_name
And another table that contains 600 different ingredients ([ingredients])
iid | i_name
Each recipe has x number of ingredients associated with it, and I use a nice join table to create an association ([recipe_ingredients])
uid | rid | iid
(where uid is only the unique identifier of the table)
For instance:
rid: 1 | recipe_name: Lemon Tart ..... iid: 99 | i_name: lemon curd iid: 154 | i_name: flour ..... 1 | 1 | 99 2 | 1 | 154
The query I'm trying to run allows the user to enter what ingredients they have, and he will tell you everything that you can do with these ingredients. He does not have to use all the ingredients, but you need to have all the ingredients for the recipe.
For example, if I had flour, egg, salt, milk and lemon curd, I could make Pancakes and Lemon Cake (if we assume that lemon tart has no other ingredients :)), but I could not 'Risotto' (since I didnβt have rice or anything else that was in it).
In my PHP, I have an array containing all the ingredients that the user has. They currently run this through each recipe (cycle 1), and then check all the ingredients in this recipe to see if each ingredient is in my array of ingredients (cycle 2). As soon as he finds the ingredient in the recipe, it is not in my array, he says no and goes on to the next recipe. If so, it stores the disk in a new array, which I use later to display the results.
But if we look at the effectiveness of this, if I assume 3500 recipes, and Ive got 40 ingredients in my array, the worst case scenario is 3500 x 40n, where n = the number of ingredients in the recipe. The best case is 3500 x 40 (does not find the ingredient for the first time for each recipe, so it comes out).
I think that my whole approach to this is wrong, and I think that there should be some kind of smart sql that I miss here. Any thoughts? I can always create a sql statement from the component array that I have.
Thanks a lot in advance, greatly appreciate