乍一看,我很感激这一点似乎显而易见,但我不确定具体情况如何。我正在做一个应用程序,从给定的膳食计划中计算购物清单。这涉及到许多迭代、表和到数据库的连接。表格包括:
MealPlan (含mealID,每天6餐,7天)
(包含每顿饭的名称和ID)
成分(包含系统中的每个成分)
MealIngredients (包含mealID和ingredientID的外键,显示哪些成分及其数量与一顿饭有关)
如果每顿饭平均包含10种食材,而我一周7天每天吃6顿饭,那就是420个单独的数据库连接,以获取特定配料的数据。
因此,相反,选择整个表是否更快/更有效?
备选案文1(每次新连接):
ingredientID = 5
For Each row As DataRow In ingredients.table.Rows
ingredients.setTableContents("SELECT `quantity` , `name`, `measurement` FROM `tblIngredients` WHERE ingredientID=" + ingredientID)
'Do stuff with the ingredients attributes
Next选项2(选择整个表和引用DataTable):
ingredientID = 5
dataTable = ingredients.setTableContents("SELECT `quantity` , `name`, `measurement` FROM `tblIngredients`")
For Each row As DataRow In ingredients.table.Rows
quantity = table.Rows(mealID).Item(0).ToString()
Next发布于 2020-07-26 14:46:22
在我看来,你可以通过为一顿特定的食物购买所有的食材来改善它。您可以使用JOIN实现这一点。
例如,
SELECT I.Name
FROM Meal AS M
JOIN MealIngredients AS MI
ON M.ID = MI.MealID
JOIN Ingredients AS I
ON MI.IngredientID = I.IngredientID
WHERE M.ID = 123可能产生以下结果:
Name
--------------
Brown bread
Cheddar cheese
Butter在这个例子中,您只需要为餐ID - 123传递一个值。
或者使用SELECT *查看查询中的所有可用内容:
ID Name MealID IngredientID IngredientID Name
----------------------------------------------------------------------------
123 Cheese sandwich 123 22 22 Brown bread
123 Cheese sandwich 123 54 54 Cheddar cheese
123 Cheese sandwich 123 51 51 Butter您应该显式地选择想要的列,即在实际代码中不要使用*。
https://stackoverflow.com/questions/63101216
复制相似问题