Postgres 函数:遍历记录和 Return 单值
Postgres Function: Loop through Records and Return Single Value
Postgres 的新手,正在尝试获取一个函数来获取我可以根据可用成分制作多少食谱。
recipe_id
required_ingredient_quantity
ingredient_id
ingredient_quantity
123
10.00
555
21.00
123
3.00
666
15.00
123
4.00
777
11.00
CREATE OR REPLACE FUNCTION fn_limiting_part(recipe_id VARCHAR)
RETURNS INT AS
$$
DECLARE
limiting_ingredient INT;
limiting_ingredient NUMERIC(6,2)[];
BEGIN
FOR record IN
SELECT
recipes_ingredients.recipe_id,
recipes_ingredients.required_ingredient_quantity,
ingredients.ingredient_id,
ingredients.ingredient_quantity
FROM ingredients
INNER JOIN recipes_ingredients
ON ingredients.ingredient_id = recipes_ingredients.ingredient_id
INTO
WHERE recipes_ingredients.recipe_id = fn_limiting_part.recipe_id;
LOOP
array_append(limiting_ingredient, ingredient_quantity * (1 / required_ingredient_quantity))
-- Find lowest limiting ingredient in limiting_ingredients array
-- Assign lowest amount to limiting_part variable
-- Use Floor to Round down to nearest whole number
END LOOP;
limiting_part := FLOOR();
RETURNS limiting_part
END;
$$
LANGUAGE PLPGSQL;
其他Questions/Considerations
- 对于 运行 postgresql 中的这种类型的逻辑,这将成为我正在制作的 GraphQL API 的一部分,这是一个很好的做法吗?return只是一个
INT
还是我应该在我的前端应用程序中进行单独的 GraphQL 查询来执行此逻辑?
您并不是真的需要 plpgsql 函数。普通 SQL、MIN
和 recipe_id
过滤就足够了。
create function fn_limiting_part(arg_recipe_id int) returns int as
$$
SELECT MIN(floor(ingredient_quantity/required_ingredient_quantity))::int
FROM ingredients
INNER JOIN recipes_ingredients
ON ingredients.ingredient_id = recipes_ingredients.ingredient_id
WHERE recipe_id = arg_recipe_id;
$$ language sql;
我认为在数据库中完成尽可能多的数据相关或域相关逻辑是一个很好的做法。这在性能、并发性和流量减少等方面带来了巨大的好处。
Postgres 的新手,正在尝试获取一个函数来获取我可以根据可用成分制作多少食谱。
recipe_id | required_ingredient_quantity | ingredient_id | ingredient_quantity |
---|---|---|---|
123 | 10.00 | 555 | 21.00 |
123 | 3.00 | 666 | 15.00 |
123 | 4.00 | 777 | 11.00 |
CREATE OR REPLACE FUNCTION fn_limiting_part(recipe_id VARCHAR)
RETURNS INT AS
$$
DECLARE
limiting_ingredient INT;
limiting_ingredient NUMERIC(6,2)[];
BEGIN
FOR record IN
SELECT
recipes_ingredients.recipe_id,
recipes_ingredients.required_ingredient_quantity,
ingredients.ingredient_id,
ingredients.ingredient_quantity
FROM ingredients
INNER JOIN recipes_ingredients
ON ingredients.ingredient_id = recipes_ingredients.ingredient_id
INTO
WHERE recipes_ingredients.recipe_id = fn_limiting_part.recipe_id;
LOOP
array_append(limiting_ingredient, ingredient_quantity * (1 / required_ingredient_quantity))
-- Find lowest limiting ingredient in limiting_ingredients array
-- Assign lowest amount to limiting_part variable
-- Use Floor to Round down to nearest whole number
END LOOP;
limiting_part := FLOOR();
RETURNS limiting_part
END;
$$
LANGUAGE PLPGSQL;
其他Questions/Considerations
- 对于 运行 postgresql 中的这种类型的逻辑,这将成为我正在制作的 GraphQL API 的一部分,这是一个很好的做法吗?return只是一个
INT
还是我应该在我的前端应用程序中进行单独的 GraphQL 查询来执行此逻辑?
您并不是真的需要 plpgsql 函数。普通 SQL、MIN
和 recipe_id
过滤就足够了。
create function fn_limiting_part(arg_recipe_id int) returns int as
$$
SELECT MIN(floor(ingredient_quantity/required_ingredient_quantity))::int
FROM ingredients
INNER JOIN recipes_ingredients
ON ingredients.ingredient_id = recipes_ingredients.ingredient_id
WHERE recipe_id = arg_recipe_id;
$$ language sql;
我认为在数据库中完成尽可能多的数据相关或域相关逻辑是一个很好的做法。这在性能、并发性和流量减少等方面带来了巨大的好处。